2017-02-13 100 views
0

我有2个表格,标题和详细信息。如下所示,表头将有1条记录,详细信息表将包含多条记录。现在在标题级别上有折扣。我想根据它们的重量分配每个细节项目的折扣并获得实际费率。如何在详细信息中分配标题级别折扣

enter image description here

感谢。

回答

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 
; 
+0

感谢。有效。 – user3625561