Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Last dates in sql

Author  Topic 

kezzaman
Starting Member

3 Posts

Posted - 2013-05-24 : 06:09:47
Hello everyone,

I'm struggling over the following issue.
I have a table from which I get the results from the collumns Artcode and Orderdate. Now I want to get all articles and their last 5 orderdates. I'm using the following query but then I get only the last orderdate. Can someone help me. Thanx.

SELECT ARTCODE, MAX(ORDERDATE)
FROM TBL_SALES
GROUP BY ARTCODE

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-05-24 : 06:31:16
[code]SELECT ARTCODE, ORDERDATE
FROM (SELECT ARTCODE, ORDERDATE, ROW_NUMBER() OVER(PARTITION BY ARTCODE ORDER BY ORDERDATE DESC) RN
FROM TBL_SALES
) Temp
WHERE Temp.RN<=5[/code]

--
Chandu
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-05-30 : 05:42:24
Also refer this http://beyondrelational.com/modules/2/blogs/70/posts/10845/return-top-n-rows.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -