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 9153311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:15:13+00:00 2026-06-17T12:15:13+00:00

In a nutshell I’m trying to group by a column but only if that

  • 0

In a nutshell I’m trying to group by a column but only if that column is not null or 0 in which case I still want to get those rows, but just not grouped. I have this table some_table:

+--------+----------+---------------------+-------+
| id     | other_id | date_value          | value |
+--------+----------+---------------------+-------+
| 1      | abc      | 2011-04-20 21:03:05 | 104   |
| 2      | abc      | 2011-04-20 21:03:04 | 229   |
| 3      | xyz      | 2011-04-20 21:03:03 | 130   |
| 4      | abc      | 2011-04-20 21:02:09 | 97    |
| 5      | 0        | 2011-04-20 21:02:08 | 65    |
| 6      | xyz      | 2011-04-20 21:02:07 | 101   |
| 7      |          | 2011-04-20 21:02:07 | 200   |
| 8      | 0        | 2011-04-20 21:02:07 | 201   |
| 9      |          | 2011-04-20 21:02:07 | 202   |
+--------+----------+---------------------+-------+

I wanted to select the rows and group by other_id, and also include a count of the grouped rows. This query works:

select id, other_id, date_value, value, cnt from
 (
   SELECT id, other_id, date_value, value, 
   ROW_NUMBER() OVER (partition by other_id order BY Date_Value desc) r,
   count(*) OVER (partition by other_id) cnt
   FROM some_table 
 )
where r = 1

The result is:

+--------+----------+---------------------+-------+-------+
| id     | other_id | date_value          | value | count |
+--------+----------+---------------------+-------+-------+
| 1      | abc      | 2011-04-20 21:03:05 | 104   | 3     |
| 3      | xyz      | 2011-04-20 21:03:03 | 130   | 2     |
| 5      | 0        | 2011-04-20 21:02:08 | 65    | 2     |
| 7      |          | 2011-04-20 21:02:07 | 200   | 2     |
+--------+----------+---------------------+-------+-------+

The problem is, I don’t want to group the rows that have other_id of null or 0. So the desired result is this:

+--------+----------+---------------------+-------+-------+
| id     | other_id | date_value          | value | count |
+--------+----------+---------------------+-------+-------+
| 1      | abc      | 2011-04-20 21:03:05 | 104   | 3     |
| 3      | xyz      | 2011-04-20 21:03:03 | 130   | 2     |
| 5      | 0        | 2011-04-20 21:02:08 | 65    | 1     |
| 7      |          | 2011-04-20 21:02:07 | 200   | 1     |
| 8      | 0        | 2011-04-20 21:02:07 | 201   | 1     |
| 9      |          | 2011-04-20 21:02:07 | 202   | 1     |
+--------+----------+---------------------+-------+-------+

As you can see, everything is grouped except when other_id is null or 0 in which case they are still selected, but not grouped. I am also trying to order by the date_value.

I have tried adding a WHERE clause in the OVER clause:

OVER (partition by other_id WHERE other_id IS NOT NULL AND other_id IS NOT '0' order BY Date_Value desc)
-- this produces an error: ORA-00907: missing right parenthesis

I have thought about doing a second select query and trying to stack the first results on top of the second, but I don’t think that would work because of the ordering.

Is there any way of grouping like this with a condition?

  • 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-17T12:15:14+00:00Added an answer on June 17, 2026 at 12:15 pm

    this will do it:

    SQL> select id, other_id, date_value, value, cnt from
      2   (
      3     SELECT id, other_id, date_value, value,
      4     case
      5       when other_id is null or other_id = '0' then 1
      6       else ROW_NUMBER() OVER (partition by other_id order BY Date_Value desc)
      7     end r,
      8     case
      9       when other_id is null or other_id = '0' then 1
     10       else count(*) OVER (partition by other_id)
     11     end cnt
     12     FROM some_table
     13   )
     14  where r = 1
     15  order by id;
    
            ID OTH DATE_VALUE               VALUE        CNT
    ---------- --- ------------------- ---------- ----------
             1 abc 2011-04-20 21:03:05        104          3
             3 xyz 2011-04-20 21:03:03        130          2
             5 0   2011-04-20 21:02:08         65          1
             7     2011-04-20 21:02:07        200          1
             8 0   2011-04-20 21:02:07        201          1
             9     2011-04-20 21:02:07        202          1
    
    6 rows selected.
    
    SQL>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a nutshell: Using jquery I want to get some xml output and use
In a nutshell, the problem is that I get a 401 Unauthorized error with
In a nutshell, i want a right div float to extend vertically 100% but
the problem in a nutshell: running plink(with specific arguments) works from cmd, but not
In a nutshell: I am trying to make a socket server to which clients
In a nutshell, I have a solution that builds fine in the IDE, and
In a nutshell, I want my JQuery UI Slider to look like this: Is
In a nutshell , I want to have different faces for some types of
Question in nutshell What is the best way to get Python and Java to
The problem in a nutshell is that in development mode we'd make changes to

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.