sql server 2005 - SQL Query, SELECT Top 2 by Foreign Key Order By Date -
I need a SQL query that gives the top 2 plans PlanDate
per client ID Is on a table where Planetida
is the primary ID, ClientID
is a foreign id
this is what I have yet ->
SELECT * FROM [dbo]. [TblPlan] where [PlanID] IN (SELECT TOP (2) PlanID FROM [DBO]. [TblPlan] Order [PlanDate] DESC), of course, only two records are sent. Where I actually have 2 records per ClientID
.
This can be done using ROW_NUMBER:
SELECT PlanId, ClientId, PlanData (Select ROW_NUMBER () (Particular by Client Id Order by Planner DESC) RN, * FOR [DBO]. [TBPLAN]) AS T1 WHERE RN & LT; = 2
You will have to select any other column to get them.
Comments
Post a Comment