Solved

SQL Update records with their occurance with a group

Posted on 2014-10-28
2
161 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
[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
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
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.

636 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