Solved

help with SQl 2005

Posted on 2011-10-01
5
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
Comment
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

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

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…

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.

Join & Ask a Question