首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

case when then的有关问题

2012-01-08 
case when then的问题?sum(caseparentIdwhen0then1else0end)sum(casewhenparentId0then0else1end)这两句

case when then的问题?
sum(case   parentId   when   0   then   1   else   0   end)
sum(case   when   parentId=0   then   0   else   1   end)
这两句是啥意思?有什么不同?

[解决办法]
sum(case parentId when 0 then 1 else 0 end)
统计 parentId=0 的记录条数

sum(case when parentId=0 then 0 else 1 end)
统计 parentId <> 0 的记录条数

[解决办法]
lixk2000() ( ) 信誉:100 Blog 加为好友 2007-06-07 08:43:43 得分: 0


每一个是:
计算parentId=0的记录条数
每二个是:
计算parentId <> 0的记录条数

为什么是这个结果?


-----------
上面不是有解釋嗎?


sum(case parentId when 0 then 1 else 0 end)

逐條對比表中每條紀錄,如果parentId=0的话,就加上1 如果parentId <> 0 ,就加上0,最後的到的統計結果就是“计算parentId=0的记录条数”。

热点排行