Using Top in SQL Server 2012 -


i have table contacts, parent table activity. select latest activity each contact, getting more 1 row.

this query:

select top 30       *        contacts o, activity d         o.id = d.contact       , d.id > 401061       , last_action null  order       d.activity_date desc 

i think need top? not sure how implement here. appreciated.

you can use row_number() number each contact's activities. in outer query, can filter down latest activity per contact:

select  top 30 *     (         select  row_number() on (                     partition o.id                     order d.activity_date desc) rn         ,       *            contacts o         join    activity d         on      o.id = d.contact            d.id > 401061                  , last_action null          ) subqueryalias   rn = 1 -- last activity per contact order           activity_date desc 

Comments