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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:49:15+00:00 2026-05-18T00:49:15+00:00

Basically I’m trying to get a distinct count within this cubed result. But unfortuantly

  • 0

Basically I’m trying to get a distinct count within this cubed result. But unfortuantly you cannot use Count(distinct(Field)) with cube and rollup (as stated here)

Here is what the Data Looks Like. (This is just a simple example I do expect duplicates in the Data)

    Category1       Category2       ItemId
    a               b               1
    a               b               1
    a               a               1
    a               a               2
    a               c               1
    a               b               2
    a               b               3
    a               c               2
    a               a               1
    a               a               3
    a               c               4

Here is what I would like to do but it doesn’t work.

SELECT
  Category1,
  Category2,
  Count(Distinct(ItemId))
FROM ItemList IL
GROUP BY
  Category1,
  Category2
WITH CUBE

I know that I can do a sub select like this to get the results I want:

SELECT
  *,
  (SELECT
     Count(Distinct(ItemId)) 
   FROM ItemList IL2 
   WHERE 
     (Q1.Category1 IS NULL OR Q1.Category1 IS NOT NULL AND Q1.Category1 = IL2.Category1) 
     AND
     (Q1.Category2 IS NULL OR Q1.Category2 IS NOT NULL AND Q1.Category2 = IL2.Category2))
       AS DistinctCountOfItems 
FROM (SELECT
        Category1,
        Category2
      FROM ItemList IL
      GROUP BY
        Category1,
        Category2
      WITH CUBE) Q1

But this runs slow when the result set is large due to the sub-select. Is there any other way to get a Distinct Count from a cubed result?

This is the result I want to see

Category1     Category2    DistinctCountOfItems
a             a            3
a             b            3
a             c            3
a             NULL         4
NULL          NULL         4
NULL          a            3
NULL          b            3
NULL          c            3
  • 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-18T00:49:16+00:00Added an answer on May 18, 2026 at 12:49 am

    You should be able to clean up your “messy” answer like so:

    select Category1, Category2, count(distinct ItemId)
    from ItemList
    group by Category1, Category2
    UNION ALL
    select Category1, null, count(distinct ItemId)
    from ItemList
    group by Category1
    UNION ALL
    select null, Category2, count(distinct ItemId)
    from ItemList
    group by Category2
    UNION ALL
    select null, null, count(distinct ItemId)
    from ItemList
    

    Then the other option I came up with:

    select IL1.Category1, IL1.Category2, count(distinct ItemId)
    from ( 
      select Category1, Category2
      from ItemList
      group by Category1, Category2
      with cube
     ) IL1
     join ItemList IL2 on (IL1.Category1=IL2.Category1 and IL1.Category2=IL2.Category2)
          or (IL1.Category1 is null and IL1.Category2=IL2.Category2)
          or (IL1.Category2 is null and IL1.Category1=IL2.Category1)
          or (IL1.Category1 is null and IL1.Category2 is null)
    group by IL1.Category1, IL1.Category2
    

    The efficiency may vary based on the indexes, number of columns being grouped, etc. For the test table I wrote, the sub-select and join (as opposed to the Unions) was slightly better. I don’t have access to a MSSQL 2000 instance at the moment (I tested on a 2005 instance), but I don’t think anything here is invalid.

    UPDATE

    An even better option, especially if you’re grouping on more than 2 columns (If you’re grouping on 8 columns the above code would require 256 join clauses to catch all null combinations!):

    select IL1.Category1, IL1.Category2, count(distinct ItemId)
    from ( 
      select Category1, Category2
      from ItemList
      group by Category1, Category2
      with cube
     ) IL1
     inner join ItemList IL2 on isnull(IL1.Category1,IL2.Category1)=IL2.Category1
                      and isnull(IL1.Category2,IL2.Category2)=IL2.Category2
    group by IL1.Category1, IL1.Category2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

basically this is my question: How to query MongoDB with "like"? but i guess
Basically I'd be looking to implement a method like this. IQueryAble GetQuery<T>(Entities db) or
Basically, what I'm trying to create is a page of div tags, each has
Basically from a database I am getting data that is formatted like this nameofproject101
Basically I'm attempting to add rows to a table, I need to do this
Basically what I'm trying to do is make a very simple vertical bullet projectile
Basically this function is meant to store the height value of the element that
Basically what I am trying to achieve is check if user is logged in
Basically I'm trying to make a web coding tutorial site (awesome that i cant
Basically I'm trying to improve on the Ghosts in a Pacman game I'm making.

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.