答案 0 :(得分:0)
您需要根据(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
;