Concatinate 3数据库表并将行转换为列 - SQL Server

时间:2016-02-02 23:51:58

标签: 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
    2      bWeb     www.bWeb.com    ServerA
    3      cWeb     www.cWeb.com    ServerB
    4      dWeb     www.dWeb.com    ServerA
    5      eWeb     www.eWeb.com    ServerC
    6      fWeb     www.fWeb.com    ServerC
    7      gWeb     www.gWeb.com    ServerD
    8      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摘要状态,并且所有没有数据的网站都应标记为“No Data Found”

预期的输出是这样的:

**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)

我在tblWebServices表中添加了summary_state列并修改了你的脚本,它应该可以工作:

DECLARE @sql NVARCHAR(MAX)
SET @sql = ''
SELECT @sql = 'SELECT tblWeb.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 
LEFT JOIN tblWeb ON t.WebID = tblWeb.WebID 
WHERE t.rn = 1 GROUP BY tblWeb.AppName ORDER BY tblWeb.AppName '
PRINT @sql
EXEC sp_executesql @sql