Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6829893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:33:14+00:00 2026-05-26T22:33:14+00:00

I have a bunch of queries that take data with a time stamp and

  • 0

I have a bunch of queries that take data with a time stamp and spit out SUMS based the last few weeks, months, and year to date. It looks like this

Week1    Sum for most recent week
Week2    Sum for second most recent week
WeekN    Sum for N most recent week
Jan-Dec  Sum for January-December
YTD      Sum for everything this year

This is how the query currently does this

SELECT TIME_PERIOD, INDEX, SUM(ITEM)   
FROM (SELECT
        INDEX ,
        (CASE  
            WHEN ACTIVITY_DAY>=(TO_DATE( :end_day,
            'yyyy-mm-dd' )-6)  
            AND ACTIVITY_DAY<=(TO_DATE( :end_day,
            'yyyy-mm-dd' )-0) THEN 'WEEK1'  
            WHEN ACTIVITY_DAY>=(TO_DATE( :end_day,
            'yyyy-mm-dd' )-13)  
            AND ACTIVITY_DAY<=(TO_DATE( :end_day,
            'yyyy-mm-dd' )-7) THEN 'WEEK2'  
            ELSE NULL  
        END) AS TIME_PERIOD,
        MAX(ITEMS) AS ITEM
    FROM
        SOURCE  
    GROUP BY
        INDEX ,
        DAY  
    UNION
    ALL SELECT
        INDEX ,
        (CASE  
            WHEN ACTIVITY_DAY>=TO_DATE( :year||'-01-01',
            'yyyy-mm-dd' )  
            AND ACTIVITY_DAY<=TO_DATE( :year||'-01-31',
            'yyyy-mm-dd' ) THEN 'Jan'  
            WHEN ACTIVITY_DAY>=TO_DATE( :year||'-02-01',
            'yyyy-mm-dd' )  
            AND ACTIVITY_DAY<TO_DATE( :year||'-03-01',
            'yyyy-mm-dd' ) THEN 'Feb'   
            ELSE NULL  
        END) AS TIME_PERIOD ,
        MAX(ITEMS) AS ITEM
    FROM
        SOURCE  
    GROUP BY
        INDEX ,
        DAY  
    UNION
    ALL SELECT
        INDEX ,
        (CASE  
            WHEN ACTIVITY_DAY>=TO_DATE( :year||'-01-01',
            'yyyy-mm-dd' )  
            AND ACTIVITY_DAY<=TO_DATE( :end_day,
            'yyyy-mm-dd' ) THEN 'YTD'  
            ELSE NULL  
        END) AS TIME_PERIOD,
        MAX(ITEMS) AS ITEM
    FROM
        SOURCE  
    GROUP BY
        INDEX ,
        DAY)
GROUP BY INDEX, TIME_PERIOD  

Is there a better way in Oracle?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:33:15+00:00Added an answer on May 26, 2026 at 10:33 pm

    I think you are looking for something like this:

    with data as
    (
        select sysdate - floor(dbms_random.value(1,400)) dt, floor(dbms_random.value(1,100)) val
        from dual
        connect by level <= 100
    )
    select
        time_period,
        sum(val) period_sum
    from
    (
        select -- weeks
            'Week'||(to_char(sysdate, 'WW') - to_char(dt, 'WW') + 1) time_period,
            val,
            (to_char(sysdate, 'WW') - to_char(dt, 'WW') + 1) ord
        from data
        where dt >= trunc(sysdate,'YY')
        union all
        select -- months
            to_char(dt, 'Mon') time_period,
            val,
            100+to_char(dt,'MM') ord
        from data
        where dt >= trunc(sysdate,'YY')
        union all
        select -- months
            'YTD' time_period,
            val,
            200
        from data
        where dt >= trunc(sysdate,'YY')
    )
    group by
        time_period, ord
    order by
        ord;
    

    Note that you won’t need the WITH block, I was just using it to create some dummy data. The Ord column might be unnecessary for you, I was just using it to order the data in a logical fashion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# console application that makes a bunch of queries to a
I have been developing an iPhone app which queries a server that relays data
So for this one project, we have a bunch of queries that are executed
We have a whole bunch of queries that search for clients, customers, etc. You
I have a bunch of SQL queries that I have created in Excel by
I have a bunch of mysql queries in my sight that Im going to
We have a database that has a bunch of records with some bad data
I have a bunch of seperate little select queries that I need to put
I have a java program that runs a bunch of queries against an sql
I have some GUI on a bunch of LINQ queries. The queries take some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.