SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Ranking using 2 columns in T-SQL 2012


Ranking using 2 columns in T-SQL 2012

Author
Message
chrismic999
chrismic999
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 5
I have a table where basically I want to identify the first Invoice placed by an account.
In the file I have 2 columns AccountName and Invoice_Date which I order by and then want to update a Rank_Order Column to state the 1st entry and then extract this for something else. basically I want to know when the company placed its very first order !!

Here's a sample of the data I would like to get out

Can someone help me with the SQL - This is pretty urgent too !!! Thanks

AccountName INVOICE_DATE Rank Order
A 01/08/2013 1
A 01/09/2013 2
A 01/10/2013 3
B 01/08/2013 1
B 01/08/2013 1
B 03/01/2014 2
B 01/02/2014 3
B 03/02/2014 4
C 01/10/2012 1
C 04/10/2013 3
C 10/04/2013 2
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63131 Visits: 17959
chrismic999 (5/27/2014)
I have a table where basically I want to identify the first Invoice placed by an account.
In the file I have 2 columns AccountName and Invoice_Date which I order by and then want to update a Rank_Order Column to state the 1st entry and then extract this for something else. basically I want to know when the company placed its very first order !!

Here's a sample of the data I would like to get out

Can someone help me with the SQL - This is pretty urgent too !!! Thanks

AccountName INVOICE_DATE Rank Order
A 01/08/2013 1
A 01/09/2013 2
A 01/10/2013 3
B 01/08/2013 1
B 01/08/2013 1
B 03/01/2014 2
B 01/02/2014 3
B 03/02/2014 4
C 01/10/2012 1
C 04/10/2013 3
C 10/04/2013 2


Using ROW_NUMBER with PARTITION is how I would do this.

Since you didn't provide ddl this example is untested.


ROW_NUMBER() OVER(PARTITION BY AccountName Order by INVOICE_DATE) as RankOrder



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
enmadrid76
enmadrid76
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 239
This should do the trick:


select
q.AccountName
,q.INVOICE_DATE as FirstOrderDate
from (
select
AccountName
,INVOICE_DATE
,ROW_NUMBER() OVER(PARTITION BY AccountName order by date desc) as Rank_Order
from table_name
) as q
where q.Rank_Order = 1
order by q.AccountName


David Burrows
David Burrows
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16727 Visits: 10114
Why not use MIN


SELECT AccountName,MIN(INVOICE_DATE) AS [INVOICE_DATE]
FROM [table]
GROUP BY AccountName




Far away is close at hand in the images of elsewhere.

Anon.


chrismic999
chrismic999
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 5
This seems to work

SELECT accountname,invoice_date, ROW_NUMBER()
OVER(PARTITION BY AccountName Order by INVOICE_DATE) as RankOrder from dbo.sagesales
where AccountName is not null

Insert help from above

But now I want to update a field called "Place" (int) in dbo_sagesales with the result ie the ranked order

thanks

alternatively I would like to extract only those that have a ROW_Number as 1?
I tried where AccountName is not null and Rankorder =1 but it didnt like it in the above query!
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63131 Visits: 17959
chrismic999 (5/27/2014)
This seems to work

SELECT accountname,invoice_date, ROW_NUMBER()
OVER(PARTITION BY AccountName Order by INVOICE_DATE) as RankOrder from dbo.sagesales
where AccountName is not null

Insert help from above

But now I want to update a field called "Place" (int) in dbo_sagesales with the result ie the ranked order

thanks

alternatively I would like to extract only those that have a ROW_Number as 1?
I tried where AccountName is not null and Rankorder =1 but it didnt like it in the above query!




You can't reference windowing functions in the where clause. If you only want where the first one then I would use MIN like suggested above. No need to go to all the trouble to number all the rows just to throw most of them away anyway. Look at the code David posted. It is exactly what you need here.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
David Burrows
David Burrows
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16727 Visits: 10114
Also if your data contains duplicate invoice dates as shown in the result set in your first post then you will need to use DENSE_RANK not ROW_NUMBER.

Plus your two requirements in your last post are not compatible.
Do you want to update all records with the rank or only update the first date?
Is there a unique key on the table?


Far away is close at hand in the images of elsewhere.

Anon.


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search