Solved

# help with SQl 2005

Posted on 2011-10-01
216 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
Question by:EWHTLC
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
5 Comments

LVL 19

Assisted Solution

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
``````
0

LVL 92

Assisted Solution

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
``````
0

LVL 75

Accepted Solution

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
``````
0

Author Closing Comment

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

ID: 36906804
Glad to help :)
0

## Featured Post

Question has a verified solution.

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

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
###### Suggested Courses
Course of the Month4 days, 10 hours left to enroll

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

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