我有一个表“ ORDERHIST”,其中包含带有客户ID,购买日期,收到付款日期,不同交易类型和发票金额的交易数据。我正在尝试创建一个表,该表显示每个工作日所有未清发票金额的总和。我只希望查询中包含类型代码为SERV或CONS的事务。
数据样本:
CliendID PurchaseDate PaymentDate TypeCode InvoiceAmt
ID1 1/3/2019 1/4/2019 SERV 430
ID2 1/3/2019 1/4/2019 PART 200
ID1 1/4/2019 1/9/2019 SERV 480
ID3 1/7/2019 1/8/2019 CONS 140
ID2 1/7/2019 1/9/2019 CONS 180
ID3 1/8/2019 1/9/2019 SERV 250
ID1 1/9/2019 1/10/2019 CONS 180
基本上,如果(PurchaseDate <=日期)和(PaymentDate> =日期)和(TypeCode = SERV或TypeCode = CONS),则逻辑应为求和。
预期结果:
Date OutstandingBal
1/3/2019 430
1/4/2019 910
1/7/2019 800
1/8/2019 1050
1/9/2019 1090
生成的表格日期可以是每天或只有交易日,以较容易的日期为准。
答案 0 :(得分:0)
OutstandingBal = sum(iif((购买日期<=日期)和(付款日期> =日期)和(TypeCode = SERV或TypeCode = CONS),invoiceAmt,0)
答案 1 :(得分:0)
这很棘手。您需要从日期列表开始。然后,我将在MS Access中进行相关子查询:
select d.dte,
(select sum(oh2.invoiceamt)
from orderhist as oh2
where oh2.PurchaseDate <= d.dte and
oh2.PaymentDate >= d.dte and
oh2.TypeCode in ("SERV", "CONS")
) as outstandingbalance
from (select distinct purchasedate as dte
from orderhist
) as d
group by d.dte;