SQL将结果集分组
时间:2011-11-26
来源:互联网
ID(int) Name(varcher) LB(int) SL(int)
1 A 1 100
2 A 2 80
3 B 1 50
4 B 2 30
5 C 1 20
......
如何利用Name合并成如下结果集
ID Name LB1 SL1 LB2 SL2
1 A 1 100 2 80
2 B 1 50 2 30
3 C 1 20 2 0 (注意C结果集不存在LB = 2的记录)
......
作者: mzcih 发布时间: 2011-11-26
use Tempdb go --> --> if not object_id(N'Tempdb..#T') is null drop table #T Go Create table #T([ID] int,[Name] nvarchar(1),[LB] int,[SL] int) Insert #T select 1,N'A',1,100 union all select 2,N'A',2,80 union all select 3,N'B',1,50 union all select 4,N'B',2,30 union all select 5,N'C',1,20 Go declare @s nvarchar(4000) set @s='' Select @s=@s+N','+quotename('LB'+RTRIM(LB))+N'=max(case when LB=N'+quotename(LB,'''')+N' then LB else '''' end),' +quotename('SL'+RTRIM(LB))+N'=max(case when LB=N'+quotename(LB,'''')+N' then SL else '''' end)' from #T group by LB --顯示生成語句 print N'select ID=row_number()over(order by min(ID)), Name'+@s+N' from #T group by Name' exec(N'select ID=row_number()over(order by min(ID)), Name'+@s+N' from #T group by Name') /* ID Name LB1 SL1 LB2 SL2 1 A 1 100 2 80 2 B 1 50 2 30 3 C 1 20 0 0 */
作者: roy_88 发布时间: 2011-11-26
;with cte as ( select a.Name,b.LB from (select distinct Name from tb) a cross join (select distinct LB from tb)b ) select row_number() over (order by getdate()) as id, a.Name,1 as LB1,sum(case when a.LB=1 then b.SL else 0 end) SL1, 2 as LB2,sum(case when a.LB=2 then b.SL else 0 end) SL2 from cte a join tb b on a.Name = b.Name and a.LB = b.LB group by a.Name
作者: AcHerat 发布时间: 2011-11-26
作者: AcHerat 发布时间: 2011-11-26
SQL code
use Tempdb go --> --> if not object_id(N'Tempdb..#T') is null drop table #T Go Create table #T([ID] int,[Name] nvarchar(1),[LB] int,[SL] int) Insert #T select 1,N'A',1,100 union all select 2,N'A',2,80 union all select 3,N'B',1,50 union all select 4,N'B',2,30 union all select 5,N'C',1,20 Go DECLARE @s NVARCHAR(4000),@i NVARCHAR(3) Select TOP 1 @i=COUNT(*),@s='' from #T GROUP BY Name order by count(*) desc WHILE @i>0 SELECT @s=N',[LB'+@i+']=max(case when Row='+@i+N' then LB else 0 end),'+'[SL'+@i+']=max(case when Row='+@i+N' then SL else 0 end)'+@s,@i=@i-1 EXEC(N'SELECT ID=row_number()over(order by min(ID)),Name'+@s+N' FROM (select *, row=row_number()over(partition by Name order by Name)from #T) as a GROUP BY Name') go /* ID Name LB1 SL1 LB2 SL2 1 A 1 100 2 80 2 B 1 50 2 30 3 C 1 20 0 0 */
作者: roy_88 发布时间: 2011-11-26
Create table tb([ID] int,[Name] nvarchar(1),[LB] int,[SL] int) Insert tb select 1,N'A',1,100 union all select 2,N'A',2,80 union all select 3,N'B',1,50 union all select 4,N'B',2,30 union all select 5,N'C',1,20 Go ;with cte as ( select a.Name,b.LB from (select distinct Name from tb) a cross join (select distinct LB from tb)b ) select row_number() over (order by getdate()) as id, a.Name,1 as LB1,sum(case when a.LB=1 then isnull(b.SL,0) else 0 end) SL1, 2 as LB2,sum(case when a.LB=2 then isnull(b.SL,0) else 0 end) SL2 from cte a left join tb b on a.Name = b.Name and a.LB = b.LB group by a.Name drop table tb /******************* id Name LB1 SL1 LB2 SL2 -------------------- ---- ----------- ----------- ----------- ----------- 1 A 1 100 2 80 2 B 1 50 2 30 3 C 1 20 2 0 (3 行受影响)
作者: AcHerat 发布时间: 2011-11-26
作者: mzcih 发布时间: 2011-11-26
作者: mzcih 发布时间: 2011-11-26
if object_id('tb','U') is not null drop table tb go create table tb ( id int identity(1,1), name varchar(10), lb int, sl int ) go insert into tb (name,lb,sl) select 'A',1,100 union all select 'A',2,80 union all select 'B',1,50 union all select 'B',2,30 union all select 'C',1,20 go select * from tb a where not exists(select 1 from tb where name=a.name and sl>a.sl) go /* id name lb sl ----------- ---------- ----------- ----------- 1 A 1 100 3 B 1 50 5 C 1 20 (3 行受影响) */
作者: pengxuan 发布时间: 2011-11-26
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28