如圖資料庫,以下是我個人想做個收支報表語法
SELECT
DATE_FORMAT(`data`, '%Y-%m') 日期,
SUM(CASE WHEN type IN ('現金提','金融卡') THEN cashout ELSE 0 END) ATM提領,
SUM(CASE WHEN type = '中信卡' THEN cashout ELSE 0 END) 中國信託信用卡,
SUM(CASE WHEN type = '跨行轉' AND note = '繳遠銀信用卡款' THEN cashout ELSE 0 END) 遠東商銀信用卡,
SUM(CASE WHEN type = '委代扣' AND note = '台新銀行信用卡' THEN cashout ELSE 0 END) 台新銀行信用卡,
SUM(CASE WHEN type = '電話費' THEN cashout ELSE 0 END) 電話及家裡網路,
SUM(CASE WHEN type = '手續費' THEN cashout ELSE 0 END) 跨行手續費支出,
balance 月結存,
sum(cashout) 總支出,
sum(cashin) 總收入
FROM
banking
GROUP BY
DATE_FORMAT(`data`, '%Y-%m')
ORDER BY
`data` DESC
目前問題是balance 月結存這個我想撈每個月的最後一筆的balance欄位資料,就卡在這了= =,希望前輩們給些指點