表如下
地区 月份 产品1销售额 产品2销售额
上海 1 100 200
上海 2 300 400
上海 12 500 600
北京 1 100 200
北京 2 300 400
。
北京 12 500 600希望查询得到如下结果
地区 产品1销售额(一月份) 产品2销售额(一月份) 产品1销售额(二月份) 产品2销售额(二月份)。。产品1销售额(十二月份) 产品2销售额(十二月份)
上海 100 200 300 400。500 600
北京 100 200 300 400。500 600实在想不出来该如何做,要不然也不会麻烦大家,请大家帮帮忙,不胜感激
地区 月份 产品1销售额 产品2销售额
上海 1 100 200
上海 2 300 400
上海 12 500 600
北京 1 100 200
北京 2 300 400
。
北京 12 500 600希望查询得到如下结果
地区 产品1销售额(一月份) 产品2销售额(一月份) 产品1销售额(二月份) 产品2销售额(二月份)。。产品1销售额(十二月份) 产品2销售额(十二月份)
上海 100 200 300 400。500 600
北京 100 200 300 400。500 600实在想不出来该如何做,要不然也不会麻烦大家,请大家帮帮忙,不胜感激
select @sql='select 地区'
select @sql=@sql+',[产品1销售额'+rtrim(月份)+']=sum(case 月份 when '+rtrim(月份)+' then 产品1销售额 end),[产品2销售额'+rtrim(月份)+']=sum(case 月份 when '+rtrim(月份)+' then 产品2销售额 end)' from t
exec(@sql+' from t group by 地区')
----------------------------------------------------------------------------------
create table a(StudentId int,StudnetName varchar(10))
insert into a select 1,'张三'
union all select 2,'李四'create table b(Id int,colName varchar(10))
insert into b select 1,'shuXue'
union all select 2,'yuWen'
union all select 3,'yingYu'create table d(StudentId int,colId int,chengJi int)
insert into d select 1 ,1 ,90
union all select 1 ,2 ,90
union all select 1 ,3 ,95
union all select 2 ,1 ,100
union all select 2 ,2 ,90
union all select 2 ,3 ,80
GOcreate view aa
as
select d.StudentId,StudnetName,colName,chengJi
from
a,b,d
where
a.StudentId=d.StudentId and
b.Id=d.colId
GOdeclare @sql varchar(8000)
set @sql='select StudentId,StudnetName'
select @sql=@sql+',['+colName+']=max(case colName when '''+colName+''' then chengJi else 0 end)' from aa group by colName
/*
将此句按程序方式来理解,事实上是一个循环赋值语句:
@sql=@sql+动态取得的值
差别仅在于SELECT不需要加上循环条件
此句需要用SELECT才可以从表中提取,用SET赋值不行
*/
select @sql
exec(@sql+' from aa group by StudentId,StudnetName')
Godrop view aa
drop table a,b,d
sum(case 月份 when 1 then [产品1销售额] else 0 end) as [产品1销售额(一月份)],
sum(case 月份 when 1 then [产品2销售额] else 0 end) as [产品2销售额(一月份)],
sum(case 月份 when 2 then [产品1销售额] else 0 end) as [产品1销售额(二月份)],
sum(case 月份 when 2 then [产品2销售额] else 0 end) as [产品2销售额(二月份)],
...
from 表
group by 地区
insert t select '上海', 1, 100, 200
insert t select '上海', 2, 300, 400
insert t select '上海', 12, 500, 600 declare @sql varchar(8000)
select @sql='select 地区'
select @sql=@sql+',[产品1销售额'+rtrim(月份)+'月]=sum(case 月份 when '+rtrim(月份)+' then 产品1销售额 end),[产品2销售额'+rtrim(月份)+'月]=sum(case 月份 when '+rtrim(月份)+' then 产品2销售额 end)' from t
exec(@sql+' from t group by 地区')
Select 地区,
[产品1销售额(一月份)]=sum(case when 月份=1 then 产品1销售额 else 0 end),
[产品2销售额(一月份)]=sum(case when 月份=1 then 产品2销售额 else 0 end),
[产品1销售额(二月份)]=sum(case when 月份=2 then 产品1销售额 else 0 end),
[产品2销售额(二月份)]=sum(case when 月份=2 then 产品2销售额 else 0 end),
.........
from 表
group by 地区
select @sql='select 地区'
select @sql=@sql+',[产品1销售额'+rtrim(月份)+'月]=sum(case 月份 when '+rtrim(月份)+' then 产品1销售额 end),[产品2销售额'+rtrim(月份)+'月]=sum(case 月份 when '+rtrim(月份)+' then 产品2销售额 end)' from 你的表名
exec(@sql+' from 你的表名 group by 地区')
( 地区 varchar(100),
月份 int,
产品1销售额 int,
产品2销售额 int
)insert into 表 select '上海',1,100,200
insert into 表 select '上海',2,100,200
insert into 表 select '上海',3,100,200
insert into 表 select '上海',4,100,200
insert into 表 select '上海',5,100,200
insert into 表 select '上海',6,100,200
insert into 表 select '上海',7,100,200
insert into 表 select '上海',8,100,200
insert into 表 select '上海',9,100,200
insert into 表 select '上海',10,100,200
insert into 表 select '上海',11,100,200
insert into 表 select '上海',12,100,200insert into 表 select '北京',1,100,200
insert into 表 select '北京',2,100,200
insert into 表 select '北京',3,100,200
insert into 表 select '北京',4,100,200
insert into 表 select '北京',5,100,200
insert into 表 select '北京',6,100,200
insert into 表 select '北京',7,100,200
insert into 表 select '北京',8,100,200
insert into 表 select '北京',9,100,200
insert into 表 select '北京',10,100,200
insert into 表 select '北京',11,100,200
insert into 表 select '北京',12,100,200declare @t varchar(8000)
set @t=''
select @t=@t+'sum(case when 月份=' +a.月份+' then 产品1销售额 else 0 end) [产品1销售额' +a.月份+ '月份], sum(case when 月份=' +a.月份+' then 产品2销售额 else 0 end) [产品2销售额' +a.月份+ '月份],'
from (select cast(月份 as varchar(5)) 月份 from 表 group by 月份 ) a
select @t='select 地区, ' + left(@t,len(@t)-1) + ' from 表 group by 地区'exec(@t)drop table 表
insert into tb select '上海',1,100,200
union all select '上海',2,300,400
union all select '上海',12,500,600
union all select '北京',1,100,200
union all select '北京',2,300,400
union all select '北京',12,500,600select 地区,
[产品1销售额(一月份)]=sum(case 月份 when '1' then 产品1销售额 else 0 end),
[产品2销售额(一月份)]=sum(case 月份 when '1' then 产品2销售额 else 0 end),
[产品1销售额(二月份)]=sum(case 月份 when '2' then 产品1销售额 else 0 end),
[产品2销售额(二月份)]=sum(case 月份 when '2' then 产品2销售额 else 0 end),
[产品1销售额(三月份)]=sum(case 月份 when '3' then 产品1销售额 else 0 end),
[产品2销售额(三月份)]=sum(case 月份 when '3' then 产品2销售额 else 0 end),
[产品1销售额(四月份)]=sum(case 月份 when '4' then 产品1销售额 else 0 end),
[产品2销售额(四月份)]=sum(case 月份 when '4' then 产品2销售额 else 0 end),
[产品1销售额(五月份)]=sum(case 月份 when '5' then 产品1销售额 else 0 end),
[产品2销售额(五月份)]=sum(case 月份 when '5' then 产品2销售额 else 0 end),
[产品1销售额(六月份)]=sum(case 月份 when '6' then 产品1销售额 else 0 end),
[产品2销售额(六月份)]=sum(case 月份 when '6' then 产品2销售额 else 0 end),
[产品1销售额(七月份)]=sum(case 月份 when '7' then 产品1销售额 else 0 end),
[产品2销售额(七月份)]=sum(case 月份 when '7' then 产品2销售额 else 0 end),
[产品1销售额(八月份)]=sum(case 月份 when '8' then 产品1销售额 else 0 end),
[产品2销售额(八月份)]=sum(case 月份 when '8' then 产品2销售额 else 0 end),
[产品1销售额(九月份)]=sum(case 月份 when '9' then 产品1销售额 else 0 end),
[产品2销售额(九月份)]=sum(case 月份 when '9' then 产品2销售额 else 0 end),
[产品1销售额(十月份)]=sum(case 月份 when '10' then 产品1销售额 else 0 end),
[产品2销售额(十月份)]=sum(case 月份 when '10' then 产品2销售额 else 0 end),
[产品1销售额(十一月份)]=sum(case 月份 when '11' then 产品1销售额 else 0 end),
[产品2销售额(十一月份)]=sum(case 月份 when '11' then 产品2销售额 else 0 end),
[产品1销售额(十二月份)]=sum(case 月份 when '12' then 产品1销售额 else 0 end),
[产品2销售额(十二月份)]=sum(case 月份 when '12' then 产品2销售额 else 0 end)
from tb
group by 地区drop table tb
地区 varchar(10),
月份 int,
产品1销售额 int,
产品2销售额 int
)
insert tabe select '上海',1,100,200
union all select '上海',2,300,400
union all select '上海',12,300,400
union all select '北京',1,100,200
union all select '北京',2,300,400
union all select '北京',12,500,600
select 地区,sum(case 月份 when 1 then 产品1销售额 else 0 end )as 产品1销售额一月份, sum(case 月份 when 1 then 产品2销售额 else 0 end )as 产品2销售额一月份,
sum(case 月份 when 2 then 产品1销售额 else 0 end )as 产品1销售额一月份, sum(case 月份 when 2 then 产品2销售额 else 0 end )as 产品2销售额一月份,
sum(case 月份 when 12 then 产品1销售额 else 0 end )as 产品1销售额一月份, sum(case 月份 when 12 then 产品2销售额 else 0 end )as 产品2销售额一月份
from tabe
group by 地区
---------------------------------------------偶很弱,动态写会写,速度比较慢最多当鬣狗了:)