检索所有相关调整详细信息的最新发票记录

发布于 2025-02-04 10:35:01 字数 526 浏览 1 评论 0原文

我正在尝试检索具有所有相关调整详细信息的最新发票记录(表可能包含重复的发票)。 不幸的是,以下SQL只能检索单个记录。 (例如,如果特定最新发票具有多个充电代码,它将仅检索1)

选择A.invoice,A.Shipment_nbr,A.Invoice_date,b.adjust_rsn,b.adjust_date,c.charge_code,c.charge_code,c.charge_amt 从开票a 内部加入调整B 在A.Shipment_nbr上= B.Shipment_nbr 内联电荷c 在b.uniq_id上= c.uniq_id 其中'05 -01-2022'和'05 -31-2022'之间的b.adjust_date 合格row_number() A.Shipment_nbr A.invoice_date desc)= 1 = 1

参考表上的图像,当前结果和预期结果。 表格,当前结果&预期结果

I'm trying to retrieve the latest invoice record (table may contains duplicate invoices) with all relevant adjustment details.
Unfortunately, the below SQL only able to retrieve a single line of record. (e.g. if the particular latest invoice has multiple charge code, it will only retrieve 1 of it)

SELECT a.Invoice, a.Shipment_Nbr, a.Invoice_date, b.Adjust_rsn, b.Adjust_date, c.Charge_code, c.Charge_Amt
FROM Invoicing a
Inner Join Adjust b
on a.Shipment_Nbr=b.Shipment_Nbr
Inner Join Charge c
on b.uniq_id=c.uniq_id
WHERE b.Adjust_date between '05-01-2022' and '05-31-2022'
QUALIFY ROW_NUMBER () OVER (PARTITION BY
a.Shipment_Nbr
ORDER BY a.Invoice_date DESC) = 1

Refer to the image on the tables, current result and the expected result.
Tables, Current Result & Expected Result

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作业与我同在 2025-02-11 10:35:01

这可能应该这样做

select
a.Invoice, b.Shipment_Nbr, a.Invoice_date, b.Adjust_rsn, b.Adjust_date, c.Charge_code, c.Charge_Amt 
FROM
(
select * from Invoicing a
qualify (row_number() over (partition by Shipment_Nbr order by Invoice_date desc)=1)
) a
inner join 
(
select * from Adjust b 
qualify (row_number() over (partition by Shipment_Nbr order by Adjust_date desc)=1)
) b on (a.Shipment_Nbr=b.Shipment_Nbr)
inner join Charge c on (b.Uniq_Id=c.Uniq_Id)
)   -- FROM

This should probably do it

select
a.Invoice, b.Shipment_Nbr, a.Invoice_date, b.Adjust_rsn, b.Adjust_date, c.Charge_code, c.Charge_Amt 
FROM
(
select * from Invoicing a
qualify (row_number() over (partition by Shipment_Nbr order by Invoice_date desc)=1)
) a
inner join 
(
select * from Adjust b 
qualify (row_number() over (partition by Shipment_Nbr order by Adjust_date desc)=1)
) b on (a.Shipment_Nbr=b.Shipment_Nbr)
inner join Charge c on (b.Uniq_Id=c.Uniq_Id)
)   -- FROM
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文