Hi i have this scenario
My Table
create table foo(
id int,
num int1,
stage enum('a','b','c'),
unique(id,stage)
);
here are a snippets for data
INSERT INTO `foo` (`id`, `num`, `stage`) VALUES
(1, 1, 'a'),
(1, 2, 'b'),
(1, 3, 'c'),
(2, 1, 'a'),
(2, 2, 'b'),
(2, 3, 'c'),
(3, 1, 'a'),
(3, 2, 'b'),
(4, 1, 'a');
Notes on table
an id that has a state of c must have a prior state of a,b triggers no problem
query this as

I did this useing distinct with scalar correlated subquery
SELECT DISTINCT
id, IFNULL((
SELECT num
FROM foo f
WHERE f.id = foo.id AND f.stage = 'a'),'') `a`, IFNULL((
SELECT num
FROM foo f
WHERE f.id = foo.id AND f.stage = 'b'),'') `b`, IFNULL((
SELECT num
FROM foo f
WHERE f.id = foo.id AND f.stage = 'c'),'') `c`
FROM foo
[Bottom line]
I want a better fasten to do this
thanks
1 Answer