我有一个包含4列Id, Date, Material, Amount
的表格。
示例数据如下所示:
--------------------------------------------
Id | Date | Material| Amount
--------------------------------------------
1 | 01/01/2013 | 1 | 500
2 | 01/01/2013 | 2 | 400
3 | 01/01/2013 | 1 | 200
4 | 02/01/2013 | 1 | 600
5 | 02/01/2013 | 2 | 200
6 | 03/01/2013 | 2 | 100
7 | 04/01/2013 | 1 | 800
8 | 05/01/2013 | 2 | 400
9 | 06/01/2013 | 1 | 800
10 | 06/01/2013 | 2 | 700
11 | 06/01/2013 | 1 | 600
我需要编写一个查询来获得如下结果:
-----------------------------------------------
Date | Material | DailyAmount | Total
-----------------------------------------------
01/01/2013 | 1 | 700 | 700
02/01/2013 | 1 | 400 | 1100
04/01/2013 | 1 | 800 | 1900
06/01/2013 | 1 | 1400 | 3300
Dailyamount
是日期中Amount
的总和。我只想选择Material = 1
。 Total
字段是DailyAmount
的总和,直到那天。
我搜索得很多,但我无法得到任何关于此的信息......
答案 0 :(得分:1)
您可以使用sum
函数的分析(窗口)变体来获取运行总计。应使用常规聚合sum
)来完成每日总和:
SELECT [date], [material], [DailyAmount],
SUM([DailyAmount]) OVER (ORDER BY [date]
ROWS BETWEEN UNBOUNDED PRECEDING AND
CURRENT ROW) AS [Total]
FROM (SELECT [date], material, SUM([amount] AS [DailyAmount]
FROM my_table
WHERE material = 1
GROUP BY [date], material) t
答案 1 :(得分:0)
WITH CTE AS
(
SELECT DATE1,MATERIAL,SUM(AMOUNT) AS AMOUNT
FROM DATA
GROUP BY DATE1,MATERIAL
)
SELECT DATE1,MATERIAL,SUM(AMOUNT) OVER(ORDER BY DATE1)
FROM CTE
WHERE MATERIAL=1
其他方式..
WITH CTE AS
(
SELECT DATE1,MATERIAL,SUM(AMOUNT) AS AMOUNT ,ROW_NUMBER() OVER(PARTITION BY MATERIAL ORDER BY DATE1) AS RN
FROM DATA
GROUP BY DATE1,MATERIAL
),
CTE1 AS
(
SELECT DATE1,MATERIAL,AMOUNT,RN FROM CTE WHERE RN=1
UNION ALL
SELECT C2.DATE1,C2.MATERIAL,C1.AMOUNT+C2.AMOUNT,C2.RN
FROM CTE1 C1
INNER JOIN CTE C2 ON C1.RN+1 = C2.RN AND C1.MATERIAL = C2.MATERIAL
)
SELECT * FROM CTE1 ORDER BY MATERIAL,DATE1
答案 2 :(得分:0)
可以使用CROSS JOIN
和;with cte as
(
select rn = row_number() over
(
order by [date]
),[Date],Material,sum(Amount) as Amount
from myTable
where Material = 1
group by [Date],Material
)
select t1.[Date],t1.Material,t1.Amount as DailyAmount,
sum(t2.Amount) as Total
from cte t1
cross join cte t2
where t2.rn <= t1.rn
group by t1.[Date],t1.Material,t1.Amount;
来实现此目的。
<强>查询强>
urljoin()
答案 3 :(得分:0)
;WITH CTE AS
(
SELECT
[Date]
,Material
,SUM(Amount) DailyAmount
FROM
YourTable
WHERE
Material = 1
GROUP BY
[Date]
,Material
)
SELECT
A.[Date]
,A.Material
,A.DailyAmount
,SUM(B.DailyAmount)
FROM CTE A
INNER JOIN
CTE B
ON
B.[Date]<=A.[Date]
GROUP BY
A.[Date]
,A.Material
,A.DailyAmount