Solved

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

Posted on 2007-04-09
5
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

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 143

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

752 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