Question : T-SQL Query Help - Select Most recent row

I have a SQL 2005 Database with names and subscription records. NAME to SUBSCRIPTIONS is 1-many. A person can have more than one subscription, but only one for a give product code. Name and subscriptions are joined on ID.
I want to identify people that have no subscriptions that are current.  Meaning I want to know who has a subscription record older than say 13 months.  Problem is, they can be old/expired in one code, but current in the other.
I have this part that gives me all of the rows from subscriptions. Now I need to either group them by ID and select the first record or something else. Can anyone help on the best way to finish this process?
SELECT     Name.ID, Name.MEMBER_TYPE, Name.STATUS, Name.LAST_FIRST, Subscriptions.PRODUCT_CODE, Subscriptions.PROD_TYPE, Subscriptions.PAID_THRU
FROM         Name INNER JOIN
                      Subscriptions ON Name.ID = Subscriptions.ID
WHERE     (Name.MEMBER_TYPE = 'M') AND (Subscriptions.PROD_TYPE = 'SUB') AND (Subscriptions.PAID_THRU < '1/1/2007') AND
                      (Subscriptions.PRODUCT_CODE LIKE 'ca%') OR
                      (Subscriptions.PRODUCT_CODE LIKE 'oa$')

Answer : T-SQL Query Help - Select Most recent row

hi, try this
1:
2:
3:
4:
5:
6:
7:
SELECT     Name.ID, Name.MEMBER_TYPE, Name.STATUS, Name.LAST_FIRST, Subscriptions.PRODUCT_CODE, Subscriptions.PROD_TYPE, Subscriptions.PAID_THRU
FROM         Name INNER JOIN
                      Subscriptions ON Name.ID = Subscriptions.ID
WHERE     (Name.MEMBER_TYPE = 'M') AND (Subscriptions.PROD_TYPE = 'SUB') AND (Subscriptions.PAID_THRU < '1/1/2007') AND
                      ((Subscriptions.PRODUCT_CODE LIKE 'ca%') OR
                      (Subscriptions.PRODUCT_CODE LIKE 'oa$')) AND
                      Subscriptions.PAID_THRU = (SELECT MAX(PAID_THRU) FROM Subscriptions WHERE Name.ID = ID)
Open in New Window Select All
Random Solutions  
 
programming4us programming4us