[AlaSQL] 多data查詢+累計
Total_Sales 表格
Name | Sales |
John | 10 |
Jennifer | 15 |
Stella | 20 |
Sophia | 40 |
Greg | 50 |
Jeff | 20 |
要算出累積總計,我們就鍵入以下的 SQL 語句:
SELECT a1.Name, a1.Sales, SUM(a2.Sales) Running_Total
FROM Total_Sales a1, Total_Sales a2
WHERE a1.Sales <= a2.Sales OR (a1.Sales=a2.Sales AND a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales DESC, a1.Name DESC;
FROM Total_Sales a1, Total_Sales a2
WHERE a1.Sales <= a2.Sales OR (a1.Sales=a2.Sales AND a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales DESC, a1.Name DESC;
結果:
Name | Sales | Running_Total |
Greg | 50 | 50 |
Sophia | 40 | 90 |
Stella | 20 | 110 |
Jeff | 20 | 130 |
Jennifer | 15 | 145 |
John | 10 | 155 |
var sql =
SELECT a1.Name, a1.Sales, SUM(a2.Sales) Running_Total
FROM ? a1, ? a2WHERE a1.Sales <= a2.Sales OR (a1.Sales=a2.Sales AND a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales DESC, a1.Name DESC;"
留言
張貼留言