?
Solved

update sequence field in Sql

Posted on 2013-06-26
4
Medium Priority
?
443 Views
Last Modified: 2013-06-26
So i have a table with lets say four fields on table1

Date
Group
Status
Sequence_number

i need to update the sequence grouping the fields by the other three fields.

so if there are 4 records where Date, Group and Status are the same,  the Sequence_number would start at 101, 102, 103, 104 then start again at 101 for the next group of records where these three fields are the same.

thanks in advance
0
Comment
Question by:damixa
[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
4 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39278044
Here's the SELECT, with some supporting testing code.  Copy-paste this into your SSMS, verify it works, then we'll have to figure out how to UPDATE with the final set.

CREATE TABLE #tmp (date datetime, the_group varchar(10), status varchar(10), sequence_number int) 

INSERT INTO #tmp (date, the_group, status)
VALUES 
	('1-1-2012', 'Group1', 'Ready'), 
	('1-1-2012', 'Group1', 'Ready'), 
	('1-1-2012', 'Group1', 'Ready'), 
	('1-1-2012', 'Group1', 'Not Ready'), 	
	('1-1-2012', 'Group2', 'Ready'), 
	('1-1-2012', 'Group2', 'Not Ready'), 
	('1-1-2012', 'Group2', 'Not Ready'), 
	('7-1-2012', 'Group1', 'Ready'), 
	('7-1-2012', 'Group1', 'Ready'), 
	('7-1-2012', 'Group1', 'Ready'), 
	('7-1-2012', 'Group1', 'Not Ready'), 	
	('7-1-2012', 'Group2', 'Ready'), 
	('7-1-2012', 'Group2', 'Not Ready'), 
	('7-1-2012', 'Group2', 'Not Ready')
	
SELECT * FROM #tmp

SELECT date, the_group, status, sequence_number = ROW_NUMBER() OVER (PARTITION BY date, the_group, status ORDER BY date ) + 100
FROM #tmp
ORDER BY date, the_group, status

Open in new window

0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 39278106
If there's a PK on your table, then this works...

DROP TABLE #tmp

CREATE TABLE #tmp (id int identity(1,1), date datetime, the_group varchar(10), status varchar(10), sequence_number int) 

INSERT INTO #tmp (date, the_group, status)
VALUES 
	('1-1-2012', 'Group1', 'Ready'), 
	('1-1-2012', 'Group1', 'Ready'), 
	('1-1-2012', 'Group1', 'Ready'), 
	('1-1-2012', 'Group1', 'Not Ready'), 	
	('1-1-2012', 'Group2', 'Ready'), 
	('1-1-2012', 'Group2', 'Not Ready'), 
	('1-1-2012', 'Group2', 'Not Ready'), 
	('7-1-2012', 'Group1', 'Ready'), 
	('7-1-2012', 'Group1', 'Ready'), 
	('7-1-2012', 'Group1', 'Ready'), 
	('7-1-2012', 'Group1', 'Not Ready'), 	
	('7-1-2012', 'Group2', 'Ready'), 
	('7-1-2012', 'Group2', 'Not Ready'), 
	('7-1-2012', 'Group2', 'Not Ready')
	
SELECT * FROM #tmp ORDER BY id

UPDATE #tmp
SET sequence_number = rn.seq
FROM #tmp
  JOIN (
	SELECT id, ROW_NUMBER() OVER (PARTITION BY date, the_group, status ORDER BY date ) + 100 as seq
	FROM #tmp) rn ON #tmp.id = rn.id

SELECT * FROM #tmp ORDER BY id




 

Open in new window

0
 

Author Closing Comment

by:damixa
ID: 39278853
perfect, thanks
0
 
LVL 66

Expert Comment

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

Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

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