case函数妙用

case函数的妙用select person,ticket_name,sum(case  when  ticket_name is not null then 1 end) as 总票数  

,sum(case  when  ticket_price is not null then ticket_price end) as 总金额  from ticket_table group by person,ticket_name  

是从网上粘贴的,数据就不附上了

将表的数据根据person ticket_name 分组  每个person ticket_name 分到的数据在遍历

case  when  ticket_name is not null then 1 end  当数据ticket_name is not null 的时候ticket返回的是1,sum就加一;

当后面有where 语句的时候先执行where条件

where -----group by ---- select --------case

 

仅是自己现阶段的理解

原例http://blog.csdn.net/u011244173/article/details/46118367

posted @ 2018-03-06 16:05  静静的代码生活  阅读(380)  评论(0编辑  收藏  举报