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

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -