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 2005 Forums
 Transact-SQL (2005)
 Inserting a ROW_NNUMBER column into a joi

Author  Topic 

Arithmomaniac
Yak Posting Veteran

66 Posts

Posted - 2008-07-18 : 15:34:48
If I had the join
Customer C LEFT JOIN Transaction T on T.CustomerID = C.ID
How would I insert
ROW_NUMBER() OVER(PARTITION BY C.ID ORDER BY T.ID)
As a column into the join (meaning, not above the FROM clause)?

---------
Ignorance may be bliss, but knowledge is thrill.

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2008-07-18 : 15:45:37
what do you mean into the join? you want ROW_NUMBER() in the output of your table joins?
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-07-18 : 15:51:23
Here is excellent article from Madhivanan:

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx
Go to Top of Page

Arithmomaniac
Yak Posting Veteran

66 Posts

Posted - 2008-07-18 : 16:13:54
I don't want to say SELECT ROW_NUMBER[...] FROM C LEFT JOIN T [...] , because I want to use the ROW_NUMBER in a calculation to find the amount someone bought at their first transaction (meaning, where ROW_COUNT would equal 1, if you sorted by transaction date). It needs to be in the FROM statement, with the join.

---------
Ignorance may be bliss, but knowledge is thrill.
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2008-07-18 : 16:20:12
then use row_number in a sub query for whatever table you want
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-19 : 01:25:08
You can also use COUNT(*) OVER (PARTITION BY ...)
But you probably already knew this because you have read Books Online.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -