如何组合多个表并将行转换为SQL Server中的列

时间:2016-02-02 08:08:49

标签: sql-server

我有这个3数据库表

tblSites
| Sites | SiteName |
   AA     AaaaaaaA
   BB     BaaaaaaB
   CC     CaaaaaaC
   DD     DaaaaaaD

tblWeb
| WebID | AppName |    AppUrl    | ServerName | 
    1      aWeb     www.aWeb.com    ServerA
    1      bWeb     www.bWeb.com    ServerA
    1      cWeb     www.cWeb.com    ServerB
    1      dWeb     www.dWeb.com    ServerA
    1      eWeb     www.eWeb.com    ServerC
    1      fWeb     www.fWeb.com    ServerC
    1      gWeb     www.gWeb.com    ServerD
    1      hWeb     www.hWeb.com    ServerD

tblWebServices
| Sites | WebID | SummaryState |       Last_Check       | 
    A       1         OK         02/01/2016 10:00:00.000
    A       1       Critical     02/01/2016 10:00:04.000
    A       2         OK         02/01/2016 10:00:04.000
    A       2       Critical     02/01/2016 10:00:06.000
    A       3         OK         02/01/2016 10:00:07.000
    A       3         OK         02/01/2016 10:00:09.000
    A       4         OK         02/01/2016 10:00:10.000
    A       4         OK         02/01/2016 10:00:12.000
    A       5       Critical     02/01/2016 10:00:14.000
    A       5         OK         02/01/2016 10:00:17.000
    A       6         OK         02/01/2016 10:00:20.000
    A       6         OK         02/01/2016 10:00:23.000
    A       7         OK         02/01/2016 10:00:25.000
    A       7       Critical     02/01/2016 10:00:36.000
    A       8         OK         02/01/2016 10:00:39.000
    A       8         OK         02/01/2016 10:00:40.000
    B       1       Critical     02/02/2016 10:00:00.000
    B       1         OK         02/02/2016 10:00:04.000
    B       2       Critical     02/02/2016 10:00:04.000
    B       2         OK         02/02/2016 10:00:06.000
    B       3       Critical     02/02/2016 10:00:07.000
    B       3       Critical     02/02/2016 10:00:09.000
    B       4       Critical     02/02/2016 10:00:10.000
    B       4       Critical     02/02/2016 10:00:12.000
    B       5         OK         02/02/2016 10:00:14.000
    B       5       Critical     02/02/2016 10:00:17.000
    B       6       Critical     02/02/2016 10:00:20.000
    B       6       Critical     02/02/2016 10:00:23.000
    B       7       Critical     02/02/2016 10:00:25.000
    B       7         OK         02/02/2016 10:00:36.000
    B       8       Critical     02/02/2016 10:00:39.000
    B       8       Critical     02/02/2016 10:00:40.000

这就是3数据库表。 tblWeb.WebID = tblWebServices.WebID

我需要得到 tblWeb的AppName并将tblSites的站点转换为列 并且在网站下面应该是"最新"摘要tblWebServices中的Appname状态和所有没有数据的站点应标记为"未找到数据"

预期的输出是这样的:

Expected Output:

| AppName |   Site-AA   |   Site-BB   |     Site-CC     |     Site-DD     |
   aWeb       Critical        OK         No Data Found     No Data Found
   bWeb       Critical        OK         No Data Found     No Data Found
   cWeb          OK        Critical      No Data Found     No Data Found
   dWeb          OK        Critical      No Data Found     No Data Found
   eWeb          OK        Critical      No Data Found     No Data Found
   fWeb          OK        Critical      No Data Found     No Data Found
   gWeb       Critical        OK         No Data Found     No Data Found
   hWeb          OK        Critical      No Data Found     No Data Found

这是我想要的输出。 我已经在这里搜索了所有的例子和问题,但对我来说似乎没有任何意义。 有人可以帮我在SQL Server中配置有关此输出的一些代码。

这是我在此问题中配置了一些答案的代码

DECLARE @sql NVARCHAR(MAX)
SET @sql = ''
SELECT @sql = 'SELECT WebID ' + CHAR(10)
SELECT @sql = @sql + '   , ISNULL(MAX(CASE WHEN Sites  = ''' + Sites  + '''THEN summary_state END), ''No Data Found'') AS '  + QUOTENAME('Site-'+Sites) + CHAR(10)
FROM tblSites
ORDER BY Sites
SELECT @sql = @sql + 'FROM ( SELECT *, rn = ROW_NUMBER() OVER(PARTITION BY Sites , WebID ORDER BY Last_Check DESC) FROM tblWebServices ) t WHERE rn = 1 GROUP BY WebID ORDER BY WebID '
PRINT @sql
EXEC sp_executesql @sql

此代码的输出如下

|  WebID  |   Site-AA   |   Site-BB   |     Site-CC     |     Site-DD     |
     1        Critical        OK         No Data Found     No Data Found
     2        Critical        OK         No Data Found     No Data Found
     3           OK        Critical      No Data Found     No Data Found
     4           OK        Critical      No Data Found     No Data Found
     5           OK        Critical      No Data Found     No Data Found
     6           OK        Critical      No Data Found     No Data Found
     7        Critical        OK         No Data Found     No Data Found
     8           OK        Critical      No Data Found     No Data Found

我不需要WebID存在。我需要AppName。 请在这件事上给予我帮助 您的评论和回答将是一个很大的帮助。 谢谢

1 个答案:

答案 0 :(得分:0)

如果您想在结果选择

上选择Appname,请尝试此操作
DECLARE @sql NVARCHAR(MAX)
SET @sql = ''
SELECT @sql = 'SELECT t.webid, tw.appname ' + CHAR(10)
SELECT @sql = @sql + '   , ISNULL(MAX(CASE WHEN Sites  = ''' + Sites  + '''THEN summary_state END), ''No Data Found'') AS '  + QUOTENAME('Site-'+Sites) + CHAR(10)
FROM tblSites
ORDER BY Sites
SELECT @sql = @sql + 'FROM ( SELECT *, rn = ROW_NUMBER() OVER(PARTITION BY Sites , WebID ORDER BY Last_Check DESC) FROM tblWebServices )t INNER JOIN tblweb tw on t.webid = tw.webid WHERE rn = 1 GROUP BY t.WebID, tw.appname ORDER BY t.WebID '
PRINT @sql
EXEC sp_executesql @sql

如果您不想要webid,请将其从第三行的选择中删除

  SELECT @sql = 'SELECT tw.appname ' + CHAR(10)

你非常接近解决方案

希望有所帮助