请教一个SQLSERVER中一列数据根据条件拆分为多列的方法 20
ID MEMO TYPE
A0001 AAA 1
A0001 BBB 1
A0001 CCC 1
A0001 DDD 2
A0001 EEE 3
A0001 FFF 3
想要的结果是
ID 说明1 说明2 说明3
A0001 AAA DDD EEE
A0001 BBB FFF
A0001 CCC 展开
有点多,你可能不想看,但是你自己整理一下 弄视图也可以
SELECT PEAppM0b02,COUNT([COUNT])[COUNT],CourseName,COUNT(A)A,COUNT(B)B,COUNT(C)C,COUNT(D)D FROM (
SELECT PEAppM0b02,COUNT([COUNT])[COUNT],CourseName,
CASE PEAppM0b162
WHEN '一等奖' THEN PEAppM0b162 END A,
CASE PEAppM0b162
WHEN '二等奖' THEN PEAppM0b162 END B,
CASE PEAppM0b162
WHEN '三等奖' THEN PEAppM0b162 END C,
CASE ISNULL(PEAppM0b162,'')
WHEN '无奖项' THEN PEAppM0b162 END D
FROM
(
SELECT * FROM
(SELECT PEAppM0b02,COUNT(PEAppM0b162)[COUNT],CourseName,PEAppM0b162
FROM PEAppM002Paper where PEAppM0b162='一等奖' GROUP BY PEAppM0b02,CourseName,PEAppM0b162) A
UNION
(SELECT PEAppM0b02,COUNT(PEAppM0b162)[COUNT],CourseName,PEAppM0b162
FROM PEAppM002Paper where PEAppM0b162='二等奖' GROUP BY PEAppM0b02,CourseName,PEAppM0b162)
UNION
(SELECT PEAppM0b02,COUNT(PEAppM0b162)[COUNT],CourseName,PEAppM0b162
FROM PEAppM002Paper where PEAppM0b162='三等奖' GROUP BY PEAppM0b02,CourseName,PEAppM0b162)
UNION
(SELECT PEAppM0b02,COUNT(PEAppM0b162)[COUNT],CourseName,PEAppM0b162
FROM PEAppM002Paper where (PEAppM0b162='无奖项' OR ISNULL(PEAppM0b162,'')='无奖项') GROUP BY PEAppM0b02,CourseName,PEAppM0b162)
)TB
GROUP BY PEAppM0b02,CourseName,PEAppM0b162
)AS TAB
WHERE PEAppM0b02='164'
GROUP BY PEAppM0b02,CourseName
原格式:
修改结果后
--学科----总人数----一等奖人数---二等奖人数-----三等奖人数----无奖项人数-----
2015-04-02
1、
select
max(case when F1%3=1 then F1 else 0 end) a,
max(case when F1%3=2 then F1 else 0 end) b,
max(case when F1%3=0 then F1 else 0 end) c
from HLR151
group by (F1-1)/3
效果:
2、
select
c1=a.F1,c2=b.F1,c3=c.F1
from HLR151 a
left join HLR151 b on b.F1=a.F1+1
left join HLR151 c on c.F1=a.F1+2
where (a.F1-1)%3=0
效果:
3、
select
max(case when (F1-1)/8=0 then F1 else 0 end) a,
max(case when (F1-1)/8=1 then F1 else 0 end) b,
max(case when (F1-1)/8=2 then F1 else 0 end) c
from HLR151
group by (F1-1)%8
你这个我刚在网站上都查到过,这个是通过数字计算的,你复制到这来有用吗?
广告 您可能关注的内容 |