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
 General SQL Server Forums
 New to SQL Server Programming
 how to order this table.

Author  Topic 

jeff06
Posting Yak Master

166 Posts

Posted - 2007-03-19 : 11:38:58
I have a table

merchant contractbr date
a 2333 1/1/2005
a A34 3/12/2006
a R78 2/1/2005
..
b
b
b
c
c
c
.....

different merchant has different number of contracts.
I want to order the contracts for merchants according to the date.
the result table should look like:

merchant contractbr date order
a 2333 1/1/2005 1
a R78 2/1/2005 2
a A34 3/12/2006 3
..
b 1
b 2
b 3
c 1
c 2
c 3
c 4
c 5
.....

Thanks

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-19 : 11:43:20
select *, row_number() over (partition by merchant order by date) as [order]
from table1


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

jeff06
Posting Yak Master

166 Posts

Posted - 2007-03-19 : 12:03:10
Thank you for your quick response
when I run this code i got error msg:

Server: Msg 195, Level 15, State 10, Line 1
'row_number' is not a recognized function name.

How to fix this problem?
Thanks
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-19 : 12:06:13
Oh, you are using SQL Server 2000?

select t1.*, (select count(*) from table1 as x where x.merchant = t1.merchant and x.date <= t1.date) as [order]
from table1 as t1


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

jeff06
Posting Yak Master

166 Posts

Posted - 2007-03-19 : 12:24:46
Thank you so much, Preso. It works perfectly.
I have one more question in a new thread. I will appreciated if you can help me out.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-19 : 13:23:44
Yes, continued here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=80826


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -