Solved

Reset row_number on field change

Posted on 2015-01-23
8
322 Views
Last Modified: 2015-01-23
Hi Experts, given the query attached and results, how do I get it to reset the row_number() every time the order_no changes?

So instead of the row_number() / line_number continually incrementing, I want it to go 1,2,3 then 1,2,3,4,5 depending on the lines in the order

Current Query   Results
0
Comment
Question by:takwirirar
8 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 180 total points
ID: 40566157
Don't know exactly what you mean, but try changing:

SELECT row_number() over (ORDER BY [product])

to

SELECT row_number() over (PARTITION BY [product] ORDER BY [order_no])
0
 
LVL 14

Assisted Solution

by:nishant joshi
nishant joshi earned 160 total points
ID: 40566158
You need to use partition by to reset row number.

SELECT ROW_NUMBER() OVER(PARTITION BY Order_No ORDER BY [Product]) as line_number,Order_no,product
,order_qty,CONVERT(VARCHAR(10),data_required,120) as date_required FROM t

Open in new window

0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 160 total points
ID: 40566160
this small addition will do;
row_number() over (partition by order_no order [by product]
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 1

Author Comment

by:takwirirar
ID: 40566163
Excellent!
0
 
LVL 1

Author Comment

by:takwirirar
ID: 40566168
Apologies moderator, I meant to give all experts a share of points as all answers are correct. How do I do this?
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40566172
I have requested the question to be re-opened.
0
 
LVL 1

Author Closing Comment

by:takwirirar
ID: 40566269
Thank you all!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL STANDARD CORE 8 40
Upgrading my SSIS package in VS 2012 6 62
Re-appearing SQL Server Agent jobs 7 30
SQL Availablity Groups Shared Path 2 16
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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

821 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