查询自己的MS SQL表

时间:2015-05-22 08:47:03

标签: sql sql-server database

对于我想要的SQL查询/ vTable我有点疑惑。我们有一个包含计算机(虚拟和物理)的表:

CluserName  ComputerName  CoreCount
N/A         BackOffice1   2
cluster1    sqlServer     4
cluster1    webserver     2
cluster1    sqlServer2    6
cluster2    adServer      2
cluster2    adServer2     4

我不想拉vTable给我这样的东西:

Cluster: cluster1, 12 (this is a summation of the cores in the
cluster) sqlServer, 4 webserver, 2 sqlServer2, 6 Cluster: cluster2, 6
adServer, 2 adServer2, 4

所以我的两个问题是:

  1. 通过vTable
  2. 执行此操作是愚蠢的
  3. 如果不是INNER JOIN或嵌套查询?
  4. 感谢。

3 个答案:

答案 0 :(得分:0)

http://sqlfiddle.com/#!3/4f7fe/11/0

SELECT CluserName, SUM(CoreCount) AS CoreCount, 
SUM(CASE WHEN ComputerName = 'sqlServer' THEN CoreCount ELSE 0 END) AS sqlServer,
SUM(CASE WHEN ComputerName = 'webserver' THEN CoreCount ELSE 0 END) AS webserver,
SUM(CASE WHEN ComputerName = 'sqlServer2' THEN CoreCount ELSE 0 END) AS sqlServer2,
SUM(CASE WHEN ComputerName = 'asServer' THEN CoreCount ELSE 0 END) AS asServer
FROM table1
group by CluserName

使用动态查询进行更新:

这构建了一个动态查询,从表中获取计算机名称,因此当添加新计算机名称时,它应该可以工作。

http://sqlfiddle.com/#!3/c95d3/34

DECLARE @statement nvarchar(max)
SET @statement = 'SELECT CluserName, SUM(CoreCount) AS CoreCount, '
SELECT @statement = @statement + 
( SELECT distinct 'SUM(CASE WHEN COMPUTERName = ''' + ComputerName 
+ ''' THEN CoreCount ELSE 0 END) AS ' + ComputerName + ',' FROM table1 FOR XML PATH('') 
) 
+ ' '''' FROM table1 group by CluserName'

EXECUTE sp_executesql @statement

答案 1 :(得分:0)

ROLLUP分组:

DECLARE @t TABLE
    (
      CluserName VARCHAR(20) ,
      ComputerName VARCHAR(20) ,
      CoreCount INT
    )

INSERT  INTO @t
VALUES  ( 'N/A', 'BackOffice1', 2 ),
        ( 'cluster1', 'sqlServer', 4 ),
        ( 'cluster1', 'webserver', 2 ),
        ( 'cluster1', 'sqlServer2', 6 ),
        ( 'cluster2', 'adServer', 2 ),
        ( 'cluster2', 'adServer2', 4 )


SELECT  CluserName , ComputerName, SUM(CoreCount) CoreCount
FROM    @t
GROUP BY ROLLUP(CluserName,ComputerName)
ORDER BY GROUPING(CluserName) DESC, CluserName, GROUPING(ComputerName) desc

输出:

CluserName  ComputerName    CoreCount
NULL        NULL            20
cluster1    NULL            12
cluster1    sqlServer       4
cluster1    sqlServer2      6
cluster1    webserver       2
cluster2    NULL            6
cluster2    adServer        2
cluster2    adServer2       4
N/A         NULL            2
N/A         BackOffice1     2

如果你不想要总行,那么:

SELECT  CluserName , ComputerName, SUM(CoreCount) CoreCount
FROM    @t
GROUP BY ROLLUP(CluserName,ComputerName)
HAVING CluserName IS NOT NULL OR ComputerName IS NOT NULL
ORDER BY GROUPING(CluserName) DESC, CluserName, GROUPING(ComputerName) desc

输出:

CluserName  ComputerName    CoreCount
cluster1    NULL            12
cluster1    sqlServer       4
cluster1    sqlServer2      6
cluster1    webserver       2
cluster2    NULL            6
cluster2    adServer        2
cluster2    adServer2       4
N/A         NULL            2
N/A         BackOffice1     2

答案 2 :(得分:0)

您可以制作动态数据透视语句,如下所示: JSFiddle

在你的情况下,我认为它看起来像这样 - 尝试一下:

SQL Server (SQLEXPRESS)

结果如下:

DECLARE @computernames NVARCHAR(MAX)
DECLARE @total NVARCHAR(MAX)
DECLARE @stmt NVARCHAR(MAX)

SET  @computernames = ''
SET  @stmt = ''

--Get List of Computers
SELECT  @computernames = @computernames + ',[' + ComputerName + ']'
FROM    yourtable
GROUP BY ComputerName

--Remove Leading Comma
SET @computernames = RIGHT(@computernames, LEN(@computernames)-1) 

--transform [adserver],[adserver2]  to ISNULL([adserver],0) + ISNULL([adserver2],0)
SET @total = 'ISNULL(' + REPLACE(@computernames,'],[','],0) + ISNULL([') + ',0)'


--Build PIVOT Statement
SET @stmt = 'SELECT [ClusterName],' + @computernames + ','+ @total + ' AS TotalCores
            FROM    yourtable
                    PIVOT
                    (
                        SUM(CoreCount) FOR ComputerName IN (' + @computernames + ')
                    ) pvt
            ORDER BY [ClusterName]'


--Execute
EXEC sp_executesql @stmt