一个查询分组的SQL
表结构
table
id 名称 分类 编码 用户ID
1 A 1 1 1111
2 B 1 0 3333
3 C 2 0 3333
4 D 3 0 3333
5 E 3 0 4444
6 F 3 0 4444
要求查询的结果显示
用户ID (分类1编码0的数据) (分类1编码1的数据) (分类2编码0的数据) (分类2编码1的数据) (分类3编码0的数据) (分类3编码1的数据)
1111 0 1 0 0 0 0
3333 1 0 1 0 1 0
4444 0 0 0 0 2 0
分类列的值 只可能是0或1或2或3,代码列的值只可能是0或1
按userid列,type列,code列分组
查询每个用户的每个分类下的每个code代码,有多少条数据
固定显示7列,分类为0的数据不查询
[解决办法]
- SQL code
if object_id('tb') is not null drop table tbgocreate table tb( id int identity(1,1), 名称 varchar(10), 分类 int, 编码 int, 用户ID int)goinsert into tb(名称,分类,编码,用户ID)select 'A',1,1,1111 union allselect 'B',1,0,3333 union allselect 'C',2,0,3333 union allselect 'D',3,0,3333 union allselect 'E',3,0,4444 union allselect 'F',3,0,4444goselect 用户ID, 分类1编码0的数据=sum(case when 分类=1 and 编码=0 then 1 else 0 end), 分类1编码1的数据=sum(case when 分类=1 and 编码=1 then 1 else 0 end), 分类2编码0的数据=sum(case when 分类=2 and 编码=0 then 1 else 0 end), 分类2编码1的数据=sum(case when 分类=2 and 编码=1 then 1 else 0 end), 分类3编码0的数据=sum(case when 分类=3 and 编码=0 then 1 else 0 end), 分类3编码1的数据=sum(case when 分类=3 and 编码=1 then 1 else 0 end)from tb group by 用户IDgo/*用户ID 分类1编码0的数据 分类1编码1的数据 分类2编码0的数据 分类2编码1的数据 分类3编码0的数据 分类3编码1的数据----------- ----------- ----------- ----------- ----------- ----------- -----------1111 0 1 0 0 0 03333 1 0 1 0 1 04444 0 0 0 0 2 0(3 行受影响)*/
[解决办法]
- SQL code
select 用户id, sum(case when 分类 = 1 and 编码 = 0 then 1 else 0 end) [分类1编码0的数据], sum(case when 分类 = 1 and 编码 = 1 then 1 else 0 end) [分类1编码1的数据], sum(case when 分类 = 2 and 编码 = 0 then 1 else 0 end) [分类2编码0的数据], sum(case when 分类 = 2 and 编码 = 1 then 1 else 0 end) [分类2编码1的数据], sum(case when 分类 = 3 and 编码 = 0 then 1 else 0 end) [分类3编码0的数据], sum(case when 分类 = 3 and 编码 = 1 then 1 else 0 end) [分类3编码1的数据]from [table]group by 用户id
[解决办法]
- SQL code
create table [table](id int,名称 varchar(10),分类 int,编码 int,用户ID int)insert into [table] values(1 ,'A', 1 ,1 ,1111)insert into [table] values(2 ,'B', 1 ,0 ,3333)insert into [table] values(3 ,'C', 2 ,0 ,3333)insert into [table] values(4 ,'D', 3 ,0 ,3333)insert into [table] values(5 ,'E', 3 ,0 ,4444)insert into [table] values(6 ,'F', 3 ,0 ,4444)goselect 用户id, sum(case when 分类 = 1 and 编码 = 0 then 1 else 0 end) [分类1编码0的数据], sum(case when 分类 = 1 and 编码 = 1 then 1 else 0 end) [分类1编码1的数据], sum(case when 分类 = 2 and 编码 = 0 then 1 else 0 end) [分类2编码0的数据], sum(case when 分类 = 2 and 编码 = 1 then 1 else 0 end) [分类2编码1的数据], sum(case when 分类 = 3 and 编码 = 0 then 1 else 0 end) [分类3编码0的数据], sum(case when 分类 = 3 and 编码 = 1 then 1 else 0 end) [分类3编码1的数据]from [table]group by 用户iddrop table [table]/*用户id 分类1编码0的数据 分类1编码1的数据 分类2编码0的数据 分类2编码1的数据 分类3编码0的数据 分类3编码1的数据 ----------- ----------- ----------- ----------- ----------- ----------- ----------- 1111 0 1 0 0 0 03333 1 0 1 0 1 04444 0 0 0 0 2 0(所影响的行数为 3 行)*/
[解决办法]
- SQL code
select 用户id, sum(case when 分类 = 1 and 编码 = 0 then 1 else 0 end) as [分类1编码0的数据], sum(case when 分类 = 1 and 编码 = 1 then 1 else 0 end) as [分类1编码1的数据], sum(case when 分类 = 2 and 编码 = 0 then 1 else 0 end) as [分类2编码0的数据], sum(case when 分类 = 2 and 编码 = 1 then 1 else 0 end) as [分类2编码1的数据], sum(case when 分类 = 3 and 编码 = 0 then 1 else 0 end) as [分类3编码0的数据], sum(case when 分类 = 3 and 编码 = 1 then 1 else 0 end) as [分类3编码1的数据]from tbgroup by 用户id