Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Oracle SQL Query

Posted on 2014-10-07
2
Medium Priority
?
311 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
2 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

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 …
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
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…
This video shows how to recover a database from a user managed backup

877 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