Solved

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

Posted on 2007-04-09
5
173 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to recover a database from a user managed backup

743 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

13 Experts available now in Live!

Get 1:1 Help Now