Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

help with SQl 2005

Posted on 2011-10-01
5
Medium Priority
?
220 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 600 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 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 600 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 800 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 93

Expert Comment

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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2

885 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