Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-04-09
5
Medium Priority
?
212 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
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…

704 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