left join without duplicate values using MIN()

Posted by Clipper87 on Stack Overflow See other posts from Stack Overflow or by Clipper87
Published on 2013-11-11T21:27:16Z Indexed on 2013/11/11 21:53 UTC
Read the original article Hit count: 100

Filed under:
|
|
|

I have a table_1:

id  custno
1   1
2   2
3   3

and a table_2:

id  custno  qty  descr
1   1       10   a
2   1       7    b
3   2       4    c
4   3       7    d
5   1       5    e
6   1       5    f

When I run this query to show the minimum order quantities from every customer:

SELECT DISTINCT table_1.custno,table_2.qty,table_2.descr 
FROM table_1 
LEFT OUTER JOIN table_2 
    ON  table_1.custno = table_2.custno AND  qty = (SELECT MIN(qty) FROM table_2 
WHERE table_2.custno = table_1.custno   )

Then I get this result:

custno qty  descr
1      5    e 
1      5    f
2      4    c
3      7    d

Customer 1 appears twice each time with the same minimum qty (& a different description) but I only want to see customer 1 appear once. I don't care if that is the record with 'e' as a description or 'f' as a description.

How could I do this ?

Thx!

© Stack Overflow or respective owner

Related posts about sql

Related posts about join