Sunday, February 19, 2012

Get MIN ROW from two tables

I have two tables 'table1' and 'table2' which have a common column column as master child relationship.

Table1 Table2
cust_id cust_id
cust_name idx
info

Please suggest query that joins these tables but returns only min(idx) from the 'table2' for each cust_id (one row each).

Thanks
Vinnieselect t1.cust_id, idx = min(t2.idx)
from Table1 t1
join Table2 t2
on t1.cust_id = t2.cust_id
group by t1.cust_id

If some don't have children then just change join to left outer join

No comments:

Post a Comment