大家帮我来看看,要快,好像今天有专家坐诊
channelno channelname mediatype adcount mediatypecount
320000107 电视1 1 114 3030
520000106 电视2 1 107 3030
420000107 电视3 1 104 3030
420000105 电视4 1 99 3030
430000014 报纸1 2 34 884
370000017 报纸2 2 28 884
510000019 报纸3 2 27 884
120000011 报纸4 2 26 884
370000107 广播1 0 146 607
370000101 广播2 0 67 607
150000107 广播3 0 56 607
610000101 广播4 0 39 607
现通过分组把数据汇总到这种程度,问题是我要通过mediatypecount把mediatype排序,通过adcount把channelname排序,然后列转行
得到这种效果
------解决方案--------------------select mediatype,
channelno=min(case when no=1 then channelno end),
channelname=min(case when no=1 then channelname end),
channelno=min(case when no=2 then channelno end),
channelname=min(case when no=2 then channelname end),
channelno=min(case when no=3 then channelno end),
channelname=min(case when no=3 then channelname end),
channelno=min(case when no=4 then channelno end),
channelname=min(case when no=4 then channelname end)
from (select *, no=row_number() over(parttion by mediatype order by channelname) from tb) a group by mediatype order by min(mediatypecount)
------解决方案--------------------select *, no=(select count(*) from tb where mediatype=a.mediatype and channelname<=a.channelname) into #tb
from tb a
select mediatype,
channelno=min(case when no=1 then channelno end),
channelname=min(case when no=1 then channelname end),
channelno=min(case when no=2 then channelno end),
channelname=min(case when no=2 then channelname end),
channelno=min(case when no=3 then channelno end),
channelname=min(case when no=3 then channelname end),
channelno=min(case when no=4 then channelno