如何以某种方式转换SQL Server查询输出?

时间:2014-03-13 02:30:02

标签: sql-server

我有一个产生以下输出的查询:

HostName    ServiceName Name        count
NULL        NULL        Received    24
canws82     MyBJE       Allocated   4
canws82     MyBJE       Scheduled   3
canws82     MyBJE       Running     3
NCBJE1      MyBJE       Running     3
NCBJE3      MyBJE       Running     3
NCBJE9      MyBJE       Allocated   37
NCBJE9      MyBJE       Scheduled   3
NCBJE9      MyBJE       Running     3

现在我陷入困境,试图弄清楚如何将此输出转换为

HostName    ServiceName Received    Allocated   Scheduled   Running
NULL        NULL        24          0           0           0
canws82     MyBJE       0           4           3           3
NCBJE1      MyBJE       0           0           0           3
NCBJE3      MyBJE       0           0           0           3
NCBJE9      MyBJE       0           37          3           3

我甚至不知道这个转换的名称,所以如果有人请你随意编辑标题(并删除这句话: - ))。

有什么想法吗?

P.S。

我正在使用SQL Server 2008R2。

修改

感谢Monty Wild我现在知道它是PIVOT我追求的。所以,我想出了以下代码:

DECLARE @Tmp TABLE (HostName NVARCHAR(32), ServiceName NVARCHAR(32), Name NVARCHAR(32), count INT)
INSERT INTO @Tmp (HostName, ServiceName, Name, count) VALUES
(NULL        ,NULL        ,'Received'    ,24),
('canws82'     ,'MyBJE'       ,'Allocated'   ,4),
('canws82'     ,'MyBJE'       ,'Scheduled'   ,3),
('canws82'     ,'MyBJE'       ,'Running'     ,3),
('NCBJE1'      ,'MyBJE'       ,'Running'     ,3),
('NCBJE3'      ,'MyBJE'       ,'Running'     ,3),
('NCBJE9'      ,'MyBJE'       ,'Allocated'   ,37),
('NCBJE9'      ,'MyBJE'       ,'Scheduled'   ,3),
('NCBJE9'      ,'MyBJE'       ,'Running'     ,3)

;WITH pivoted AS ( 
    SELECT HostName, ServiceName, Received, Allocated, Scheduled, Running FROM @Tmp
    PIVOT (SUM(count) FOR Name IN (Received, Allocated, Scheduled, Running)) as pvt
)
SELECT HostName, ServiceName, SUM(ISNULL(Received, 0)) Received, SUM(ISNULL(Allocated, 0)) Allocated, SUM(ISNULL(Scheduled, 0)) Scheduled, SUM(ISNULL(Running, 0)) Running
FROM pivoted
GROUP BY HostName, ServiceName

它确实产生了预期的结果。但是,我强烈认为我的代码不是最好的解决方案。我相信有更好的方法。

编辑2

我真正的SQL代码有点不同。这是:

;WITH raw AS (
    SELECT AllocatedAgentHostName, AllocatedAgentServiceName, Status, COUNT(1) count
    FROM BackgroundJobWork bjw
    WHERE Status < 100
    GROUP BY AllocatedAgentHostName,AllocatedAgentServiceName,Status
), data AS (
    SELECT raw.*, wsn.Name
    FROM raw
    JOIN WorkStatusName wsn ON raw.Status = wsn.Id
), pivoted AS ( 
    SELECT AllocatedAgentHostName, AllocatedAgentServiceName, Received, Allocated, Scheduled, Running FROM data
    PIVOT (SUM(count) FOR Name IN (Received, Allocated, Scheduled, Running)) as pvt
)
SELECT * FROM pivoted
ORDER BY AllocatedAgentHostName, AllocatedAgentServiceName

运行此查询会生成以下输出:

AllocatedAgentHostName  AllocatedAgentServiceName Received  Allocated Scheduled Running
NULL                    NULL                      24        NULL      NULL      NULL
canws82                 MyBJE                     NULL      4         NULL      NULL
canws82                 MyBJE                     NULL      NULL      3         NULL
canws82                 MyBJE                     NULL      NULL      NULL      3
NCBJE1                  MyBJE                     NULL      NULL      NULL      3
NCBJE3                  MyBJE                     NULL      NULL      NULL      3
NCBJE9                  MyBJE                     NULL      37        NULL      NULL
NCBJE9                  MyBJE                     NULL      NULL      3         NULL
NCBJE9                  MyBJE                     NULL      NULL      NULL      3

我不知道它为什么会产生阶梯效应,这在@Tmp表的设计示例中不会发生。

编辑3

好的,我找到了楼梯的原因(尽管我无法解释为什么它是原因)。我的真实代码data有一个额外的列 - Status,未被选入pivoted。不知何故,这会导致楼梯。

1 个答案:

答案 0 :(得分:1)

您正在寻找的术语是&#34; Pivot&#34;。看看the TechNet Using PIVOT and UNPIVOT page,如果您仍然无法理解,有人可能会进一步提供帮助。