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
Solved

Oracle SQL Query

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

809 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