3 回答
TA贡献1877条经验 获得超1个赞
您可以使用条件聚合来解决此问题,只需对每个周期的相关值求和:
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贡献1810条经验 获得超4个赞
您可以使用分组依据
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贡献1757条经验 获得超7个赞
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 关注
- 107 浏览
添加回答
举报