Monday, 26 August 2013

select last 5 rows in join query sql-server 2008

select last 5 rows in join query sql-server 2008

I am trying to bring the last 5 rows of the Order table based on OrderDate
with the column name "firstname" from Customer table. The below query
displays all the values from Order table instead of last 5 rows.
SELECT
A.[FirstName],B.[OrderId],B.[OrderDate],B.[TotalAmount],B.[OrderStatusId]
FROM [schema].[Order] B
OUTER APPLY
( SELECT TOP 5 *
FROM [schema].[Customer] A
WHERE B.[CustomerId]=1 AND A.[CustomerId]=B.[CustomerId]
ORDER BY
B.[OrderDate] DESC
) A
Any mistake in my logic of using TOP and DESC ?

No comments:

Post a Comment