当要显示的列的筛选条件不同时,怎么写sql
作者:互联网
提示:使用case when
例:
SELECT MIN(province_id) provinceNumber, MIN(province_name) provinceName, COUNT(project_id) a, SUM(this_issue_money) b, SUM(CASE WHEN is_not_exit = true THEN 1 END) c, SUM(this_issue_money) d, SUM(CASE WHEN is_not_exit = true AND enterprise_type_id = 1 THEN 1 END) e, SUM(CASE WHEN enterprise_type_id = 1 THEN this_issue_money END) f, SUM(CASE WHEN is_not_exit = true AND enterprise_type_id = 2 THEN 1 END) g, SUM(CASE WHEN enterprise_type_id = 2 THEN this_issue_money END) h, SUM(CASE WHEN is_not_exit = true AND enterprise_type_id = 2 THEN 1 END) i, SUM(CASE WHEN enterprise_type_id = 2 THEN this_issue_money END) j FROM k
标签:CASE,END,SUM,WHEN,筛选,sql,当要,type,id 来源: https://www.cnblogs.com/woyujiezhen/p/13698947.html