create table a (d char(8),
sacount int,
pacount int,
gacount int
)
insert into a values('04-12-6 ',100,50,50)
insert into a values('04-12-8 ',100,70,130)
insert into a values('04-12-9 ',98,28,70)
insert into a values('04-12-12',110,60,50)
declare @s1 varchar(2000),@s2 varchar(2000),@s3 varchar(2000),@s4 varchar(2000),@s varchar(8000)
set @s1='select ''sacount'''
select @s1=@s1+ ',['+rtrim(d)+']=max(case d when''' + rtrim(d) + ''' then sacount else null end)' from (select distinct d from a )b
set @s1= @s1 + ' from
(select d,sum(sacount) as sacount from a
group by d)c'
set @s2='select ''pacount'''
select @s2=@s2+ ',['+rtrim(d)+']=max(case d when''' + rtrim(d) + ''' then pacount else null end)' from (select distinct d from a )b
set @s2= @s2 + ' from
(select d,sum(pacount) as pacount from a
group by d)c'
set @s3='select ''gacount'''
select @s3=@s3+ ',['+rtrim(d)+']=max(case d when''' + rtrim(d) + ''' then gacount else null end)' from (select distinct d from a )b
set @s3= @s3 + ' from
(select d,sum(gacount) as gacount from a
group by d)c'
exec(@s1+ ' union all '+ @s2 + ' union all '+ @s3)
SQL(行列互換)
有乙個sql題在面試中出現的概率極高,最近有學生出去面試仍然會遇到這樣的題目,在這裡跟大家分享一下。題目 資料庫中有一張如下所示的表,表名為sales。年 季度銷售量 1991111 1991212 1991313 1991414 1992121 1992222 1992323 1992424 要求...
SQL行列互換
有乙個sql題在面試中出現的概率極高,在這裡跟大家分享一下。題目 資料庫中有一張如下所示的表,表名為sales。年 季度銷售量 1991111 1991212 1991313 1991414 1992121 1992222 1992323 1992424 要求 寫乙個sql語句查詢出如下所示的結果。...
SQL行列互換
題目 資料庫中有一張如下所示的表,表名為sales。年 季度銷售量 1991111 1991212 1991313 1991414 1992121 1992222 1992323 1992424 要求 寫乙個sql語句查詢出如下所示的結果。年 一季度二季度 三季度四季度 1991 1112 1314...