Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Composite serial column in mysql

Is it possible to create a serial column based on another column in MySql?

Example table

reg    val    serialval
1       a      1 
1       b      2
2       z      1  
2       x      2
2       y      3 

If I insert another reg 2 the next serialval should be 4, if I insert another reg 1 the next serialval should be 3 if I insert a reg 3 the serial val must be 1 (first occurance of reg 3).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Desired output:

reg    val    serialval
1       a      1 
1       b      2
1       c      3
2       z      1  
2       x      2
2       y      3 
2       a      4
3       k      1

I’ve tried to create a storage procedure using chatGPT

DELIMITER $$

CREATE TRIGGER test_trigger
BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
    DECLARE max_serial INT;

    -- Get the maximum serial value for the current reg
    SELECT IFNULL(MAX(serialval), 0) INTO max_serial
    FROM my_table
    WHERE reg = NEW.reg;

    -- Set the serial value to max_serial + 1
    SET NEW.serialval = max_serial + 1;
END $$

DELIMITER ;

Although the above code gives me the following error:

Unknown system variable 'NEW.serialval'

>Solution :

Actually, given that MySQL 8+ now supports window functions, the serialval column can possibly be generated using something like:

SELECT reg, val, ROW_NUMBER() OVER (PARTITION BY reg ORDER BY val) serialval
FROM my_table
ORDER BY 1, 3;

The only issue here is that, going by your sample data, the serialval should reflect the order of insertion of new data, which does not completely line up with the sort order of val. In this case, we should use this version:

SELECT reg, val, ROW_NUMBER() OVER (PARTITION BY reg ORDER BY id) serialval
FROM my_table
ORDER BY 1, 3;

where perhaps id is some auto incrementing sequence value.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading