如何将结果合并到一行

时间:2018-02-15 10:54:11

标签: sql-server tsql pivot

我有一个查询,我正在尝试使用SQL服务器整合到一行。 它一直强制我按Frequency列进行分组,但我只想按UserId进行分组。任何人都可以让我知道我做错了什么。

CODE:

SELECT userid, frequency,
(select count(frequency) where frequency = 1) AS Onetime,
(select count(frequency) where frequency = 2) AS Daily,
(select count(frequency) where frequency = 3) AS Weekly,
(select count(frequency) where frequency = 4) AS Fortnightly,
(select count(frequency) where frequency = 5) AS Monthly,
(select count(frequency) where frequency = 6) AS Quarterly,
(select count(frequency) where frequency = 7) AS SemiAnnual,
(select count(frequency) where frequency = 8) AS Annual
FROM AppTasks
where userid = 'userid@gmail.com'
group by UserID, Frequency

数据:

image

3 个答案:

答案 0 :(得分:2)

你可以尝试一下吗?

SELECT userid,
SUM(CASE WHEN frequency = 1 THEN 1 ELSE 0 END) AS Onetime,
SUM(CASE WHEN frequency = 2 THEN 1 ELSE 0 END) AS Daily,
SUM(CASE WHEN frequency = 3 THEN 1 ELSE 0 END) AS Weekly,
SUM(CASE WHEN frequency = 4 THEN 1 ELSE 0 END) AS Fortnightly,
SUM(CASE WHEN frequency = 5 THEN 1 ELSE 0 END) AS Monthly,
SUM(CASE WHEN frequency = 6 THEN 1 ELSE 0 END) AS Quarterly,
SUM(CASE WHEN frequency = 7 THEN 1 ELSE 0 END) AS SemiAnnual,
FROM AppTasks
where userid = 'userid@gmail.com'
group by UserID;

答案 1 :(得分:1)

原始查询无效,因为SELECT语句包含7个子查询,每个子查询都计算所有记录,其中包含外部查询返回的频率,而不仅仅是用户的。

您可以使用PIVOT计算特定频率数的计数,并将其显示为列:

declare @apptasks table (userid nvarchar(20),frequency int)

insert into @apptasks
values
('userid@gmail.com',1),
('userid@gmail.com',2),
('userid@gmail.com',3),
('userid@gmail.com',4),
('userid@gmail.com',5),
('userid@gmail.com',6),
('userid@gmail.com',7),
('userid@gmail.com',7),
('userid@gmail.com',2),
('userid@gmail.com',1),
('userid@gmail.com',1)


select 
    userid, 
    [1] AS Onetime,
    [2] AS Daily,
    [3] AS Weekly,
    [4] AS Fortnightly,
    [5] AS Monthly,
    [6] AS Quarterly,
    [7] AS SemiAnnual
from ( select 
              userid,
              frequency 
       from @apptasks) as source
PIVOT (
    count(frequency) for Frequency in ([1],[2],[3],[4],[5],[6],[7])
) as pvt

结果是

userid            Onetime   Daily   Weekly  Fortnightly Monthly Quarterly   SemiAnnual
userid@gmail.com  3         2       1       1           1       1           2

如果您事先知道要使用哪些值,PIVOT和UNPIVOT会工作。 PIVOT实质上计算IN子句中字段(频率)值的聚合(COUNT(*)),并将它们作为以值命名的列返回。

您也可以省略频率值:

select 
    userid, 
    [1] AS Onetime,
    [2] AS Daily,
    [3] AS Weekly,
    [4] AS Fortnightly,
    [5] AS Monthly
from (select userid,frequency from @apptasks) as source
PIVOT 
(
        count(frequency) for Frequency in ([1],[2],[3],[4],[5])
) as pvt

答案 2 :(得分:0)

只是因为您只希望按userID分组,所以请从选择陈述中移除frequency作为下一个: -

SELECT userid,
(select count(frequency) where frequency = 1) AS Onetime,
(select count(frequency) where frequency = 2) AS Daily,
(select count(frequency) where frequency = 3) AS Weekly,
(select count(frequency) where frequency = 4) AS Fortnightly,
(select count(frequency) where frequency = 5) AS Monthly,
(select count(frequency) where frequency = 6) AS Quarterly,
(select count(frequency) where frequency = 7) AS SemiAnnual,
(select count(frequency) where frequency = 8) AS Annual
FROM AppTasks
where userid = 'userid@gmail.com'
group by UserID