3 回答

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊
您可以使用條件聚合來(lái)解決此問(wèn)題,只需對(duì)每個(gè)周期的相關(guān)值求和:
SELECT a.code,
SUM(CASE WHEN period='2019-12-31' THEN b.balance ELSE 0 END) AS `balance 2019-12-31`,
SUM(CASE WHEN period='2020-01-17' THEN b.balance ELSE 0 END) AS `balance 2020-01-17`,
SUM(CASE WHEN period='2020-01-24' THEN b.balance ELSE 0 END) AS `balance 2020-01-24`
FROM Area a
JOIN Person p ON p.code = A.code
JOIN BalanceDetail b ON b.customernumber = p.customernumber
GROUP BY a.code
輸出:
code balance 2019-12-31 balance 2020-01-17 balance 2020-01-24
1 110 150 180
2 150 40 170
3 140 0 0

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以使用分組依據(jù)
SELECT Person.Code, BalanceDetail.period, SUM(BalanceDetail.balance) as balance
FROM Person JOIN BalanceDetail
ON Person.customernumber= BalanceDetail.customernumber
WHERE Code IN (SELECT Code FROM Area WHERE Code NOT IN ('0004'))
group by Person.Code, BalanceDetail.period

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊
SELECT bd.period,
a.AreaName,
SUM(bd.balance) as balance
FROM Person AS p
JOIN BalanceDetail AS bd ON p.customernumber = bd.customernumber
JOIN Area AS a ON a.code = p.code
GROUP BY bd.period, p.code;
- 3 回答
- 0 關(guān)注
- 136 瀏覽
添加回答
舉報(bào)