Solved

update sequence field in Sql

Posted on 2013-06-26
4
439 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 500 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

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.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

691 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