Note that there are some explanatory texts on larger screens.

plurals
  1. POI would like to combine ROLLUP with PIVOT - is that an option?
    primarykey
    data
    text
    <p>I have been using </p> <pre><code>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 </code></pre> <p>To transform:</p> <pre><code>[Author] [Status] Alan SUCCESS Bob FAILURE Bob SUCCESS Charles SUCCESS Dave FAILURE Dave DUPLICATE </code></pre> <p>TO:</p> <pre><code>[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 </code></pre> <p>I can get close to this output using a PIVOT statement, but I'm not sure how to get the TOTAL row/column?</p> <pre><code>SELECT * FROM (SELECT Author, status_id FROM tbl_Document) d PIVOT (COUNT(status_id) FOR status_id IN ([1],[3],[5],[6])) p </code></pre> <p>Gives:</p> <pre><code>[Author] [SUCCESS] [FAILURE] [DUPLICATE] Alan 1 0 0 Bob 1 1 0 Charles 1 0 0 Dave 0 1 1 </code></pre> <p>I'm guessing I need to put the ROLLUP into a subquery somewhere...?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload