Solved

SQL query / procedure

Posted on 2011-03-23
4
415 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 32

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Encryption Decryption in Oracle 12 102
Oracle Subquery bad Join 11 59
Fastest way to replace data in Oracle 5 50
Problems using Provider=OraOLEDB.Oracle via VBScript/Classic ASP 5 44
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

929 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

14 Experts available now in Live!

Get 1:1 Help Now