Solved

SQL Update records with their occurance with a group

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

752 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