行转列求和:不加 in 条件,sum的数据会不会准确?
我的习惯写法,担心不加 in 条件 ,统计结果会包含其他的数据
SELECT ZWKMYE_KJND as 年度,ZWKMYE_KJQJ as 月份,ZWKMYE_DWBH as 单位, sum(case ZWKMYE_KMBH when '6403' then ZWKMYE_JFFS end) 税金, sum(case ZWKMYE_KMBH when '6051' then ZWKMYE_DFFS end) 其他业务收入, sum(case ZWKMYE_KMBH when '6402' then ZWKMYE_JFFS end) 其他业务支出 , sum(case ZWKMYE_KMBH when '6611' then ZWKMYE_JFFS end) 营业费用, sum(case ZWKMYE_KMBH when '6612' then ZWKMYE_JFFS end) 管理费用, sum(case ZWKMYE_KMBH when '6613' then ZWKMYE_JFFS end) 财务费用, sum(case ZWKMYE_KMBH when '6111' then ZWKMYE_DFFS end) 投资收益, sum(case ZWKMYE_KMBH when '6301' then ZWKMYE_DFFS end) 营业外收入, sum(case ZWKMYE_KMBH when '6711' then ZWKMYE_JFFS end) 营业支出, sum(case ZWKMYE_KMBH when '1602' then 0-ZWKMYE_DQYE end) 累计折旧, sum(case ZWKMYE_KMBH when '1702' then 0-ZWKMYE_DQYE end) 累计摊销, sum(case ZWKMYE_KMBH when '61302' then ZWKMYE_JFFS end) 利息支出 from VW_GLBI_CWFX_JLR_KMYE where 1=1 and ZWKMYE_KMBH in ('6403','6051','6402','6611','6612','6613','6111','6301','6711','1602','1702','61302') and ZWKMYE_DWBH='89' and ZWKMYE_KJND='2015' group by ZWKMYE_KJND,ZWKMYE_KJQJ,ZWKMYE_DWBH
测试验证:不加in条件,统计结果正确
create table zy ( F01 varchar(4), F02 int ) insert into zy select 'A',10 union all select 'B',10 union all select 'C',10 union all select 'D',10 union all select 'E',10 select sum(case F01 when 'A' then F02 end) A, sum(case F01 when 'B' then F02 end) B from zy