行轉列求和:不加 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
View Code

 

測試驗證:不加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
View Code
相關文章
相關標籤/搜索