私はmysqlで書かれた銀行データベースを持っています。生成グループごとのレコード/レポートに MySQL ピボット テーブルを使用しています。
これは、アカウント メンテナンス用のサンプル テーブルです。
----------------------------------------------------------------------------------
account_number | description | Transaction Amount | Balance | transaction_date
----------------------------------------------------------------------------------
1021212 1 0 0 2013-04-02
1021213 1 100 100 2013-04-01
1021212 1 1000 1000 2013-04-02
1021213 1 100 200 2013-04-01
1021214 1 500 500 2013-04-02
1021212 2 100 900 2013-04-09
日次 (月次) のトランザクションを表示する完全なレポートを実行する必要があります。
これは私が必要とするレポート形式です。
-----------------------------------------------------------------------------------
account_number | init_balance | total_banking | total_withdraw | final_balance
----------------------------------------------------------------------------------
1021212 0 1000 100 900
1021213 100 100 0 200
1021214 0 500 0 600
ピボット テーブル クエリを使用してこのレポートを生成しようとしていますが、初期残高フィールドと最終残高フィールドを除くすべての情報を取得できました。
これは私のサンプルクエリです。
SELECT account_number,
**xxxxxxxxxx AS init_balance,**
SUM(CASE WHEN m.description = '1' THEN m.amount END) AS total_banking,
SUM(CASE WHEN m.description = '2' THEN m.amount END) AS total_withdraw,
**xxxxxxxxxx AS final_balance**
FROM account
WHERE transaction_date BETWEEN '2013-04-01' AND '2013-04-30'
GROUP BY account_number
ORDER BY account_number
誰かが助けてくれるなら、結果をグループ化してテーブルの最初と最後の行を取得するためのピボットテーブルを書くパターンを教えてください。