sql - First row from each group based on condition -
id date 1 5/11/2015 1 5/11/2015 1 5/12/2015 1 5/13/2015 2 5/11/2015 2 5/11/2015 2 5/12/2015 2 5/13/2015 3 5/14/2015 3 5/15/2015 3 5/16/2015 3 5/17/2015 4 5/13/2015 4 5/13/2015 4 5/14/2015 4 5/15/2015 id name 1 roy 2 jame 3 jani 4 romi i not able first row matching second table
i want 1 row each table group id date greater today's date (i.e. 5/11/2015), shown below.
id name date 1 roy 5/12/2015 2 jane 5/12/2015 3 jani 5/14/2015 4 romi 5/13/2015
use cross apply or correlated subquery this
select * table2 t2 cross apply (select top 1 [date] table1 t1 t2.id = t1.id , t1.[date] > convert(date,getdate()) order [date] asc) cs
Comments
Post a Comment