到目前为止,我还不是SQL方面的专家,但是我正在尽我最大的努力。
在SQL中清理和转换数据后,我在SQL中有一个名为 PaymentData 的大表。我的一段数据如下所示:
--------------------------------------------------------------------
Date | Code | User ID | Payment Amount
2018-09-26 | 111 | 1 | 125
2018-09-26 | 122 | 1 | 300
2018-09-26 | 177 | 1 | 150
2018-09-26 | 116 | 1 | 130
2018-09-26 | 118 | 1 | 180
2018-09-26 | 111 | 2 | 290
2018-09-26 | 122 | 2 | 200
2018-09-26 | 188 | 2 | 200
2018-09-26 | 199 | 2 | 310
请注意,数据集某种程度上是递归的。用户每月获得报酬,并且这些付款被分配给某些分配代码。因此,很有可能,例如,用户1在2018-10-26具有相同的金额和代码。但是也可能某个用户在另一个月内只有几个(或更多)代码和不同的付款金额。
因此,我想创建一个新表,该表按ID和Date对数据进行分组,然后按某些代码获取总和。例如,我想有一列将代码111和122的付款金额相加为X,另一列Y仅将177中按ID和日期分组的金额相加,以此类推。像这样,只有用户1和2:
--------------------------------------------------------------------
Date | X | Y | User ID
2018-09-26 | 425 | 150 | 1
2018-09-26 | 490 | 0 | 2
简而言之:
新表将根据日期和ID分组的代码列表创建包含累计金额的列。例如,在X列中,每个日期和ID都有代码111和112的加法数,在Y列中有基于每个日期和ID的其他5个代码的总和,依此类推。
汇总此内容的最佳方法是什么,以便从原则上讲,我只能将每列的代码列表传递给SQL,以便它可以为我方便地进行处理?
答案 0 :(得分:3)
您可以在此处使用条件聚合
select
[Date]
,X = sum(case when Code in (111,222) then [Payment Amount] end)
,Y = sum(case when Code in (177) then [Payment Amount] end)
,[User ID]
from
SomeTable
group by
[Date], [User ID]
或使用IIF
select
[Date]
,X = sum(iif(Code in (111,222),[Payment Amount],0))
,Y = sum(iif(Code = 177,[Payment Amount],0))
,[User ID]
from
SomeTable
创建一个视图,而不是将其存储在另一个表中。
create view MyViewName
as
select
[Date]
,X = sum(case when Code in (111,222) then [Payment Amount] end)
,Y = sum(case when Code in (177) then [Payment Amount] end)
,[User ID]
from
SomeTable
然后命名...
select * from MyViewName
答案 1 :(得分:2)
CREATE VIEW STUFF AS
SELECT
[Date],
SUM(CASE WHEN CODE IN ('111','122') THEN [Payment Amount] ELSE 0 END AS x,
SUM(CASE WHEN CODE = '177' THEN [Payment Amount] ELSE 0 END AS y,
[user id]
FROM PaymentData
GROUP BY [Date], [user id]
答案 2 :(得分:0)
使用条件聚合,例如
CREATE TABLE T
([Date] datetime, [Code] int, [User ID] int, [Payment Amount] int)
;
INSERT INTO T
([Date], [Code], [User ID], [Payment Amount])
VALUES
('2018-09-26 00:00:00', 111, 1, 125),
('2018-09-26 00:00:00', 122, 1, 300),
('2018-09-26 00:00:00', 177, 1, 150),
('2018-09-26 00:00:00', 116, 1, 130),
('2018-09-26 00:00:00', 118, 1, 180),
('2018-09-26 00:00:00', 111, 2, 290),
('2018-09-26 00:00:00', 122, 2, 200),
('2018-09-26 00:00:00', 188, 2, 200),
('2018-09-26 00:00:00', 199, 2, 310)
;
SELECT [Date],
[User ID],
SUM(CASE WHEN Code IN (111, 122) THEN [Payment Amount] ELSE 0 END) X,
SUM(CASE WHEN Code = 177 THEN [Payment Amount] ELSE 0 END) Y
FROM T
GROUP BY [Date], [User ID];
返回:
+---------------------+---------+-----+-----+
| Date | User ID | X | Y |
+---------------------+---------+-----+-----+
| 26/09/2018 00:00:00 | 1 | 425 | 150 |
| 26/09/2018 00:00:00 | 2 | 490 | 0 |
+---------------------+---------+-----+-----+
更新:
根据您的comment,Code
数据类型不是int,因此您可以更改
SUM(CASE WHEN Code IN ('111', '122') THEN [Payment Amount] ELSE 0 END) X,
SUM(CASE WHEN Code = '177' THEN [Payment Amount] ELSE 0 END) Y