|
我一直在用2 v0 L# F+ f% `, n3 A+ ~5 Z
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 TOTALFROM (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) BASEGROUP BY Author转换:' z# J) M I0 F9 [8 }2 t
[Author] [Status]Alan SUCCESSBob FAILUREBob SUCCESSCharles SUCCESSDave FAILUREDave DUPLICATE到:
0 D% q: Z6 K0 Z5 s' F/ _+ i/ ^[Author] [SUCCESS] [FAILURE] [DUPLICATE] [TOTALS]Alan Bob 2222 Charles 1 Dave 22 TOTAL 3 6 我可以使用PIVOT语句接近这个输出,但我不确定如何获得TOTAL行/列?
. a1 s7 ]" o! M8 q; P* S5 ]( kSELECT * FROM (SELECT Author,status_id FROM tbl_Document) dPIVOT (COUNT(status_id) FOR status_id IN p给出:
* T" R; ?6 \" p6 r3 \[Author] [SUCCESS] [FAILURE] [DUPLICATE] Alan Bob Charles Dave 我猜我需要将军ROLLUP放在子查询中的某个地方…?. v9 z) [9 \5 R
2 R- v$ S& B* t$ H9 O 解决方案:
5 P$ t8 B" u; Y7 V+ `$ q& s7 h 你没有发布表架构,所以我试着推断它。我从您提供的输入开始(请参考最里面的注释)SELECT),因此,您应该能够将其调整为实际模式。我添加了一个没有任何文档的作者,因为我认为你想在最终报告的输出中看到这些作者。排除这些作者是微不足道的。7 b* G/ B8 z4 T* @! f& A4 C9 l1 r
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 documentsINSERT 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输出:! ?4 l& t8 I6 U4 O' `( Y2 O
Author Duplicate Failure Rejected Success TotalAlan Bob 222 Charles Dave 22 Tom Total |
|