We help IT Professionals succeed at work.

Tsql Rank, SQL 2008

Leo Torres
Leo Torres asked
on
354 Views
Last Modified: 2012-02-14
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

Open in new window

Comment
Watch Question

Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
I'd suggest not using RANK for this


SELECT top 5 dateadd( d,   7 - datepart(dw,ph.OrderDate), ph.OrderDate)  as WeekEndDate
         ,SUM(ph.TotalDue) As GrandTotal
FROM Purchasing.PurchaseOrderHeader ph
Where YEAR(OrderDate) = 2004
GROUP BY dateadd( d,   7 - datepart(dw,ph.OrderDate), ph.OrderDate)
Order by SUM(ph.TotalDue) desc;
Database Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Leo TorresSQL Developer
CERTIFIED EXPERT

Author

Commented:
Ok, so How can I display rank by grand Total
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
use the rank query,  but include the "r" column


select WeekEndDate, GrandTotal, r from ....
Leo TorresSQL Developer
CERTIFIED EXPERT

Author

Commented:
Guess I should Have mentioned that Displaying the rank is part of the requirement
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
not a problem,  now you have 2 ways to do it without displaying rank, and one with displaying the rank.

need anything else?
Leo TorresSQL Developer
CERTIFIED EXPERT

Author

Commented:
select      Convert(date,WeekEndDate) as WeekEnding,
            GrandTotal,
            r as WeekRank
from    (
            select WeekEndDate, GrandTotal, rank() over(order by GrandTotal desc) r
            from (
                        SELECT  dateadd( d,   7 - datepart(dw,ph.OrderDate), ph.OrderDate)  as WeekEndDate
                        ,SUM(ph.TotalDue) As GrandTotal
                        FROM Purchasing.PurchaseOrderHeader ph
                        Where YEAR(OrderDate) = 2007
                        GROUP BY dateadd( d,   7 - datepart(dw,ph.OrderDate), ph.OrderDate)
                        ) as x
            ) as x
where r  <=5
Order by GrandTotal desc;
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
why are you converting a date into a date?
Leo TorresSQL Developer
CERTIFIED EXPERT

Author

Commented:
Actually yes, Now I need a pivot..

With The ShipMethod table

Date to date I wanted to get rid of time just wanted date see picture

This is the result I am looking for
EE-pivot.png
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
sorry, when I asked "anything else" I meant with relation to the original question.


since that appears to be a new question.  please close this one and open a new question
Leo TorresSQL Developer
CERTIFIED EXPERT

Author

Commented:
Very Excellent and fast
Leo TorresSQL Developer
CERTIFIED EXPERT

Author

Commented:

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.