Solved

Sequential number grouped by Ordernumber

Posted on 2016-10-12
5
45 Views
Last Modified: 2016-10-13
Hello,

I'm trying to generate a column in a SQL Server 2008 query  where I can have a sequential number for all the order records that include their order details.  I basically want to have a sequential number grouped by order number, which can be repeated if the order has multiple rows.

In this case, the first row is what I need.


MySq      Ord#      Product            OrdDetailSeq
1      42825      widget1            1
2      42828      widget3            1
3      42829      widget1            1
3      42829      widget2            2
3      42829      widget4            3
4      42832      widget2            1
4      42832      widget3            2
5      42837      widget2            1


Does anyone know how to accomplish this?

Thanks.
0
Comment
Question by:TheUndecider
  • 3
5 Comments
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 200 total points
ID: 41840358
SELECT MySq,      Ord#,      Product,            
    ROW_NUMBER() OVER(PARTITION BY Ord# ORDER BY Product) AS OrdDetailSeq
FROM ...
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41840364
Not sure what 'In this case, the first row is what I need.' means, but looking at the desired set and the rest of the description OrdDetailSeq can be generated using ROW_NUMBER:  
SELECT MySq, [Ord#], Product, 
  ROW_NUMBER() OVER (PARTITION BY [Ord#] ORDER BY Product) as OrdDetailSeq
FROM YourTable

Open in new window

0
 

Author Comment

by:TheUndecider
ID: 41840954
Thank you for replying, but what I need is the first column: MySq.  The rest of the columns are already there.  I need the sequential number for each set of rows that share the same order number.
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 300 total points
ID: 41840962
>Thank you for replying, but what I need is the first column: MySq.
Your question said 'first row', which confused everyone.  Try this
SELECT 
   RANK() OVER (ORDER BY [Ord#]) as MySq, 
   [Ord#], Product, OrdDetailSeq
FROM YourTable

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41842026
Thanks for the split.  Good luck with your project.  -Jim
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

772 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