Solved

help with SQl 2005

Posted on 2011-10-01
5
215 Views
Last Modified: 2012-05-12
Can someone help / advise.
I have a tabe containing order line details. In it is an order line number.
i.e

Order    Line-Number
1234            1000
1234            1500
1234            2000
2222            1000
2222            2000

Because I would like to identify the line number in a more specfic way I'd like to add a counr to the table as follows

Order    Line-Number   Line Sequence
1234            1000                   1
1234            1500                   2
1234            2000                   3
2222            1000                   1
2222            2000                   2

Any ideas
0
Comment
Question by:EWHTLC
5 Comments
 
LVL 19

Assisted Solution

by:Bhavesh Shah
Bhavesh Shah earned 150 total points
ID: 36896342
Hi,

use  Row_Number function.

http://msdn.microsoft.com/en-us/library/ms186734.aspx


- Bhavesh
SELECT OrderNo, Line, ROW_NUMBER() OVER(ORDER BY OrderNo) AS 'Row'

FROM TableName

Open in new window

0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 150 total points
ID: 36896380
Another way, compatible with SQL 2000:

SELECT t1.OrderNo, t1.LineNo, COUNT(T2.OrderNo) AS Sequence
FROM SomeTable t1 INNER JOIN
    SomeTable t2 ON t1.OrderNo = t2.OrderNo AND t1.LineNo >= t2.LineNo
GROUP BY t1.OrderNo, t1.LineNo
ORDER BY t1.OrderNo, t1.LineNo

Open in new window

0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 200 total points
ID: 36896917
Actually the query using ROW_NUMBER should be:
SELECT  [Order],
        [Line-Number],
        ROW_NUMBER() OVER (PARTITION BY [Order] ORDER BY [Line-Number]) [Line Sequence]
FROM    YourTableName

Open in new window

0
 

Author Closing Comment

by:EWHTLC
ID: 36903972
THank you all for you responses.
Sorry I was a while responding myself.

I tried all and although all worked pretty well the last option fit my needs perfectly.
I've split the points.
hope thats OK.

Thanks again
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36906804
Glad to help :)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question