SQL Pivot如何用于矩阵视图和多表

时间:2016-09-13 08:47:52

标签: sql-server pivot

问题:我有2个表格,如下所示,我想从中查看矩阵视图

  1. WorkflowStatus(状态编号可由用户定义)
  2. ID    |   Name  
    ============== 
    1     |   Draft  
    2     |   Submitted
    3     |   Approved
    4     |   Rejected
    5     |   Closed
    6     |   ...
    
    1. WorkflowMatrix
    2. OldStatus     |  NextStatus  
      ==========================  
      1 (Draft)     |  2 (Submitted)
      1 (Draft)     |  3 (Approved)
      2 (Submitted) |  4 (Rejected)
      2 (Submitted) |  3 (Approved)  
      4 (Rejected)  |  2 (Submitted)
      3 (Approved)  |  5 (Closed)
      

      问题:如何在SQL中使用Pivot命令查看结果如下

                      |  Draft  | Submitted  |  Approved |  Rejected |  Closed
         =========================================================================
          1 Draft     |         |  true      |  true     |           |
          2 Submitted |         |            |  true     |   true    |
          3 Approved  |         |            |           |           |   true
          4 Rejected  |         |  true      |           |           |
          5 Closed    |         |            |           |           |
      

      非常感谢你的帮助。

3 个答案:

答案 0 :(得分:2)

这是一个令人困惑的Pivot

SELECT id, 
       NAME, 
       Max(CASE WHEN nextstatus = 1 THEN 'True' ELSE '' END) [Draft], 
       Max(CASE WHEN nextstatus = 2 THEN 'True' ELSE '' END) [Submitted], 
       Max(CASE WHEN nextstatus = 3 THEN 'True' ELSE '' END) [Approved], 
       Max(CASE WHEN nextstatus = 4 THEN 'True' ELSE '' END) [Rejected], 
       Max(CASE WHEN nextstatus = 5 THEN 'True' ELSE '' END) [Closed] 
FROM   WorkflowStatus  a 
       LEFT JOIN WorkflowMatrix b 
              ON a.id = b.oldstatus 
GROUP  BY id, 
          NAME 
ORDER  BY id 

答案 1 :(得分:1)

Pivot version.

SELECT  id,
        name,
        IIF([1] IS NULL, '', 'true') AS [Draft],
        IIF([2] IS NULL, '', 'true') AS [Submitted],
        IIF([3] IS NULL, '', 'true') AS [Approved],
        IIF([4] IS NULL, '', 'true') AS [Rejected],
        IIF([5] IS NULL, '', 'true') AS [Closed]
FROM    WorkflowStatus  a 
        LEFT JOIN WorkflowMatrix b 
              ON a.id = b.oldstatus 
PIVOT (
    MAX(NextStatus)
    FOR NextStatus IN ([1],[2],[3],[4],[5])
) p

答案 2 :(得分:1)

你可以尝试这个,但在使用枢轴表

上的GROUP BY时仍然遇到问题
DECLARE @cols AS NVARCHAR(MAX),
  @query  AS NVARCHAR(MAX);

select @cols = STUFF((SELECT ',' + QUOTENAME(id) 
                from #WorkflowStatus 
                group by id, Name
                order by id
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'');

set @query = '
WITH Pivoted
AS
(
SELECT OldStatus, '+ @cols +' FROM
         #WorkflowStatus  a
         LEFT JOIN #WorkflowMatrix b 
          ON a.id = b.NextStatus
        PIVOT
        (
            COUNT(NextStatus)
            for NextStatus in (' + @cols + ')
        ) p
)    
SELECT OldStatus,'+@cols+'
FROM Pivoted a
WHERE OldStatus IS NOT NULL
GROUP BY OldStatus,'+ @cols +'
ORDER BY OldStatus ASC
';

exec sp_executesql @query;

我希望我能提供帮助。 这是the SQL