mysql - Concise syntax on a single table to collapse columns using UNION ALL -
table:
+--------+-------------+-------------+ | time | type_x | type_y | |--------|-------------|-------------| | t1 | val_x_1 | val_y_1 | | t2 | val_x_2 | val_y_2 | +--------v-------------v-------------+ desired output:
+--------+-------------+-------------+ | time | type | val | |--------|-------------|-------------| | t1 | type_x | val_x_1 | | t2 | type_x | val_x_2 | | t1 | type_y | val_y_1 | | t2 | type_y | val_y_2 | +--------v-------------v-------------+ what came uses union all (which works)
(select `time`, 'type_x' type, `type_x` val t ...) union (select `time`, 'type_y' type, `type_y` val t ...) however, repeated where clauses , size of query problem deal more fields. i'm trying concise query. me out here?
select time, if (temp.row_n=1,t.type_x,t.type_y) type, if (temp.row_n=1,t.type_x,t.type_y) val t, (select 1 row_n union select 2 row_n) temp
Comments
Post a Comment