How to get CountDistinct for Subtotal and grandtotal

Eshwar 216 Reputation points
2021-11-11T17:39:35.323+00:00

Hi,
I need to get count distinct for each group member and all group member

Input:
SELECT *FROM
(
SELECT 1 USRID,100 CUSTOMERID,200.03 BALANCE
UNION ALL
SELECT 1,200,300.03
UNION ALL
SELECT 1,300,400.03
UNION ALL
SELECT 1,400,500.03
UNION ALL
SELECT 1,400,500.03
UNION ALL
SELECT 1,500,600.03
UNION ALL
SELECT 2,200,300.03
UNION ALL
SELECT 2,300,400.03
UNION ALL
SELECT 2,700,500.03
UNION ALL
SELECT 2,800,700.03
UNION ALL
SELECT 2,800,900.03
) ET
ORDER BY USRID,CUSTOMERID

Expected output:
148623-image.png

If I do the CountDistinct I am getting below but I need to get sum of distinct count of all groups which is 5+4 = 9:
148576-image.png

Please help!

Thanks,
Eshwar

SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
3,064 questions
0 comments No comments
{count} votes

Accepted answer
  1. Joyzhao-MSFT 15,636 Reputation points
    2021-11-12T06:35:31.433+00:00

    Hi @Eshwar
    Based on the usage of COUNTDISTINCT, I propose my solution here:

    =SUM(COUNTDISTINCT(Fields!CUSTOMERID.Value,"USRID"))  
    

    As shown below:
    148766-01.jpg
    148749-02.jpg
    Hope this helps.
    Best Regards,
    Joy


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Joyzhao-MSFT 15,636 Reputation points
    2021-11-12T05:22:15.42+00:00

    Hi @Eshwar
    Count distinct is a measure (aggregate function). When the measure is used in the total row, it is not summing the distinct count values about but is calculating the function in the context of all the table data.
    When you use =Sum(CountDistinct(Fields!CUSTOMERID.Value)) or "add total", you will calculate the number of all non-coincident data in the [CUSTOMERID] field, So you will get "7" in the total.

    Best Regards,
    Joy

    1 person found this answer helpful.
    0 comments No comments

  2. Eshwar 216 Reputation points
    2021-11-12T04:58:44.277+00:00

    I got the answer.
    using "CountDistinct(Fields!CUSTOMERID.Value + Fields!USRID.Value)" gave me required results
    Regards,
    Eshwar.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.