简体   繁体   中英

SQL- how to join 3 tables but only group by columns from two

I am trying to join 3 tables below together but I only want to group by customer_ID In the first 2 table, how to achieve this?
In other words the third table I use is only to eliminate the records. Many thanks!

table 1

customer_ID  product_No  
 100          ABC001
 100          ABC111
 100          ABC112 
 200          ABC002

table2

product_No   Amount
 ABC001        10
 ABC002        50
 ABC111        60
 ABC112        70

table 3

Valid_Product  Desc
 ABC001         Y
 ABC111         Y

I'm ok with joining table 1 and 2 by doing

select 
    t1.product_No, Max(t2.amount)
from 
    t1, t2
where 
    t1.product_No = t2.product_No
group by 
    customer_ID

Now how do I in the same query join to table 3 and only get the value of ABC111 for customer 100 as that's the valid product with Max amount?

so the result would be Customer_ID 100 Amount 60 The goal is to get the max amount of a product (only if the product is in the 3rd table with a desc Y) under a customer.

If for each customer you want the valid product with the maximum amount, then use row_number() :

select *
from (select t1.*, t2.amount,
             row_number() over (partition by t1.customer_id t2.amount desc) as seqnum
      from t1 join
           t2
           on t1.product_No = t2.product_No join
           t3
           on t3.Valid_Product = t1.product_No
     ) t
where seqnum = 1;

It's just another join and a WHERE clause filtering on t3.desc . It doesn't seem like you need the columns from T3 in the projection.

select t1.customer_ID, Max(t2.amount)
from t1
     join t2
     on t1.product_No = t2.product_No
     join t3
     on t1.product_No = t3.valid_product
where t3.desc = 'Y'
Group by t1.customer_ID

Incidentally, you will notice I wrote the query using the ANSI 92 join syntax. Oracle has supported this since 9i (twenty years now) and it really does make queries easier to read.

For your output sample simple aggregate function and join is enough

select t1.*,t2.Amount from table1 t1 join

 (
 select * from table2 t2 where amount in
   ( 
    select max(Amount) as amt
   from table2 inner join table3 t3 on 
    t2.product_No=t3.Valid_Product

   )  
  ) t2 on t1.product_No=t2.product_No

You want to know whether a product exists in table 3. We check existence in SQL with EXISTS or IN .

select 
  t1.customer_id, max(t2.amount)
from t2
left join t2 on  t2.product_no = t1.product_no
where t1.product_no in (select product_no from t3 where desc = 'Y')
group by t1.customer_id
order by t1.customer_id;

Don't join, when you are only looking up a table whether an entry exists.

Try below using row_number()

select * from 
(select customer_ID,t1.product_No, t2.amount, row_number() over (partition by customerid order by amount desc) as rn
from t1 inner join t2
where t1.product_No = t2.product_No)a
inner join table3 on a.product_no = Valid_Product
where rn=1

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