Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Ranking using 2 columns in T-SQL 2012 Expand / Collapse
Author
Message
Posted Tuesday, May 27, 2014 9:18 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, May 27, 2014 11:57 AM
Points: 3, 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
Post #1574832
Posted Tuesday, May 27, 2014 9:24 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:41 PM
Points: 13,315, Visits: 12,182
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 Moden's 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)
Post #1574835
Posted Tuesday, May 27, 2014 9:31 AM This worked for the OP Answer marked as solution
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, August 15, 2014 6:42 AM
Points: 3, Visits: 194
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

Post #1574841
Posted Tuesday, May 27, 2014 9:37 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:45 AM
Points: 7,102, Visits: 6,933
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.

Post #1574844
Posted Tuesday, May 27, 2014 9:39 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, May 27, 2014 11:57 AM
Points: 3, 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!


Post #1574845
Posted Tuesday, May 27, 2014 9:49 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:41 PM
Points: 13,315, Visits: 12,182
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 Moden's 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)
Post #1574851
Posted Tuesday, May 27, 2014 10:00 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:45 AM
Points: 7,102, Visits: 6,933
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.

Post #1574860
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse