Solved

SQL query / procedure

Posted on 2011-03-23
4
414 Views
Last Modified: 2013-12-07
Hi,

Have a table called tbl1 like :

UPDATEDATETIME       dBDate               HFCol      HVCol
3/10/2011 13:06:53       6/10/2011      10369.8      0.1979
3/10/2011 13:06:53       3/11/2011      10440      0.2059
3/10/2011 13:07:03       6/10/2011      10369.8      0.1976
3/10/2011 13:07:03       3/11/2011      10440      0.2036
3/10/2011 13:07:13       6/10/2011      10369.8      0.1975
3/10/2011 13:07:13       3/11/2011      10440      0.2038

and struggling to get sql/sotre proc to produce following results:

UPDATEDATETIME      HFCol_JUN11      HFCol_MAR11       HVCol_JUN11       HVCol_MAR11
3/10/2011 13:06:53      10369.8             10440               0.1979               0.2059
3/10/2011 13:07:03      10369.8             10440               0.1976               0.2036
3/10/2011 13:07:13      10369.8              10440               0.1975               0.2038

[As you can see new columns in result are old columns and month and year from dbDate is appended. For eg. HFCol for dbDate 03/11/2011 becomes HFCOL_MAR11 etc.]
                        
Thanks,

0
Comment
Question by:punit
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35204637
it's a pivot table ... must it be fully dynamic, or are you happy with those 2x2 columns?
0
 

Author Comment

by:punit
ID: 35204643
hi angelll,
it should be dynamic.
One more note, as you can see what I'm trying to do is trying to put data for same UPDATEDATETIME  into one row.

Thanks
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35204909
could the data be over 1 year? how to represent that?
also, could there be more than 1 row per "month" ?

let's assume for the moment that it was all from 1 year
select UPDATEDATETIME       
  , max(case when month(dBDate) = 1 then  HFCol else null end) HFCol_Jan
  , max(case when month(dBDate) = 1 then  HVCol else null end) HVCol_Jan
  , max(case when month(dBDate) = 2 then  HFCol else null end) HFCol_Feb
  , max(case when month(dBDate) = 2 then  HVCol else null end) HVCol_Feb
  --- etc for the other month
 from tbl1 
group by UPDATEDATETIME

Open in new window

0
 
LVL 31

Expert Comment

by:awking00
ID: 35235074
See attached.
query.txt
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
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.
Via a live example, show how to take different types of Oracle backups using RMAN.

760 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