select s.sPeizhi,s.cRoom,count(s.cRoom) as num from tb_server s where s.sState='空闲' group by s.sPeizhi,s.cRoom
得到
sPeizhi cRoom num
E2102160 老机房 2
P4300180 老机房 2
PD3002160 老机房 3
E5202160 新机房 4
P4300180 新机房 4
世纪东方45 新机房 4
怎么变成
sPeizhi 老机房 新机房
E2102160 2 0
P4300180 2 4
PD3002160 3 0
E5202160 4 0
世纪东方45 0 4
cRoom那列可以伸展, 即又可能不只 老机房和新机房
7 个解决方案
#1
select s.sPeizhi,s.cRoom,count(s.cRoom) as num INTO #T
from tb_server s where s.sState='空闲' group by s.sPeizhi,s.cRoom
SELECT SPEIZH
,
SUM(CASE WHEN cRoom ='老机房' THEN NUM ELSE 0 END )AS '老机房',
SUM(CASE WHEN cRoom ='新机房' THEN NUM ELSE 0 END )AS '新机房'
FROM #T GROUP BY SPEIZH
#2
参考:http://topic.csdn.net/u/20090522/16/11df6ef4-3338-44a6-9156-64d4b26527ec.html
#3
#4
;with f as
(
select
s.sPeizhi,s.cRoom,count(s.cRoom) as num
from
tb_server s
where
s.sState='空闲'
group by
s.sPeizhi,s.cRoom
)
select
sPeizhi,
sum(case cRoom when '老机房' then 1 else 0 end) as 老机房,
sum(case cRoom when '新机房' then 1 else 0 end) as 新机房
from
f
group by
sPeizhi
#5
--如果不止新老机房的话可以用动态语句
declare @sql varchar(8000)
set @sql = 'select sPeizhi '
select @sql = @sql + ' , sum(case cRoom when ''' + cRoom + ''' then 1 else 0 end) [' + cRoom + ']'
from (select distinct cRoom from tb) as a
set @sql = @sql + ' from tb group by sPeizhi'
exec(@sql)
#6
#7
谢谢
小F和其他的朋友了!
结贴!
希望帮我顶一下下面那个 混淆+加壳的问题
http://topic.csdn.net/u/20091023/10/d52136e5-0857-471f-bff3-b7e06f28622f.html
结贴!
希望帮我顶一下下面那个 混淆+加壳的问题
http://topic.csdn.net/u/20091023/10/d52136e5-0857-471f-bff3-b7e06f28622f.html
#1
select s.sPeizhi,s.cRoom,count(s.cRoom) as num INTO #T
from tb_server s where s.sState='空闲' group by s.sPeizhi,s.cRoom
SELECT SPEIZH
,
SUM(CASE WHEN cRoom ='老机房' THEN NUM ELSE 0 END )AS '老机房',
SUM(CASE WHEN cRoom ='新机房' THEN NUM ELSE 0 END )AS '新机房'
FROM #T GROUP BY SPEIZH
#2
参考:http://topic.csdn.net/u/20090522/16/11df6ef4-3338-44a6-9156-64d4b26527ec.html
#3
#4
;with f as
(
select
s.sPeizhi,s.cRoom,count(s.cRoom) as num
from
tb_server s
where
s.sState='空闲'
group by
s.sPeizhi,s.cRoom
)
select
sPeizhi,
sum(case cRoom when '老机房' then 1 else 0 end) as 老机房,
sum(case cRoom when '新机房' then 1 else 0 end) as 新机房
from
f
group by
sPeizhi
#5
--如果不止新老机房的话可以用动态语句
declare @sql varchar(8000)
set @sql = 'select sPeizhi '
select @sql = @sql + ' , sum(case cRoom when ''' + cRoom + ''' then 1 else 0 end) [' + cRoom + ']'
from (select distinct cRoom from tb) as a
set @sql = @sql + ' from tb group by sPeizhi'
exec(@sql)
#6
#7
谢谢
小F和其他的朋友了!
结贴!
希望帮我顶一下下面那个 混淆+加壳的问题
http://topic.csdn.net/u/20091023/10/d52136e5-0857-471f-bff3-b7e06f28622f.html
结贴!
希望帮我顶一下下面那个 混淆+加壳的问题
http://topic.csdn.net/u/20091023/10/d52136e5-0857-471f-bff3-b7e06f28622f.html