您的位置:首页 > 其它

Case when ……… then………else ……end

2010-09-14 11:07 106 查看
sql范例:

select Email,count(Guid)as TotalOrderCount,sum(case when  PromotionType=8 then 1 else 0 end)as OriginalCount,
sum( SaleTotalPrice )as SumTotalPrice,sum(case when PromotionType=8 then SaleTotalPrice else 0 end)as SumOriginalPrice
into #t from #torder
group by Email order by Email


CASE的多条件应用

select email,(CASE WHEN createmonth=1 THEN case when promotion=1 then 1 else 0 end ELSE 0 END) as ontad,
(case when createmonth=1 then case when promotion=1 then 1 else 0 end else 0 end )as onefeiad,
(case when createmonth=2 then case when promotion=1 then 1 else 0 end else 0 end )as twoad,
(case when createmonth=2 then case when promotion=1 then 1 else 0 end else 0 end )as twofeiad
from #o_one

select email,sum(CASE WHEN createmonth=1 and promotion=1 then 1 ELSE 0 END) as ontad
sum(case when createmonth=1 and promotion=1 then 1 else 0 end  )as onefeiad,
sum(case when createmonth=2 and promotion=1 then 1 else 0 end  )as twoad,
sum(case when createmonth=2 and promotion=1 then 1 else 0 end  )as twofeiad from #o_one
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: