_ | 覦覈襦 | 豕蠏手 | 殊螳 | 譯殊碁
FrontPage › 螻/螻襷り鍵

create table #test(codel char(4),codes char(3), varchar(4),螳蟆 int)

insert #test values('0001','a01','1',100)
insert #test values('0001','a01','2',100)
insert #test values('0001','a02','2',200)
insert #test values('0001','a03','3',300)
insert #test values('0002','a04','1',300)
insert #test values('0002','a05','2',200)
insert #test values('0003','a06','1',100)
insert #test values('0003','a06','3',500)
insert #test values('0003','a07','3',700)


select
	isnull(code, '螻') code
,	isnull(convert(varchar(4), mon), '螻') code
,	[1]
,	[2]
,	[3]
from(
	select
		case when b.seq in (2, 3) then code end code
	,	case when b.seq = 2 then mon end mon
	,	sum([1]) [1]
	,	sum([2]) [2]
	,	sum([3]) [3]
	from (
		select codel code,
			   codes 'mon',
			   sum(case  when '1' then 螳蟆 else 0 end) [1],
			   sum(case  when '2' then 螳蟆 else 0 end) [2],
			   sum(case  when '3' then 螳蟆 else 0 end) [3]
		  from #test
		 group by codel,codes) a 
			cross join (select 1 seq union all select 2 union all select 3) b
	group by 
		case when b.seq in (2, 3) then code end
	,	case when b.seq = 2 then mon end
) t
order by
	case when code is null then '9999' else code end
,	case when mon is null then 'zzz' else mon end
/*
code code          1          2          3
---- ---- ----------- ----------- -----------
0001 A01          100         100           0
0001 A02            0         200           0
0001 A03            0           0         300
0001 螻           100         300         300
0002 A04          300           0           0
0002 A05            0         200           0
0002 螻           300         200           0
0003 A06          100           0         500
0003 A07            0           0         700
0003 螻           100           0        1200
螻   螻           500         500        1500
*/
蠍 蠍郁鍵..
企: : るジ讓曙 襦螻豺 企Ν 譯殊語. 襦螻豺
EditText : Print : Mobile : FindPage : DeletePage : LikePages : Powered by MoniWiki : Last modified 2018-04-13 23:12:52

朱 煙隈蠍 蠍 る 襯 至鍵 覈襯 覿磯 企. <譴蟲 >