Solved

Sequential number grouped by Ordernumber

Posted on 2016-10-12
5
49 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 66

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 66

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 66

Expert Comment

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

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

630 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