Solved

Oracle SQL Query

Posted on 2014-10-07
2
292 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 73

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

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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

759 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