简单判断:
要判断值的列写在case后面,when后面写值,相等的情况执行then后的语句,可以写多个when判断语句,都不满足返回else后的值,要以end结尾,end后面可以给该列指定列名。
CASE [col_name]
WHEN [value1] THEN [result1]
WHEN [value1] THEN [result2]
....
ELSE [default]
END [new_col_name]
表达式判断:
case开头,when后面写表达式,表达式为真则执行then后的语句,可以写多个when判断表达式,都不满足的情况下返回else后的值,以end结尾,end后可以为该列指定列名。
CASE
WHEN [expr] THEN [result1]
WHEN [expr] THEN [result2]
....
ELSE [default]
END [new_col_name]
SELECTCOUNT(IF(r.channel_type=1,1,0)) AS wangzhan,
COUNT(IF(r.channel_type=2,1,0)) baozhi,
COUNT(IF(r.channel_type=3,1,0)) zazhi,
SUM(CASE WHEN r.channel_type=1 AND r.check_status=6 THEN 1 ELSE 0 END) AS wangzhanluyong,
(CASE WHEN r.channel_type=1 THEN sum(r.fraction) ELSE 0 END) AS wangzhanfenshu,
sum(CASE WHEN r.channel_type=2 AND r.check_status=6 THEN 1 ELSE 0 END) AS baozhiluyong,
(CASE WHEN r.channel_type=2 THEN sum(r.fraction) ELSE 0 END) AS baozhifenshu,
sum(CASE WHEN r.channel_type=3 AND r.check_status=6 THEN 1 ELSE 0 END) AS zazhiluyong,(
CASE WHEN r.channel_type=3 THEN sum(r.fraction) ELSE 0 END) AS zazhifenshu
FROM
tougao_record r LEFT JOIN tougao t
ON r.tougao_id = t.id
WHERE
r.accept_company_id=100 AND t.create_at>='%2014-7-1%'
GROUP BY
r.channel_type
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)