【问题标题】:sql sum case not summarisingsql sum case 不汇总
【发布时间】:2016-12-09 09:47:48
【问题描述】:

我正在尝试编写一个脚本来总结每个期间每个代码的总量,不包括某些批次类型。

脚本运行但不汇总每个代码的数据。我做错了什么?

如果有更好的脚本编写方法,也欢迎反馈。

谢谢

SELECT 
(CASE
    when DET_ledger = 'NL' and det_nominaldr = ''  then det_nominalcr
    when DET_ledger = 'NL' and det_nominalcr = ''  then det_nominaldr
    else  det_analysis
    end ) as Code,
DET_YEAR,
DET_PERIODNUMBR,
sum(CASE
    when DET_ledger = 'NL' and det_nominaldr = ''  then -1* det_nett
    when DET_ledger = 'PL' and det_type = 'CRN'   then -1* det_nett 
        else  det_nett
    end) as Net
FROM SL_PL_NL_DETAIL
where det_year = 'C'
and det_periodnumbr = '07'
and (det_nominalcr like '617%' or det_nominaldr like '617%')
and det_primary not in 
    (select det_primary
     from sl_pl_nl_detail
     where det_ledger in ('PL','SL')
     and det_batch_flag = 1)
group by det_nominaldr, det_nominalcr, det_nett, det_ledger, det_year, det_periodnumbr, det_analysis, det_type

【问题讨论】:

  • 如果你只做SELECT *并跳过GROUP BY,结果看起来不错吗?
  • 排除总和返回正确的数据。但是,我需要汇总数据,因为数据库包含今年近 100 万笔交易。

标签: sql sum case


【解决方案1】:

我认为您不需要 Group by 子句中的 det_nett 列试试这个

     SELECT 
(CASE
    when DET_ledger = 'NL' and det_nominaldr = ''  then det_nominalcr
    when DET_ledger = 'NL' and det_nominalcr = ''  then det_nominaldr
    else  det_analysis
    end ) as Code,
DET_YEAR,
DET_PERIODNUMBR,
sum(CASE
    when DET_ledger = 'NL' and det_nominaldr = ''  then -1* det_nett
    when DET_ledger = 'PL' and det_type = 'CRN'   then -1* det_nett 
        else  det_nett
    end) as Net
FROM SL_PL_NL_DETAIL
where det_year = 'C'
and det_periodnumbr = '07'
and (det_nominalcr like '617%' or det_nominaldr like '617%')
and det_primary not in 
    (select det_primary
     from sl_pl_nl_detail
     where det_ledger in ('PL','SL')
     and det_batch_flag = 1)
group by det_nominaldr, det_nominalcr,  det_ledger, det_year, 
      det_periodnumbr, det_analysis, det_type

【讨论】:

  • 嗨。可悲的是,删除 det_nett 意味着并非所有数据都包含在结果总和中。
  • 但是您正在对列求和并将其包含在 group by 中,这似乎不正确
【解决方案2】:

如果这是 Oracle,因为你没有指定你的 DBMS,试试这个(在 oracle 中你可以很容易地在 group by 中提到这个案例,我不确定其他数据库):

SELECT 
(CASE
    when DET_ledger = 'NL' and det_nominaldr = ''  then det_nominalcr
    when DET_ledger = 'NL' and det_nominalcr = ''  then det_nominaldr
    else  det_analysis
    end ) as Code,
DET_YEAR,
DET_PERIODNUMBR,
sum(CASE
    when DET_ledger = 'NL' and det_nominaldr = ''  then -1* det_nett
    when DET_ledger = 'PL' and det_type = 'CRN'   then -1* det_nett 
        else  det_nett
    end) as Net
FROM SL_PL_NL_DETAIL
where det_year = 'C'
and det_periodnumbr = '07'
and (det_nominalcr like '617%' or det_nominaldr like '617%')
and det_primary not in 
    (select det_primary
     from sl_pl_nl_detail
     where det_ledger in ('PL','SL')
     and det_batch_flag = 1)
group by CASE  when DET_ledger = 'NL' and det_nominaldr = ''  then det_nominalcr  
               when DET_ledger = 'NL' and det_nominalcr = ''  then det_nominaldr
               else  det_analysis  end,
         DET_YEAR,
         DET_PERIODNUMBR

【讨论】:

  • 抱歉 - 这是使用 Access Dimensions 数据库
  • ANSI SQL 不允许 GROUP BY 中的 case 表达式,只允许列。解决方案是使用派生表(请参阅我的答案。)
【解决方案3】:

通过将原始查询包装在派生表中来节省一些输入,GROUP BY 其结果:

select Code, DET_YEAR, DET_PERIODNUMBR, SUM(Net)
FROM
(
    SELECT (CASE
            when DET_ledger = 'NL' and det_nominaldr = ''  then det_nominalcr
            when DET_ledger = 'NL' and det_nominalcr = ''  then det_nominaldr
            else  det_analysis
            end) as Code,
           DET_YEAR,
           DET_PERIODNUMBR,
           (CASE
            when DET_ledger = 'NL' and det_nominaldr = ''  then -1* det_nett
            when DET_ledger = 'PL' and det_type = 'CRN'   then -1* det_nett 
            else  det_nett
            end) as Net
    FROM SL_PL_NL_DETAIL
    where det_year = 'C'
      and det_periodnumbr = '07'
      and (det_nominalcr like '617%' or det_nominaldr like '617%')
      and det_primary not in 
        (select det_primary
         from sl_pl_nl_detail
         where det_ledger in ('PL','SL')
         and det_batch_flag = 1)
) dt
group by Code, DET_YEAR, DET_PERIODNUMBR

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 2021-11-26
    • 2022-11-23
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多