SELECT [DATE], [AMOUNT], SUM(AMOUNT) OVER (ORDER BY DATE) AS 'Running Total'
FROM PeopleActi
WHERE INSTANCE = 'Bank'
AND DATE IS NOT NULL
GROUP BY [DATE], [AMOUNT];
在上面的代码中,我选择一个用户的日期,金额和"SUM(AMOUNT) OVER (ORDER BY DATE) AS 'Running Total'"
是他们在一段时间内的费用的总计。运行此代码时,我得到以下结果:
DATE AMOUNT Running Total
2018-10-05 100 100
2018-10-06 1000 1100
2018-10-07 5000 6100
2018-10-08 2000 8100
2018-10-09 1000 9100
2018-10-10 5000 14100
2018-10-11 3000 25100
2018-10-11 8000 25100
这很好用,但是我的问题是最后两行。我希望按日期将它们分组,并具有同一天的总数,所以应该是:
Date Amount Running Total
2018-10-11 11000 25100
有人知道如何实现吗?我的[DATE]
的类型为DATE。
更新!!!
我看过您的一些解决方案,它们很好,但重要的是,我也显示了AMOUNT和Running Total金额,所以最终结果应该是...
DATE AMOUNT Running Total
2018-10-05 100 100
2018-10-06 1000 1100
2018-10-07 5000 6100
2018-10-08 2000 8100
2018-10-09 1000 9100
2018-10-10 5000 14100
2018-10-11 11000 25100
谢谢大家的帮助!
答案 0 :(得分:2)
将金额分组,然后进行累计总计
WITH CTE
AS
(
SELECT A.Dt,
SUM(A.Amount) AS Amount
FROM (
VALUES ('2018-10-05',100),
('2018-10-06',1000),
('2018-10-07',5000),
('2018-10-08',2000),
('2018-10-09',1000),
('2018-10-10',5000),
('2018-10-11',3000),
('2018-10-11',8000)
) AS A(Dt,Amount)
GROUP BY A.Dt
)
SELECT C.Dt,
C.Amount,
SUM(C.Amount) OVER (ORDER BY C.Dt) AS CumTotal
FROM CTE AS C;
答案 1 :(得分:1)
尝试如下
SELECT [DATE],sum( [AMOUNT]), SUM(AMOUNT) OVER (ORDER BY DATE) AS 'Running Total'
FROM PeopleActi
WHERE INSTANCE = 'Bank'
AND DATE IS NOT NULL
GROUP BY [DATE]
答案 2 :(得分:1)
如果您需要摸索总和,那么为什么要使用窗口函数,仅聚合就足够了:
SELECT [DATE], SUM([AMOUNT])
FROM PeopleActi
WHERE INSTANCE = 'Bank' AND DATE IS NOT NULL
GROUP BY [DATE];
答案 3 :(得分:1)
group by中的每个字段都将导致其潜在地创建新行。如果您对金额字段求和并将其从分组中删除,那应该可以解决问题。编辑:我看到了这个问题,我在下面提供了一个完全独立的查询示例,您可以对其进行调整。
DECLARE @PeopleActi TABLE ([DATE] DATE,[AMOUNT] MONEY)
INSERT INTO @PeopleActi SELECT '2018-10-05',100
INSERT INTO @PeopleActi SELECT '2018-10-06',1000
INSERT INTO @PeopleActi SELECT '2018-10-07',5000
INSERT INTO @PeopleActi SELECT '2018-10-08',2000
INSERT INTO @PeopleActi SELECT '2018-10-09',1000
INSERT INTO @PeopleActi SELECT '2018-10-10',5000
INSERT INTO @PeopleActi SELECT '2018-10-11',3000
INSERT INTO @PeopleActi SELECT '2018-10-11',8000
SELECT *, SUM(AMOUNT) OVER (ORDER BY DATE) AS 'Running Total'
FROM (
SELECT [DATE], SUM([AMOUNT]) AS AMOUNT
FROM @PeopleActi
WHERE DATE IS NOT NULL
GROUP BY [DATE]
) a
GROUP BY [DATE],Amount
答案 4 :(得分:1)
尝试一下
;WITH CTe([DATE],AMOUNT)
AS
(
SELECT '2018-10-05', 100 UNION ALL
SELECT '2018-10-06', 1000 UNION ALL
SELECT '2018-10-07', 5000 UNION ALL
SELECT '2018-10-08', 2000 UNION ALL
SELECT '2018-10-09', 1000 UNION ALL
SELECT '2018-10-10', 5000 UNION ALL
SELECT '2018-10-11', 3000 UNION ALL
SELECT '2018-10-11', 8000
)
SELECT DISTINCT [DATE],SUM(AMOUNT)OVER(PARTITION BY [DATE] ORDER BY [DATE]) AMOUNT , SUM(AMOUNT)OVER( ORDER BY [DATE]) AS RuningTot FROM CTe
脚本
SELECT DISTINCT [DATE],
SUM(AMOUNT)OVER(PARTITION BY [DATE] ORDER BY [DATE]) AS AMOUNT,
SUM(AMOUNT) OVER (ORDER BY DATE) AS 'Running Total'
FROM PeopleActi
WHERE INSTANCE = 'Bank'
AND DATE IS NOT NULL
答案 5 :(得分:1)
我会使用CTE首先按日期分组,然后再进行累计。 所以像
with myAmounts AS
(
SELECT [DATE], SUM([AMOUNT]) AS Amount
FROM PeopleActi
WHERE INSTANCE = 'Bank'
AND DATE IS NOT NULL
GROUP BY [DATE]
)
SELECT [DATE], [AMOUNT], SUM(AMOUNT) OVER (ORDER BY DATE) AS 'Running Total'
FROM myAmounts
GROUP BY [DATE], [AMOUNT]
;
HTH,
B
ps;刚刚看到它的答案与另一个..实际解码中的答案相同
答案 6 :(得分:1)
尝试子选择:
SELECT p.[DATE], p.[AMOUNT], SUM(AMOUNT) OVER (ORDER BY DATE) AS 'Running Total'
FROM
(
select [date], sum([amount]) as Amount from PeopleActi
WHERE INSTANCE = 'Bank'
AND DATE IS NOT NULL
group by [date]
) p
GROUP BY p.[DATE], p.[AMOUNT]