troubleshooting Question

Tsql Rank, SQL 2008

Avatar of Leo Torres
Leo TorresFlag for United States of America asked on
SQL
12 Comments1 Solution357 ViewsLast Modified:
Hello,

I am still a bit green when it comes to using Rank

I Need to have a query pull the top 5 weeks in a year 2007

The query below works with AdventureWorks2008R2 Database
All my ranks are 1 I need it to rank the top 5


SELECT convert(date,dateadd( d,   7 - datepart(dw,ph.OrderDate), ph.OrderDate))  as WeekEndDate--Convert(Date,DateAdd(day, -1 * datepart(dw, ph.OrderDate), ph.OrderDate)) as WeekEnding
	   ,SUM(ph.TotalDue) As GrandTotal
	   ,RANK() Over (Partition BY dateadd( d,7 - datepart(dw,ph.OrderDate), ph.OrderDate) Order by SUM(ph.TotalDue) desc) rn
FROM Purchasing.PurchaseOrderHeader ph
Where YEAR(OrderDate) = 2007
GROUP BY dateadd( d,   7 - datepart(dw,ph.OrderDate), ph.OrderDate)
Order by SUM(ph.TotalDue) desc
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 12 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros