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

  • Home
  • SEARCH
  • 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 8434611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:42:14+00:00 2026-06-10T06:42:14+00:00

My question has been addressed previously but I can’t seem to apply any solution

  • 0

My question has been addressed previously but I can’t seem to apply any solution to my query to make it work. Would very much appreciate some guidance.

My current query below returns this data set:

  |  Age      |   Count   |
     0-1 day      300
     2-3 days     6000
     3-4 days     100 
SELECT(CASE WHEN time_dtm > SYSDATE -1 THEN '0-1 day'
        WHEN time_dtm > SYSDATE -2 AND time_dtm < SYSDATE -1 THEN '1-2 days'
        WHEN time_dtm > SYSDATE -3 AND time_dtm < SYSDATE -2 THEN '2-3 days'
        WHEN time_dtm > SYSDATE -4 AND time_dtm < SYSDATE -3 THEN '3-4 days'
        WHEN time_dtm > SYSDATE -5 AND time_dtm < SYSDATE -4 THEN 'Closed'
        END) AS Age, 
    COUNT( * ) AS "Count" 
FROM table_1 
WHERE id IN (1,2,3)
GROUP BY (CASE WHEN time_dtm > SYSDATE -1 THEN '0-1 day'
        WHEN time_dtm > SYSDATE -2 AND time_dtm < SYSDATE -1 THEN '1-2 days'
        WHEN time_dtm > SYSDATE -3 AND time_dtm < SYSDATE -2 THEN '2-3 days'
        WHEN time_dtm > SYSDATE -4 AND time_dtm < SYSDATE -3 THEN '3-4 days'
        WHEN time_dtm > SYSDATE -5 AND time_dtm < SYSDATE -4 THEN 'Closed'
        END)
ORDER BY (CASE WHEN time_dtm > SYSDATE -1 THEN '0-1 day'
        WHEN time_dtm > SYSDATE -2 AND time_dtm < SYSDATE -1 THEN '1-2 days'
        WHEN time_dtm > SYSDATE -3 AND time_dtm < SYSDATE -2 THEN '2-3 days'
        WHEN time_dtm > SYSDATE -4 AND time_dtm < SYSDATE -3 THEN '3-4 days'
        WHEN time_dtm > SYSDATE -5 AND time_dtm < SYSDATE -4 THEN 'Closed'
        END)

However, I would like it to show the zero/null rows as zeros like this:

  |  Age      |   Count   |
     0-1 day      300
     1-2 days     0
     2-3 days     6000
     3-4 days     100
     Closed       0

I’ve read all sorts from the past couple of days re: NVL, COALESCE, FULL/LEFT/RIGHT OUTER JOIN, LEFT/RIGHT JOINS, UNION ALL etc none of which had CASE statements and tried to work around it myself BUT! You have to know when to stop and ask for directions.

  • 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-06-10T06:42:16+00:00Added an answer on June 10, 2026 at 6:42 am

    First off, re-write your query. Use views or common table expression to avoid repeating yourself three times for your SELECT, GROUP BY, ORDER BY clauses. Your query becomes:

    WITH data AS (
        SELECT(CASE WHEN time_dtm > SYSDATE -1 THEN '0-1 day'
                    WHEN time_dtm > SYSDATE -2 AND 
                         time_dtm < SYSDATE -1 THEN '1-2 days'
                    WHEN time_dtm > SYSDATE -3 AND 
                         time_dtm < SYSDATE -2 THEN '2-3 days'
                    WHEN time_dtm > SYSDATE -4 AND 
                         time_dtm < SYSDATE -3 THEN '3-4 days'
                    WHEN time_dtm > SYSDATE -5 AND 
                         time_dtm < SYSDATE -4 THEN 'Closed'
               END) AS Age 
        FROM table_1 
        WHERE id IN (1,2,3)
    )
    SELECT Age, COUNT(*)
    FROM data
    GROUP BY Age
    ORDER BY Age
    

    Then, in order to be sure that any of your desired groups will be available in the result, you have lots of options.

    You could use UNION ALL:

    WITH data AS (
        SELECT(CASE WHEN time_dtm > SYSDATE -1 THEN '0-1 day'
                    WHEN time_dtm > SYSDATE -2 AND 
                         time_dtm < SYSDATE -1 THEN '1-2 days'
                    WHEN time_dtm > SYSDATE -3 AND 
                         time_dtm < SYSDATE -2 THEN '2-3 days'
                    WHEN time_dtm > SYSDATE -4 AND 
                         time_dtm < SYSDATE -3 THEN '3-4 days'
                    WHEN time_dtm > SYSDATE -5 AND 
                         time_dtm < SYSDATE -4 THEN 'Closed'
               END) AS Age 
        FROM table_1 
        WHERE id IN (1,2,3)
    
        -- The below will add one record for every desired Age group
        UNION ALL
        SELECT '0-1 day'  FROM DUAL UNION ALL
        SELECT '1-2 days' FROM DUAL UNION ALL
        SELECT '2-3 days' FROM DUAL UNION ALL
        SELECT '3-4 days' FROM DUAL UNION ALL
        SELECT 'Closed'   FROM DUAL
    )
    SELECT Age, COUNT(*) - 1 -- Subtract the extra record again
    FROM data
    GROUP BY Age
    ORDER BY Age
    

    An entirely different solution would involve LEFT OUTER JOINs:

    -- Groups is a dynamic table that contains the date ranges and their "Age" label
    WITH groups AS (
        SELECT SYSDATE -1 lower, SYSDATE upper, '0-1 day'  Age FROM DUAL UNION ALL
        SELECT SYSDATE -2      , SYSDATE -1   , '1-2 days'     FROM DUAL UNION ALL
        SELECT SYSDATE -3      , SYSDATE -2   , '2-3 days'     FROM DUAL UNION ALL
        SELECT SYSDATE -4      , SYSDATE -3   , '3-4 days'     FROM DUAL UNION ALL
        SELECT SYSDATE -5      , SYSDATE -4   , 'Closed'       FROM DUAL
    )
    SELECT g.Age, NVL(SUM(t.counter), 0)
    FROM groups g
    
    -- LEFT OUTER JOINing "table_1" to "groups" will ensure that every group
    -- appears at least once in the result
    LEFT OUTER JOIN (
      SELECT 1 counter, t.* FROM table_1 t WHERE t.id IN (1,2,3)
    ) t
    ON  t.time_dtm >= g.lower
    AND t.time_dtm <  g.upper
    GROUP BY g.Age
    ORDER BY g.Age
    

    In the second example, you could also do without a CTE and use a nested SELECT for the groups table. It is easy to see how the second example is simpler to evolve in the future, should your requirements change.

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

Sidebar

Related Questions

I'm sure that this question has been addressed in many best practices books, but
A similar question has been posted at but i could not find the solution
This question has already been addressed but I would like to see some PHP
I know this has been addressed many times, but i just cant seem to
I know this question has been asked before but I seem to have a
Please forgive a question that has been addressed in some form or fashion previously.
I'm sure this problem has been addressed before but I can't find a 'duplicate'.
Sort of a methods/best practices question here that I am sure has been addressed,
This question has been asked before but i still don't understand it fully so
This question has been bugging me for a long time now but essentially I'm

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.