0

I have got next table:

date            3 6 9 12 15 18 21 24     
2015.01.01      3 2 5  2  5  7  2  4
2015.01.02      9 3 1  3  4  6  4  1
2015.01.03      0 2 3  1  5  6  3  4

I need to get table like:

date                  time          numbers
2015.01.01            03:00:00       3
2015.01.01            06:00:00       2
2015.01.01            09:00:00       5
2015.01.01            12:00:00       2
2015.01.01            15:00:00       5
2015.01.01            18:00:00       7
2015.01.01            21:00:00       2
2015.01.01            24:00:00       4
2015.01.02            03:00:00       9
2015.01.02            06:00:00       3

How I can do it?

1
  • 1
    Add the columnnames of original table Commented Mar 10, 2015 at 18:47

1 Answer 1

1

Here's one approach with union all:

select `date`, '3:00:00' as `time`, `3` as val from yourtable
union all select `date`, '6:00:00' as `time`, `6` as val from yourtable
union all select `date`, '9:00:00' as `time`, `9` as val from yourtable
...

BTW, I'd highly recommend renaming your columns...


Here's another approach using conditional aggregation and a cross join:

select `date`, val as `time`, 
  max(case when val = 3 then `3`
           when val = 6 then `6`
           when val = 9 then `9`
      end) as `val`
from yourtable cross join (select 3 val union all select 6 union all select 9) t
group by `date`, val
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.