【问题标题】:How to distribute header level discount across detail items如何在明细项目之间分配抬头级别折扣
【发布时间】:2017-07-01 02:26:53
【问题描述】:

我有 2 个表格,标题和详细信息。如下所示,表头表有 1 条记录,明细表有多条记录。现在标题级别有折扣。我想根据每个详细商品的重量分配折扣并获得实际费率。

谢谢。

【问题讨论】:

    标签: sql sql-server oracle sql-server-2008 oracle10g


    【解决方案1】:

    您需要根据(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
    ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-25
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多