Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Query to capture last three months data

2960347Jun 24 2015 — edited Jun 25 2015

Hi All ,

I have a table with below ddl

CREATE TABLE test  (

          "CLIENT_NAME" VARCHAR(150) ,

          "SUMMARY" DECIMAL(25,2) ,

          "JAN" DECIMAL(25,2) ,

          "FEB" DECIMAL(25,2) ,

          "MAR" DECIMAL(25,2) ,

          "APR" DECIMAL(25,2) ,

          "MAY" DECIMAL(25,2) ,

          "JUN" DECIMAL(25,2) ,

          "JUL" DECIMAL(25,2) ,

          "AUG" DECIMAL(25,2) ,

          "SEP" DECIMAL(25,2) ,

          "OCT" DECIMAL(25,2) ,

          "NOV" DECIMAL(25,2) ,

The source for this table is a excel sheet which we load as required. Now problem is that in excel we have the data till latest month..so currently table will have data for jan till june.

From july-december will be blank in excel hence same in table.

Important Point :So next year suppose in 2016 jan, we will have again data only for jan and data for all other fields will be empty.

Now i have a different table(fact table having client_name as common column) where i use above ddl table as join(based on client_name) to get value for the latest month...in this case it wil be june month as table is having maximum column as june...

Now i need one more calculation which will give me data against a particular client for last three months....so at present it should give me data for april,may and june for a particular client.

example :-

client_name april may june

xyz              10   20   30

next month it should give me data like

client_name may   june  july

xyz              20      30   40

I am not sure that we need to create pivot view please suggest me  sql which will give me above requirement i.e last three months data (with current month starting as latest running month)

Also how do we keep 'Important Point' case in mind .

Any help will be appreciated,as i am feeling totally blank on this now.

Many thanks!!

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 23 2015
Added on Jun 24 2015
9 comments
1,476 views