我的要求是双重的,我有一个巨大的源表,其中包含数百万条记录,并且需要在截止日期之后获取记录,然后查找是否有任何相关记录具有相同的元数据并将其分组。相关记录可以从截止日期之前的日期开始。 原始表格
Account Company Ledger P01 P02---P12 Date
1000 XYZ XX 0 20 07/06/18
2000 XYZ XX 0 30 07/06/18
1000 XYZ XX 10 30 07/01/18
3000 XYZ XX 20 50 06/05/18
Cutoff date = 07/05/18
Expected result
Account Company Ledger P01 P02---P12
1000 XYZ XX 10 50
2000 XYZ XX 0 30
答案 0 :(得分:1)
我尚未在iSeries上对其进行测试
SELECT account, company, ledger, sum(P01) , sum(P02)
from test_1 t
where exists (select 1 from test_1
where date1 > '2018-07-05'
and account = t.account
and company = t.company
and ledger = t.ledger)
group by account, company, ledger
由于稀疏描述,分组列或列名可能需要调整
答案 1 :(得分:0)
我认为以下内容可以满足您的需求。
with MYDATA as (
select distinct Account as Act, Company as Cmp, Ledger as Ldgr
from MYTABLE
where Date > Cutoff_Date)
select Account, Company, Ledger, sum(P01), sum(P02)
from MYTABLE join MYDATA
on Account = Act and Company = Cmp and Ledger = Ldgr
group by Account, Company, Ledger