Solved

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

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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 explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

626 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