Avatar of cyber-33
cyber-33Flag for United States of America

asked on 

populating table column based on order

I have a table with the following columns
Id
Date
Class
Seq

The last column is currently empty.  I need to populate it based ordered date grouped by class, so that the earliest record within a given class has a value of 1, next has 2 and so on.
Engine is mysql.

Thank you!
DatabasesMySQL ServerSQL

Avatar of undefined
Last Comment
cyber-33
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Hi,

You may try this:

CREATE TABLE Table1 (id int, Date Datetime, Class varchar(10),Seq int);
INSERT INTO Table1 VALUES (1, '20130201', 'a',NULL);
INSERT INTO Table1 VALUES (2, '20130202', 'a',NULL);
INSERT INTO Table1 VALUES (3, '20130203', 'a',NULL);
INSERT INTO Table1 VALUES (4, '20130204', 'b',NULL);
INSERT INTO Table1 VALUES (5, '20130205', 'b',NULL);
INSERT INTO Table1 VALUES (6, '20130206', 'c',NULL);
INSERT INTO Table1 VALUES (7, '20130207', 'd',NULL);


UPDATE Table1 dest,(
SELECT  o.id
       ,o.Date
       ,o.Class
       ,@row_num := IF(@prev_value=o.Class,@row_num+1,1) AS Seq
       ,@prev_value := o.Class
  FROM Table1 o,
      (SELECT @row_num := 1) x,
      (SELECT @prev_value := '') y
  ORDER BY o.Class
  ) src
SET dest.Seq = src.Seq
WHERE dest.Id = src.Id

SELECT * FROM Table1

Open in new window


As you may see though i inserted NULL in Seq, when i select the results after the update Seq is populated.

Giannis
Avatar of ol muser
ol muser
Flag of United States of America image

The above solution by @jyparask should work as long as you have at least  one unique identifier(as in the column ID above). Just remember to disable safe mode, by un-checking the option in Preferences -> SQL Queries and reconnect.
Avatar of cyber-33
cyber-33
Flag of United States of America image

ASKER

Looks pretty elegant.
I dont see what in this query will keep the records within each class ordered by date?

Also, why disable the safe mode?
ASKER CERTIFIED SOLUTION
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of cyber-33
cyber-33
Flag of United States of America image

ASKER

Have not tested it, but looks pretty good. Thank you!
Databases
Databases

Databases are organized collections of data, most commonly accessed through management systems including schemas, tables, queries and processes that allow users to enter and manipulate the information or utilize it in other fashions, such as with web applications or for reporting purposes.

62K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo