Solved

SQL Update records with their occurance with a group

Posted on 2014-10-28
2
135 Views
Last Modified: 2014-10-29
I have a t-SQL problem I can't get my head around.  I have a table of orders with each order having a unique alpha OrderNo.  Each order is associated with a BaseOrder in a many to one relation ship (one baseorder can have multiple orders).

The issue I have is I need to update the table and set a column to indicate the count of the order within the associated base order.  The example below indicates what I am after.  Orders 10001, 10012, 10018 and 1001A are all associated with BaseOrder 10001.  I need to update the SubOrderNumber with 1, 2, 3, 4 respectively as I want them stamped in incremental order based on their alpha sort order.


Example
0
Comment
Question by:canuckconsulting
2 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 500 total points
ID: 40409616
Firts try if this select is ginivng you required output :


select orderno, baseorder,  ROW_NUMBER() OVER (PARTITION BY orderno, baseorder ORDER By orderno, baseorder) as SubOrderNumber 
from orders

Open in new window

then try this:

UPDATE t
SET t.SubOrderNumber = t2.SubOrderNumber 
FROM order t
INNER JOIN (
select orderno, baseorder,  ROW_NUMBER() OVER (PARTITION BY orderno, baseorder ORDER By orderno, baseorder) as SubOrderNumber 
from orders
)t2
ON (t.OrderNo= t2.OrderNo and t.BaseOrder =t2.BaseOrder  )
WHERE conditions;

Open in new window

0
 

Author Closing Comment

by:canuckconsulting
ID: 40410996
Fabulous!  I had never heard of the over clause...what a handy tool!

Thanks,

Scott
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now