自分用家計集計ソフトで使っているものです。
自分用なので、テーブルとかは汎用性は無視して自分勝手に作っています。
詳細はこちらをご覧下さい。
SELECT
CASE WHEN karikei>kashikei THEN karikei-kashikei
ELSE 0 END AS karizan,karikei,klname,kmname,kashikei,
CASE WHEN karikei
ELSE 0 END AS kashizan FROM
(SELECT SUM(karigaku) AS karikei,kamoku,
SUM(kashigaku) AS kashikei FROM
(SELECT
SUM(kingaku) AS karigaku,karicode AS kamoku,
0 AS kashigaku FROM torihiki AS t1
GROUP BY t1.karicode
UNION
SELECT
0 AS karigaku,kashicode AS kamoku,
SUM(kingaku) AS kashigaku FROM torihiki AS t2
Group BY t2.kashicode)
GROUP BY kamoku)
INNER JOIN kamoku_M ON kamoku=kmcode
INNER JOIN kamoku_L ON kl_id=klcode;
自分用なので、テーブルとかは汎用性は無視して自分勝手に作っています。
詳細はこちらをご覧下さい。
SELECT
CASE WHEN karikei>kashikei THEN karikei-kashikei
ELSE 0 END AS karizan,karikei,klname,kmname,kashikei,
CASE WHEN karikei
(SELECT SUM(karigaku) AS karikei,kamoku,
SUM(kashigaku) AS kashikei FROM
(SELECT
SUM(kingaku) AS karigaku,karicode AS kamoku,
0 AS kashigaku FROM torihiki AS t1
GROUP BY t1.karicode
UNION
SELECT
0 AS karigaku,kashicode AS kamoku,
SUM(kingaku) AS kashigaku FROM torihiki AS t2
Group BY t2.kashicode)
GROUP BY kamoku)
INNER JOIN kamoku_M ON kamoku=kmcode
INNER JOIN kamoku_L ON kl_id=klcode;
※コメント投稿者のブログIDはブログ作成者のみに通知されます