zoukankan      html  css  js  c++  java
  • 行转列求和:不加 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
  • 相关阅读:
    蜗牛讲-Fabric入门之架构
    No module named flask 错误解决
    adb测试Doze和App Standby模式
    以太坊挖矿原理
    mac上 go-delve 安装出现The specified item could not be found in the keychain 解决方法
    nginx+lua 根据指定路径反向代理
    asp.net 网站监控方案
    go开源项目influxdb-relay源码分析(一)
    碰到的jpython用ssh连接机器,有些命令无法运行
    git常用命令(备忘)
  • 原文地址:https://www.cnblogs.com/crsn/p/4835900.html
Copyright © 2011-2022 走看看