【发布时间】: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 万笔交易。