如何将行计数为单个列值

时间:2017-12-02 06:21:23

标签: sql sql-server sql-server-2012

我试图获得两行计数并将其分配给一个列。

select distinct 
    userreferenceid, 'Level(' + cast(level as varchar(255))+')' GroupLevel,
    referenceid 
from 
    (select 
         t0.userreferenceid, 
         case level.level
            when 1 then t1.userreferenceid
            when 2 then t2.userreferenceid
            when 3 then t3.userreferenceid 
            when 4 then t4.userreferenceid
         end referenceid,
         level.level
     from 
         member_details t0
     left outer join 
         member_details t1 on t1.referenceid = t0.userreferenceid
     left outer join 
         member_details t2 on t2.referenceid = t1.userreferenceid
     left outer join 
         member_details t3 on t3.referenceid = t2.userreferenceid
     left outer join 
         member_details t4 on t4.referenceid = t3.userreferenceid
     cross join 
         (select 1 level 
          union all
          select 2 
          union all
          select 3 
          union all
          select 4) level) t
where 
    t.referenceid is not null

我的refernceid是用户用来注册的内容(referalid),userreferenceid是用户自己的id(自己的referenceid)。

对于上述查询,我​​的结果集显示如下

user
ref
ere
nceid   GroupLevel Referenceid
-------------------------------------------
REF101  Level(1)    REF143
REF101  Level(2)    REF113
REF101  Level(3)    REF119
REF101  Level(3)    REF227
REF101  Level(4)    REF245
REF101  Level(4)    REF251
REF107  Level(1)    REF221
REF107  Level(1)    REF257
REF107  Level(2)    REF119
REF107  Level(2)    REF227
REF107  Level(3)    REF125
REF107  Level(3)    REF161
REF107  Level(4)    REF191
REF113  Level(3)    REF191
REF119  Level(1)    REF125

我需要结果集看起来像这样。

我需要分配级别1,2,3,4,

中所有用户的每个refid和count
Userreferenceid  | Level1users|Level2Users|level3Users|Level4Users
-----------------+------------+-----------+-----------+------------
REF101                1            1           2           2
REF107                2            2           2           1
REF113                0            0           1           0
REF119                1            0           0           0

我的表结构是

userreferenceid (varchar)   ||    referenceid(varchar)

请帮我解决这个问题..提前致谢

2 个答案:

答案 0 :(得分:0)

试试这个 总和总和

select Userreferenceid,
sum(case when GroupLevel='Level(1)' then 1 else 0 end) Level1users,
sum(case when GroupLevel='Level(2)' then 1 else 0 end) Level2users,
sum(case when GroupLevel='Level(3)' then 1 else 0 end) Level3users,
sum(case when GroupLevel='Level(4)' then 1 else 0 end) Level4users
from mytable group by Userreferenceid

答案 1 :(得分:0)

您也可以通过执行动态SQL查询来完成。如果有很多GroupLevel值,那么编写查询将是忙乱的。

<强>查询

declare @sql as varchar(max);

select @sql = stuff((
        select distinct 
        ', sum(case when [GroupLevel] = ' + char(39) + [GroupLevel] + char(39) 
        + ' then 1 else 0 end) as [' + replace(replace([GroupLevel], '(', ''), 
')', '') + 'Users]'
        from [your_table_name]
        for xml path('')
    )
    , 1, 2, ''
);

select @sql = 'select [userreferenceid], ' + @sql 
                + 'from [your_table_name] '
                + 'group by [userreferenceid];';

exec(@sql);