如何在明细项目中分配 header 级折扣
How to distribute header level discount across detail items
我有 2 tables,header 和细节。如下所示,header table 将有 1 条记录,详细信息 table 将有多条记录。现在 header 级别有折扣。我想按重量分配每个细节项目的折扣并获得实际费率。
谢谢。
您需要根据 (rate * qty)
占交易总额的百分比来分配折扣:
WITH transaction_totals AS (
SELECT h.header_id
,h.discount
,SUM(d.rate * d.ty) AS total
FROM header h
INNER JOIN detail d
ON h.header_id = d.header_id
GROUP BY h.header_id
,h.discount
)
SELECT d.item
,d.rate
,d.qty
,d.total
--,d.total / tt.total AS DetailPctTotal
,tt.discount * (d.total / tt.total) AS DetailDiscount
FROM detail d
INNER JOIN transaction_totals tt
ON d.header_id = tt.header_id
;
我有 2 tables,header 和细节。如下所示,header table 将有 1 条记录,详细信息 table 将有多条记录。现在 header 级别有折扣。我想按重量分配每个细节项目的折扣并获得实际费率。
谢谢。
您需要根据 (rate * qty)
占交易总额的百分比来分配折扣:
WITH transaction_totals AS (
SELECT h.header_id
,h.discount
,SUM(d.rate * d.ty) AS total
FROM header h
INNER JOIN detail d
ON h.header_id = d.header_id
GROUP BY h.header_id
,h.discount
)
SELECT d.item
,d.rate
,d.qty
,d.total
--,d.total / tt.total AS DetailPctTotal
,tt.discount * (d.total / tt.total) AS DetailDiscount
FROM detail d
INNER JOIN transaction_totals tt
ON d.header_id = tt.header_id
;