首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

这样的SQL语句如何写啊

2012-03-06 
这样的SQL语句怎么写啊!有这样结构的表格:表名为:test类型数量小组甲10A乙8A丙9A丁11A甲12B乙10B丙9B丁8B

这样的SQL语句怎么写啊!
有这样结构的表格:表名为:test

类型         数量         小组
甲               10             A
乙               8               A
丙               9               A
丁               11             A
甲               12             B
乙               10             B
丙               9               B
丁               8               B

现在想得到这样的数据结果:
小组           甲             乙       丙     丁
A                 10             8         9       11
B                 12             10       9       8  
  这样的SQL,怎么写啊,请各位达人,指点迷津啊!在线等!



[解决办法]
select 小组,
甲=max(case when 类型= '甲 ' then 数量 else 0 end),
乙=max(case when 类型= '乙 ' then 数量 else 0 end),
丙=max(case when 类型= '丙 ' then 数量 else 0 end),
丁=max(case when 类型= '丁 ' then 数量 else 0 end)
from tbName
group by 小组
[解决办法]

create table T(类型 varchar(10), 数量 int, 小组 varchar(10))
insert T select '甲 ', 10, 'A '
union all select '乙 ' , 8, 'A '
union all select '丙 ', 9, 'A '
union all select '丁 ', 11, 'A '
union all select '甲 ', 12, 'B '
union all select '乙 ', 10, 'B '
union all select '丙 ', 9 , 'B '
union all select '丁 ', 8, 'B '

select 小组,
甲=max(case when 类型= '甲 ' then 数量 else 0 end),
乙=max(case when 类型= '乙 ' then 数量 else 0 end),
丙=max(case when 类型= '丙 ' then 数量 else 0 end),
丁=max(case when 类型= '丁 ' then 数量 else 0 end)
from T
group by 小组

--result
小组 甲 乙 丙 丁
---------- ----------- ----------- ----------- -----------
A 10 8 9 11
B 12 10 9 8

(2 row(s) affected)
[解决办法]

create table T(类型 varchar(10), 数量 int, 小组 varchar(10))
insert T select '甲 ', 10, 'A '
union all select '乙 ' , 8, 'A '
union all select '丙 ', 9, 'A '
union all select '丁 ', 11, 'A '
union all select '甲 ', 12, 'B '
union all select '乙 ', 10, 'B '
union all select '丙 ', 9 , 'B '
union all select '丁 ', 8, 'B '



declare @sql varchar(8000)
set @sql= 'select 小组, '
select @sql=@sql+quotename(类型)+ '=max(case when 类型= '+quotename(类型, ' ' ' ')+ ' then 数量 else 0 end), ' from T group by 类型
select @sql=left(@sql, len(@sql)-1), @sql=@sql+ 'from T group by 小组 '
exec(@sql)

--result
小组 丙 丁 甲 乙
---------- ----------- ----------- ----------- -----------
A 9 11 10 8
B 9 8 12 10

[解决办法]

create table a(类型 char(10),数量 int,小组 char(10))
insert into a select '甲 ', 10, 'A '
union all select '乙 ', 8, 'A '
union all select '丙 ', 9, 'A '
union all select '丁 ', 11, 'A '
union all select '甲 ', 12, 'B '
union all select '乙 ', 10, 'B '
union all select '丙 ', 9, 'B '
union all select '丁 ', 8, 'B '
declare @sql varchar(4000)
set @sql = 'select 小组 as ' + '小组 '
select @sql = @sql + ' , sum(case 类型 when ' ' '+类型+ ' ' ' then 数量 end) [ '+类型+ '] '
from (select distinct 类型 from a) as a
set @sql = @sql + ' from a group by 小组 '
exec(@sql)
drop table a
result:
小组 丙 丁 甲 乙
---------- ----------- ----------- ----------- -----------
A 9 11 10 8
B 9 8 12 10
[解决办法]
好,学习了。
我补充一下,
select 小组,
甲=sum(case when 类型= '甲 ' then 数量 else 0 end),
乙=sum(case when 类型= '乙 ' then 数量 else 0 end),
丙=sum(case when 类型= '丙 ' then 数量 else 0 end),
丁=sum(case when 类型= '丁 ' then 数量 else 0 end)
from tbName
group by 小组

热点排行