Code :
- CREATE TABLE test (
- ID SERIAL,
- POS varchar(1),
- VAL varchar(3)
- );
- INSERT INTO test(ID, POS, VAL) VALUES
- (1,'a','1,5'),
- (1,'b','2,5'),
- (1,'c','0'),
- (2,'a','1,4'),
- (2,'b','2,3');
- SELECT s.ID, a.POS as POS1, a.VAL as POS1, b.POS as POS2, b.VAL as POS2, c.POS as POS3, c.VAL as POS3
- FROM test s
- LEFT JOIN test a ON a.ID = s.ID AND a.POS = 'a'
- LEFT JOIN test b ON b.ID = s.ID AND b.POS = 'b'
- LEFT JOIN test c ON c.ID = s.ID AND c.POS = 'c'
- GROUP BY s.ID, a.POS, a.VAL, b.POS, b.VAL, c.POS, c.VAL
- ORDER BY s.ID
|