Solved

Oracle SQL Query

Posted on 2014-10-07
2
302 Views
Last Modified: 2014-10-07
Hello,

I am attaching a file that shows how the sample data is stored in a table and what the expected results are from that table.

Basically, I need to be able to write a query that shows last 6 weeks trend for certain Action's. I need the First day of the week to be Sunday. For a certain Table name, if there are more data inserted within one week then it should get the max RUN_DT for that week and its related counts.

Could someone please help?
Thank you
SAMPLE-DATA.xls
0
Comment
Question by:angel7170
[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
2 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 40366238
you can't have dynamic columns in sql.
But, we can still have the data run for the most recent 6 weeks


  SELECT table_name,
         action_item,
         MAX(curr_week) week_ago_0,
         MAX(week_ago_1) week_ago_1,
         MAX(week_ago_2) week_ago_2,
         MAX(week_ago_3) week_ago_3,
         MAX(week_ago_4) week_ago_4,
         MAX(week_ago_5) week_ago_5
    FROM (SELECT run_dt,
                 table_name,
                 action_item,
                 val,
                 (NEXT_DAY(TRUNC(SYSDATE - 7), 'Sunday') - NEXT_DAY(TRUNC(run_dt - 7), 'Sunday')) / 7
                     run_wk
            FROM yourtable UNPIVOT (val
                           FOR action_item
                           IN (source_count,
                              target_count,
                              s_minus_t_count,
                              t_minus_s_count,
                              bwd_count))
           WHERE run_dt > NEXT_DAY(TRUNC(SYSDATE - 7), 'Sunday') - 35) PIVOT (MAX(val)
                                                                           FOR run_wk
                                                                           IN (0 curr_week,
                                                                              1 week_ago_1,
                                                                              2 week_ago_2,
                                                                              3 week_ago_3,
                                                                              4 week_ago_4,
                                                                              5 week_ago_5))
GROUP BY table_name, action_item
ORDER BY table_name, action_item;
0
 

Author Comment

by:angel7170
ID: 40366283
This works perfect as expected. Thank you very much
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle encryption 12 73
error in my cursor 5 64
Pass multiple values or string arrays in java as a parameter 3 73
Automate an Oracle update in Excel 7 70
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
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 explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

738 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