简体   繁体   中英

SQL Query with join on non-unique Entries

I have problems with two tables I need to join.

Table1

Order_id              Item_Revenue            Item_id
1                     30 €                    22222222
1                     20 €                    11111111
1                     10 €                    33333333
2                     5 €                     55555555

Table2

package_id           Order_id                 shipping_costs
456                  1                        1 €
567                  1                        2 €
789                  1                        3 €
999                  2                        2 €

The Output I need: I want to show the Item Revenue and the Shipping costs per Order_Id

Order_ID          count(item_id)      sum(item_revenue)  sum(shipping_costs)
1                 3                   60 €                6 €
2                 1                   5 €                 2 €

My first try was:

Select
sum(t1.item_revenue),
count(t1.item_id),
sum(t2.shipping_costs),
from table1 t1
left join table2 t2 on t1.order_id = t2.order_id
group by t1.order_id

But it does not work because order_id is not unique.

Please look at my example:

I would be so lucky if somebody could help me.

I think you need inner join

    Select order_id,count( distinct item_id),
    sum(item_revenue),
    sum(shipping_costs)
    from table1
    join table2 on table1.order_id = table2.order_id
    group by order_id

Your problem with alias and missing commas. in the GROUP BY you used " order_id " but you need to define the field related with Table1 or Table2. Also, for alias. Don't start with number like 1,2. you can get error message.

EDIT (Table2 in SubQuery):

Select
    T1.order_id
    ,count(1) ItemCount
    ,sum(item_revenue) ItemRevenue
    ,sum(shipping_costs) ShippingCost
from table1 T1
INNER join (SELECT Order_id
                  ,SUM(shipping_costs) shipping_costs  
            FROM Table2
            GROUP BY Order_id) T2 on T1.order_id = T2.order_id
group by T1.order_id

I hope this helps and assuming you are using Oracle SQL

select table1.Order_id
sum(table1.item_revenue)
count(table1.item_id)
sum(table2.shipping_costs)
from
(
  select Order_id,Item_Revenue,Item_id, row_number() over (partition by Order_id order by Item_id) as man_id
  from table 1
)
table1
left join
(
  select package_id,Order_id,shipping_costs, row_number() over (partition by Order_id order by package_id) as man_id
  from table 2
)
table2
on table1.order_id = table2.order_id
and table1.man_id = table2.man_id
group by table1.order_id
select order_id , count(item_id), sum(item_revenue) , sum(shipping_costs)

来自table1,table2,其中table1.orderid = table2.order_id按order_id分组

Summarize the two tables before joining. Then join using a full join so you get all orders in both tables:

select coalesce(t1.order_id, t2.order_id) as order_id,  -- include the order_id
       t1_revenue, t1_count,
       shipping_costs
from (select t1.order_id, sum(t1.item_revenue) as t1_revenue, count(*) as t1_count
      from table1 t1
      group by t1.order_id
     ) t1 full join
     (select t2.order_id, t2.shipping_costs as shipping_costs
      from table2 t2
      group by t2.order_id
     ) t2
     on t1.order_id = t2.order_id;

If your database doesn't support full join , then use left join . Presumably, orders in table2 are also in table1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM