Henry Henry - 1 year ago 104
SQL Question

LEFT JOIN SQL but distinct

I Have 2 tables,

A.Table Sales

Code | qty_sales Values
A | 10
B | 20

B. Table Master

Code | Style
A | AA10
A | AA12
B | BB10

And I want the result like this :

Code | Style | qty
A | A10 | 10
B | BB10 | 20

I just want to get one of the 2 style for code A

Answer Source

Complementing the last answer by Gordon Linoff

I just change "m.style" to "c.style".

select s.code, c.style , s.qty_sales
from sales s join
 (select m.code, max(m.style) as style
  from master m
  group by m.code
 ) c
 on s.code = c.code;

I agree you will need a "rule" to get just one register of Master table. I the example above by Gordon Linoff used the MAX command.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download