一对多加入两个表中的总和;如何处理重复?

时间:2018-03-07 00:47:33

标签: sql sql-server one-to-many

我有以下情况 -

物品销售:

    ╔═════╦════════╦════════════╗
    ║ SKU ║ ItemId ║ SaleAmount ║
    ╠═════╬════════╬════════════╣
    ║ 123 ║      1 ║ $45.99     ║
    ║ 456 ║      2 ║ $54.99     ║
    ╚═════╩════════╩════════════╝

促销优惠券:

    ╔═════╦══════════╦══════════════╗
    ║ SKU ║ CouponId ║ CouponAmount ║
    ╠═════╬══════════╬══════════════╣
    ║ 123 ║        1 ║ $4           ║
    ║ 123 ║        2 ║ $5           ║
    ║ 123 ║        3 ║ $2           ║
    ║ 456 ║        1 ║ $2           ║
    ╚═════╩══════════╩══════════════╝

期望的最终结果

    ╔═════╦════════════╦══════════════╗
    ║ SKU ║ SaleAmount ║ CouponAmount ║
    ╠═════╬════════════╬══════════════╣
    ║ 123 ║ $45.99     ║ $11          ║
    ║ 456 ║ $54.99     ║ $2           ║
    ╚═════╩════════════╩══════════════╝

我已经通过将已过滤的item sales查询加入过滤的sale coupons查询并按sku分组来实现此功能,但我想知道是否有更好的方法可以执行此操作这并不会导致SaleAmount中每条记录的sale coupons重复。即如果你只是在LEFT JOIN以及sale couponsSUM(SaleAmount), SUM(CouponAmount)GROUP BY SKU,你就会得到 -

    ╔═════╦════════════╦══════════════╗
    ║ SKU ║ SaleAmount ║ CouponAmount ║
    ╠═════╬════════════╬══════════════╣
    ║ 123 ║ $137.97    ║ $11          ║
    ║ 456 ║ $54.99     ║ $2           ║
    ╚═════╩════════════╩══════════════╝

哪个适用于sku=456,但如果有多个相应的sale coupon记录则会崩溃。

3 个答案:

答案 0 :(得分:2)

试试这个:

WITH S AS 
(
   SELECT SKU, SUM(SaleAmount) AS SaleAmount
   FROM Sales
   GROUP BY SKU
), 
C AS 
(
   SELECT SKU, SUM(CouponAmount) AS CouponAmount
   FROM Coupons
   GROUP BY SKU
)
SELECT ISNULL(S.SKU, C.SKU) AS SKU, S.SaleAmount, C.CouponAmount
FROM S FULL OUTER JOIN C ON S.SKU = C.SKU

答案 1 :(得分:0)

你可以这样做:

;with item_sales as (
select 
123 as sku
,1 as itemid
,45.99 as salesamount

union all

select 
456 as sku
,2 as itemid
, 54.99 as salesamount
)
Select item_sales.* into #itemsales from item_sales


;with mycte as (
select 
123 as sku
,1 as couponid
,4 as couponamount

union all

select 
123 as sku
,2 as couponid
,5 as couponamount
union all

select 
123 as sku
,3 as couponid
,2 as couponamount
union all

select 
456 as sku
,1 as couponid
,2 as couponamount
)

Select
 mycte.sku
 ,ist.salesamount
 ,sum(couponamount )  as couponamount 
from mycte

inner join #itemsales  ist
    on  ist.sku = mycte.sku
group by 
 mycte.sku
 ,ist.salesamount

 drop table #itemsales

答案 2 :(得分:0)

这会对你有所帮助。您需要使用SKU和SaleAmount进行分组

    select i.sku,SaleAmount,sum(CouponAmount) as CouponAmount from item i join sale s 
    on i.sku=s.sku group by i.sku,i.SaleAmount
--Edited