34,593
社区成员
发帖
与我相关
我的任务
分享
create table tb(code1 varchar(10), code2 int, code3 varchar(10), value decimal(18,2))
insert into tb values('A09',1,'01',0.00)
insert into tb values('A09',1,'02',null)
insert into tb values('A09',1,'03',null)
insert into tb values('A09',1,'04',null)
insert into tb values('A09',1,'05',null)
insert into tb values('A09',1,'06',null)
insert into tb values('A09',1,'07',null)
insert into tb values('A09',1,'08',null)
insert into tb values('A09',1,'09',null)
insert into tb values('A09',1,'10',null)
insert into tb values('A09',1,'11',null)
insert into tb values('A09',1,'12',null)
insert into tb values('A0A',1,'01',1.9)
insert into tb values('A0A',1,'02',null)
insert into tb values('A0A',1,'03',null)
insert into tb values('A0A',1,'04',1.2)
insert into tb values('A0A',1,'05',null)
insert into tb values('A0A',1,'06',null)
insert into tb values('A0A',1,'07',null)
insert into tb values('A0A',1,'08',null)
insert into tb values('A0A',1,'09',null)
insert into tb values('A0A',1,'10',null)
insert into tb values('A0A',1,'11',null)
insert into tb values('A0A',1,'12',null)
go
--静态SQL,指CODE3的值固定为01-12
select code1,code2,
max(case code3 when '01' then code3 else '' end ) as f1,
max(case code3 when '01' then cast(value as varchar) end ) as c1,
max(case code3 when '02' then code3 else '' end ) as f2,
max(case code3 when '02' then cast(value as varchar) end ) as c2,
max(case code3 when '03' then code3 else '' end ) as f3,
max(case code3 when '03' then cast(value as varchar) end ) as c3,
max(case code3 when '04' then code3 else '' end ) as f4,
max(case code3 when '04' then cast(value as varchar) end ) as c4,
max(case code3 when '05' then code3 else '' end ) as f5,
max(case code3 when '05' then cast(value as varchar) end ) as c5,
max(case code3 when '06' then code3 else '' end ) as f6,
max(case code3 when '06' then cast(value as varchar) end ) as c6,
max(case code3 when '07' then code3 else '' end ) as f7,
max(case code3 when '07' then cast(value as varchar) end ) as c7,
max(case code3 when '08' then code3 else '' end ) as f8,
max(case code3 when '08' then cast(value as varchar) end ) as c8,
max(case code3 when '09' then code3 else '' end ) as f9,
max(case code3 when '09' then cast(value as varchar) end ) as c9,
max(case code3 when '10' then code3 else '' end ) as f10,
max(case code3 when '10' then cast(value as varchar) end ) as c10,
max(case code3 when '11' then code3 else '' end ) as f11,
max(case code3 when '11' then cast(value as varchar) end ) as c11,
max(case code3 when '12' then code3 else '' end ) as f12,
max(case code3 when '12' then cast(value as varchar) end ) as c12
from tb group by code1,code2
/*
code1 code2 f1 c1 f2 c2 f3 c3 f4 c4 f5 c5 f6 c6 f7 c7 f8 c8 f9 c9 f10 c10 f11 c11 f12 c12
---------- ----------- ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------
A09 1 01 0.00 02 NULL 03 NULL 04 NULL 05 NULL 06 NULL 07 NULL 08 NULL 09 NULL 10 NULL 11 NULL 12 NULL
A0A 1 01 1.90 02 NULL 03 NULL 04 1.20 05 NULL 06 NULL 07 NULL 08 NULL 09 NULL 10 NULL 11 NULL 12 NULL
警告: 聚合或其他 SET 操作消除了空值。
(2 行受影响)
*/
--动态SQL,指CODE3的值不固定
declare @sql varchar(8000)
set @sql = 'select code1,code2'
select @sql = @sql + ' , max(case code3 when ''' + code3 + ''' then code3 else '' '' end) [f' + code3 + ']'
+ ' , max(case code3 when ''' + code3 + ''' then cast(value as varchar) end) [f' + code3 + ']'
from (select distinct code3 from tb) as a
set @sql = @sql + ' from tb group by code1,code2 '
exec(@sql)
/*
code1 code2 f01 f01 f02 f02 f03 f03 f04 f04 f05 f05 f06 f06 f07 f07 f08 f08 f09 f09 f10 f10 f11 f11 f12 f12
---------- ----------- ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------
A09 1 01 0.00 02 NULL 03 NULL 04 NULL 05 NULL 06 NULL 07 NULL 08 NULL 09 NULL 10 NULL 11 NULL 12 NULL
A0A 1 01 1.90 02 NULL 03 NULL 04 1.20 05 NULL 06 NULL 07 NULL 08 NULL 09 NULL 10 NULL 11 NULL 12 NULL
警告: 聚合或其他 SET 操作消除了空值。
(2 行受影响)
*/
drop table tb
create table tb(code1 varchar(10), code2 int, code3 varchar(10), value decimal(18,2))
insert into tb values('A09',1,'01',0.00)
insert into tb values('A09',1,'02',null)
insert into tb values('A09',1,'03',null)
insert into tb values('A09',1,'04',null)
insert into tb values('A09',1,'05',null)
insert into tb values('A09',1,'06',null)
insert into tb values('A09',1,'07',null)
insert into tb values('A09',1,'08',null)
insert into tb values('A09',1,'09',null)
insert into tb values('A09',1,'10',null)
insert into tb values('A09',1,'11',null)
insert into tb values('A09',1,'12',null)
insert into tb values('A0A',1,'01',1.9)
insert into tb values('A0A',1,'02',null)
insert into tb values('A0A',1,'03',null)
insert into tb values('A0A',1,'04',1.2)
insert into tb values('A0A',1,'05',null)
insert into tb values('A0A',1,'06',null)
insert into tb values('A0A',1,'07',null)
insert into tb values('A0A',1,'08',null)
insert into tb values('A0A',1,'09',null)
insert into tb values('A0A',1,'10',null)
insert into tb values('A0A',1,'11',null)
insert into tb values('A0A',1,'12',null)
go
select code1,code2,
max(case code3 when '01' then code3 else '' end ) as f1,
max(case code3 when '01' then cast(value as varchar) end ) as c1,
max(case code3 when '02' then code3 else '' end ) as f2,
max(case code3 when '02' then cast(value as varchar) end ) as c2,
max(case code3 when '03' then code3 else '' end ) as f3,
max(case code3 when '03' then cast(value as varchar) end ) as c3,
max(case code3 when '04' then code3 else '' end ) as f4,
max(case code3 when '04' then cast(value as varchar) end ) as c4,
max(case code3 when '05' then code3 else '' end ) as f5,
max(case code3 when '05' then cast(value as varchar) end ) as c5,
max(case code3 when '06' then code3 else '' end ) as f6,
max(case code3 when '06' then cast(value as varchar) end ) as c6,
max(case code3 when '07' then code3 else '' end ) as f7,
max(case code3 when '07' then cast(value as varchar) end ) as c7,
max(case code3 when '08' then code3 else '' end ) as f8,
max(case code3 when '08' then cast(value as varchar) end ) as c8,
max(case code3 when '09' then code3 else '' end ) as f9,
max(case code3 when '09' then cast(value as varchar) end ) as c9,
max(case code3 when '10' then code3 else '' end ) as f10,
max(case code3 when '10' then cast(value as varchar) end ) as c10,
max(case code3 when '11' then code3 else '' end ) as f11,
max(case code3 when '11' then cast(value as varchar) end ) as c11,
max(case code3 when '12' then code3 else '' end ) as f12,
max(case code3 when '12' then cast(value as varchar) end ) as c12
from tb group by code1,code2
drop table tb
/*
code1 code2 f1 c1 f2 c2 f3 c3 f4 c4 f5 c5 f6 c6 f7 c7 f8 c8 f9 c9 f10 c10 f11 c11 f12 c12
---------- ----------- ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------
A09 1 01 0.00 02 NULL 03 NULL 04 NULL 05 NULL 06 NULL 07 NULL 08 NULL 09 NULL 10 NULL 11 NULL 12 NULL
A0A 1 01 1.90 02 NULL 03 NULL 04 1.20 05 NULL 06 NULL 07 NULL 08 NULL 09 NULL 10 NULL 11 NULL 12 NULL
警告: 聚合或其他 SET 操作消除了空值。
(2 行受影响)
*/
select code1,code2,
max(case code3 when '01' then code3 else '' end ) as f1,
max(case code3 when '01' then value end ) as c1,
max(case code3 when '02' then code3 else '' end ) as f2,
max(case code3 when '02' then value end ) as c2,
max(case code3 when '03' then code3 else '' end ) as f3,
max(case code3 when '03' then value end ) as c3,
max(case code3 when '04' then code3 else '' end ) as f4,
max(case code3 when '04' then value end ) as c4,
max(case code3 when '05' then code3 else '' end ) as f5,
max(case code3 when '05' then value end ) as c5,
max(case code3 when '06' then code3 else '' end ) as f6,
max(case code3 when '06' then value end ) as c6,
max(case code3 when '07' then code3 else '' end ) as f7,
max(case code3 when '07' then value end ) as c7,
max(case code3 when '08' then code3 else '' end ) as f8,
max(case code3 when '08' then value end ) as c8,
max(case code3 when '09' then code3 else '' end ) as f9,
max(case code3 when '09' then value end ) as c9,
max(case code3 when '10' then code3 else '' end ) as f10,
max(case code3 when '10' then value end ) as c10,
max(case code3 when '11' then code3 else '' end ) as f11,
max(case code3 when '11' then value end ) as c11,
max(case code3 when '12' then code3 else '' end ) as f12,
max(case code3 when '12' then value end ) as c12
from tab group by code1,code2
create table T(code1 nvarchar(5), code2 int, code3 nvarchar(2), value numeric(18,4) )
insert T select 'A09',1,'01',0.0000
insert T select 'A09',1,'02',null
insert T select 'A09',1,'03',null
insert T select 'A09',1,'04',null
insert T select 'A09',1,'05',null
insert T select 'A09',1,'06',null
insert T select 'A09',1,'07',null
insert T select 'A09',1,'08',null
insert T select 'A09',1,'09',null
insert T select 'A09',1,'10',null
insert T select 'A09',1,'11',null
insert T select 'A09',1,'12',null
insert T select 'A0A',1,'01',1.9
insert T select 'A0A',1,'02',null
insert T select 'A0A',1,'03',null
insert T select 'A0A',1,'04',1.2
insert T select 'A0A',1,'05',null
insert T select 'A0A',1,'06',null
insert T select 'A0A',1,'07',null
insert T select 'A0A',1,'08',null
insert T select 'A0A',1,'09',null
insert T select 'A0A',1,'10',null
insert T select 'A0A',1,'11',null
insert T select 'A0A',1,'12',null
go
declare @c3 table (code3 nvarchar(2))
insert into @c3 select distinct code3 from T order by code3
select
case when c3.code3='01' then '<tr><td>'+T.code1+'</td><td>'+cast(T.code2 as varchar)+'</td>' else '' end
+'<td>'+isnull(cast(T.value as varchar),' ')+'</td>'
+case when c3.code3='12' then '</tr>' else '' end
from @c3 c3
left join T on t.code3=c3.code3
order by code1, code2, c3.code3
go
drop table T
go
<tr><td>A09</td><td>1</td><td>0.0000</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td></tr>
<tr><td>A0A</td><td>1</td><td>1.9000</td>
<td> </td>
<td> </td>
<td>1.2000</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td></tr>
create table T(code1 nvarchar(5), code2 int, code3 nvarchar(2), value numeric(18,4) )
insert T select 'A09',1,'01',0.0000
insert T select 'A09',1,'02',null
insert T select 'A09',1,'03',null
insert T select 'A09',1,'04',null
insert T select 'A09',1,'05',null
insert T select 'A09',1,'06',null
insert T select 'A09',1,'07',null
insert T select 'A09',1,'08',null
insert T select 'A09',1,'09',null
insert T select 'A09',1,'10',null
insert T select 'A09',1,'11',null
insert T select 'A09',1,'12',null
insert T select 'A0A',1,'01',1.9
insert T select 'A0A',1,'02',null
insert T select 'A0A',1,'03',null
insert T select 'A0A',1,'04',1.2
insert T select 'A0A',1,'05',null
insert T select 'A0A',1,'06',null
insert T select 'A0A',1,'07',null
insert T select 'A0A',1,'08',null
insert T select 'A0A',1,'09',null
insert T select 'A0A',1,'10',null
insert T select 'A0A',1,'11',null
insert T select 'A0A',1,'12',null
go
declare @s nvarchar(4000),@i int
select @s='',@i=1
while @i<=12
select @s=@s+',F'+rtrim(@i)+'=max(case when code3='''+right(100+@i,2)+''' then code3 else '''' end),C'
+rtrim(@i)+'=max(case when code3='''+right(100+@i,2)+''' then value end)',@i=@i+1
--print @s--显示字符串
exec( 'select code1,code2'+@s+' from T group by code1,code2')
code1 code2 F1 C1 F2 C2 F3 C3 F4 C4 F5 C5 F6 C6 F7 C7 F8 C8 F9 C9 F10 C10 F11 C11 F12 C12
----- ----------- ---- -------------------- ---- -------------------- ---- -------------------- ---- -------------------- ---- -------------------- ---- -------------------- ---- -------------------- ---- -------------------- ---- -------------------- ---- -------------------- ---- -------------------- ---- --------------------
A09 1 01 .0000 02 NULL 03 NULL 04 NULL 05 NULL 06 NULL 07 NULL 08 NULL 09 NULL 10 NULL 11 NULL 12 NULL
A0A 1 01 1.9000 02 NULL 03 NULL 04 1.2000 05 NULL 06 NULL 07 NULL 08 NULL 09 NULL 10 NULL 11 NULL 12 NULL
警告: 聚合或其它 SET 操作消除了空值。