一个使用统计语句select sum(case when ... then ... end) ...(转)
一个统计需求,需要统计产品表中下线产品数量、上线产品数量、热销产品数量、折扣产品数量。要求用一个SQL语句实现!
想到用sum(case when ...then ... end), 如下所示:
Select Sum(case when IsOnline = 1 then 1 else 0 end) as OnlineProducts,
Sum(case when IsOnline = 0 then 1 else 0 end) as OfflineProducts,
Sum(case when IsHot = 1 then 1 else 0 end) as HotProducts,
Sum(case when IsDiscount = 1 then 1 else 0 end) as DiscountProducts
From Tbl_Product
还有更好的办法吗?.....