Facebook Sharer
选择您要替换的背景颜色:
【农历新年】背景图片:
个性化设定
 注册  找回密码
查看: 982|回复: 0
打印 上一主题 下一主题

帮帮忙!!MSSQL pivot( )

[复制链接]

2

主题

0

好友

17

积分

初级会员

Rank: 1

跳转到指定楼层
1#
发表于 2012-7-31 10:47 AM |只看该作者 |倒序浏览
result:

castid    S1   S2     S3   S4  S5   S6
------        --      --     --      --      --    --
13700        1        1        1        1        1        1
13701        1        1        1        1        1        1
13702        1        1        1        1        1        1
13703        1        1        1        1        1        1
13704        1        1        1        1        1        1
13705        1        1        1        1        1        1
13706        1        1        1        1        1        1
13707        1        1        1        0        1        1
13708        1        0        1        0        1        1

syntax :
------------
select c.castid,
sum(CASE WHEN strandid = 1 AND s.castend - s.caststart >0 THEN 1 ELSE 0 END) as S1,
sum(CASE WHEN strandid = 2 AND s.castend - s.caststart >0 THEN 1 ELSE 0 END) as S2,
sum(CASE WHEN strandid = 3 AND s.castend - s.caststart >0 THEN 1 ELSE 0 END) as S3,
sum(CASE WHEN strandid = 4 AND s.castend - s.caststart >0 THEN 1 ELSE 0 END) as S4,
sum(CASE WHEN strandid = 5 AND s.castend - s.caststart >0 THEN 1 ELSE 0 END) as S5,
sum(CASE WHEN strandid = 6 AND s.castend - s.caststart >0 THEN 1 ELSE 0 END) as S6
from tbcast c JOIN tbstrand s ON c.castid = s.castid
group by c.castid

以上是用case()来做.
如果用pivot()来做你们有谁会吗?




收藏收藏0
您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2024-10-27 04:39 AM , Processed in 0.111885 second(s), 27 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Ultra High-performance Dedicated Server powered by iCore Technology Sdn. Bhd.
Domain Registration | Web Hosting | Email Hosting | Forum Hosting | ECShop Hosting | Dedicated Server | Colocation Services
本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved
合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部