Solved

Sequential number grouped by Ordernumber

Posted on 2016-10-12
5
47 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
[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
  • 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

740 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