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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:12:11+00:00 2026-05-18T04:12:11+00:00

I have been using SELECT Author, ISNULL(MAX(CASE Status WHEN ‘Duplicate’ THEN NumDocs END),”) AS

  • 0

I have been using

SELECT
    Author,
    ISNULL(MAX(CASE Status WHEN 'Duplicate' THEN NumDocs END),'') AS Duplicate,
    ISNULL(MAX(CASE Status WHEN 'Failure' THEN NumDocs END),'') AS Failure,
    ISNULL(MAX(CASE Status WHEN 'Rejected' THEN NumDocs END),'') AS Rejected,
    ISNULL(MAX(CASE Status WHEN 'Success' THEN NumDocs END),'') AS Success,
    ISNULL(MAX(CASE Status WHEN 'TOTAL' THEN NumDocs END),'') AS TOTAL
FROM    
    (SELECT
        CASE WHEN (GROUPING(Author)=1) THEN 'ALL'
            ELSE ISNULL(Author,'UNKNOWN') END AS Author,
        CASE WHEN (GROUPING(Status )=1) THEN 'TOTAL'
            ELSE ISNULL(Status ,'UNKNOWN') END AS [Status],
        COUNT(Status) AS NumDocs
    FROM 
        tbl_Document D
    LEFT JOIN
        tbl_Status S
            ON
                D.status_id = S.status_id   
    GROUP BY
        Author,
        Status
    WITH ROLLUP) BASE
GROUP BY 
    Author

To transform:

[Author]  [Status]
Alan      SUCCESS
Bob       FAILURE
Bob       SUCCESS
Charles   SUCCESS
Dave      FAILURE
Dave      DUPLICATE

TO:

[Author] [SUCCESS] [FAILURE] [DUPLICATE] [TOTALS]
Alan        1         0           0         1
Bob         1         1           0         2
Charles     1         0           0         1
Dave        0         1           1         2
TOTAL       3         2           1         6

I can get close to this output using a PIVOT statement, but I’m not sure how to get the TOTAL row/column?

SELECT
  * 
FROM 
  (SELECT Author, status_id FROM tbl_Document) d
PIVOT
  (COUNT(status_id) FOR status_id IN ([1],[3],[5],[6])) p

Gives:

[Author] [SUCCESS] [FAILURE] [DUPLICATE] 
Alan        1         0           0      
Bob         1         1           0      
Charles     1         0           0      
Dave        0         1           1     

I’m guessing I need to put the ROLLUP into a subquery somewhere…?

  • 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-18T04:12:12+00:00Added an answer on May 18, 2026 at 4:12 am

    You didn’t post the table schema, so I tried to infer it. I started with the input you gave (see the comment in the innermost SELECT), so you should be able to adapt this to your actual schema. I included an extra author without any documents, because I figured you’d want to see those in the final report output. It’s trivial to exclude those authors.

    DECLARE @Status table
    (
        Id int NOT NULL,
        Status nvarchar(50) NOT NULL
    )
    
    DECLARE @Authors table
    (
        Id int NOT NULL,
        Name nvarchar(50) NOT NULL
    )
    
    DECLARE @Documents table
    (
        Id int NOT NULL,
        AuthorId int NOT NULL,
        StatusId int NOT NULL
    )
    
    INSERT INTO @Status VALUES (1, 'Duplicate')
    INSERT INTO @Status VALUES (2, 'Failure')
    INSERT INTO @Status VALUES (3, 'Rejected')
    INSERT INTO @Status VALUES (4, 'Success')
    
    INSERT INTO @Authors VALUES (1, 'Alan')
    INSERT INTO @Authors VALUES (2, 'Bob')
    INSERT INTO @Authors VALUES (3, 'Charles')
    INSERT INTO @Authors VALUES (4, 'Dave')
    INSERT INTO @Authors VALUES (5, 'Tom') -- Test for authors without documents
    
    INSERT INTO @Documents VALUES (1, 1, 4)
    INSERT INTO @Documents VALUES (2, 2, 2)
    INSERT INTO @Documents VALUES (3, 2, 4)
    INSERT INTO @Documents VALUES (4, 3, 4)
    INSERT INTO @Documents VALUES (5, 4, 2)
    INSERT INTO @Documents VALUES (6, 4, 1)
    
    SELECT
        (CASE WHEN GROUPING(Name) = 1 THEN 'Total' ELSE Name END) AS Author,
        SUM(Duplicate) AS Duplicate,
        SUM(Failure) AS Failure,
        SUM(Rejected) AS Rejected,
        SUM(Success) AS Success,
        SUM(Duplicate + Failure + Rejected + Success) AS Total
        FROM
        (
            SELECT
                Name,
                (CASE WHEN Status = 'Duplicate' THEN 1 ELSE 0 END) AS Duplicate,
                (CASE WHEN Status = 'Failure' THEN 1 ELSE 0 END) AS Failure,
                (CASE WHEN Status = 'Rejected' THEN 1 ELSE 0 END) AS Rejected,
                (CASE WHEN Status = 'Success' THEN 1 ELSE 0 END) AS Success
                FROM
                (
                    -- Original input
                    SELECT
                        a.Name,
                        s.Status
                        FROM @Authors a
                        LEFT OUTER JOIN @Documents d ON d.AuthorId = a.Id
                        LEFT OUTER JOIN @Status s ON d.StatusId = s.Id
                ) i
        ) j
        GROUP BY Name WITH ROLLUP
    

    Output:

    Author   Duplicate  Failure  Rejected  Success  Total
    Alan     0          0        0         1        1
    Bob      0          1        0         1        2
    Charles  0          0        0         1        1
    Dave     1          1        0         0        2
    Tom      0          0        0         0        0
    Total    1          2        0         3        6
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.