Solved

Query to place data in columns by date/time  from rows

Posted on 2007-04-09
5
183 Views
Last Modified: 2010-03-20
I have data as such

datetime                      ID      Value      
04.09.2007 14:00      23      1.2      
04.09.2007 14:00      27      4.8      
04.09.2007 14:00      30      9.2      
04.09.2007 15:00      23      1.3      
04.09.2007 15:00      27      4.9      
04.09.2007 15:00      30      9.1      
            
I would like to query out (display like this):                  

datetime                       23      27     30
04.09.2007 14:00      1.2      4.8      9.2
04.09.2007 15:00      1.3      4.9      9.1

What query can do this?
0
Comment
Question by:robjay
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18876587
0
 
LVL 16

Accepted Solution

by:
MohanKNair earned 500 total points
ID: 18876723
select datetime,
max(decode(id, 23,value,null)) col23,
max(decode(id, 27,value,null)) col27,
max(decode(id, 30,value,null)) col30
from TableA GROUP BY datetime;
0
 

Author Comment

by:robjay
ID: 18878187
Thanks Mohan - can't get that to work in an Oracle SQL session
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18878386
what error do you get?
0
 
LVL 3

Expert Comment

by:imaredia
ID: 18908583
The query given by Mohan works perfect for me. Here is what i did, let me know if it's still a problem.

CREATE TABLE t
(  id           number,
   datetime     date,
   VALUE        number(4,2)
)

INSERT INTO t(id, datetime, value) SELECT 23, to_date('04.09.2007 14:00', 'DD.MM.YYYY hh24:mi'), 1.2 FROM dual;

INSERT INTO t(id, datetime, value) SELECT 27, to_date('04.09.2007 14:00', 'DD.MM.YYYY hh24:mi'), 4.8 FROM dual;

INSERT INTO t(id, datetime, value) SELECT 30, to_date('04.09.2007 14:00', 'DD.MM.YYYY hh24:mi'), 9.2 FROM dual;

INSERT INTO t(id, datetime, value) SELECT 23, to_date('04.09.2007 15:00', 'DD.MM.YYYY hh24:mi'), 1.3 FROM dual;

INSERT INTO t(id, datetime, value) SELECT 27, to_date('04.09.2007 15:00', 'DD.MM.YYYY hh24:mi'), 4.9 FROM dual;

INSERT INTO t(id, datetime, value) SELECT 30, to_date('04.09.2007 15:00', 'DD.MM.YYYY hh24:mi'), 9.1 FROM dual;

COMMIT;

SELECT      datetime, MAX (DECODE (ID, 23, VALUE, NULL)) "23",
                   MAX (DECODE (ID, 27, VALUE, NULL)) "27",
                   MAX (DECODE (ID, 30, VALUE, NULL)) "30"
    FROM     t
GROUP BY datetime;
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now