2

我有以下 SQL 脚本:

  SELECT       
    vapp.NAME
  , memcpu.[MEM_SIZE_MB]/1000
  , memcpu.[NUM_VCPU]
  , sum(convert(bigint, hdd.CAPACITY))
  FROM [VCenterServer].[dbo].[VPX_VM] AS memcpu
  INNER JOIN [VCenterServer].[dbo].[VPX_GUEST_DISK] AS hdd
  ON memcpu.ID = hdd.VM_ID
  INNER JOIN [VCenterServer].[dbo].[VPXV_RESOURCE_POOL] AS vapp
  ON memcpu.RESOURCE_GROUP_ID = vapp.RESOURCEPOOLID
  GROUP BY 
  memcpu.[ID]
  , memcpu.[DNS_NAME] 
  , memcpu.[MEM_SIZE_MB]
  , memcpu.[NUM_VCPU]
  , hdd.PATH
  , hdd.CAPACITY
  , vapp.RESOURCEPOOLID
  , vapp.NAME;

它产生了这个结果

NAME      MEM     NUM_VCPU  CAPACITY
Core       3            1   42947571712
Core       2            1   42947571712
Core       4            1   42947571712
Core       4            1   134214578176
Phones     2            2   26977628160
Phones     2            2   103442432
Phones     1            1   37378260992
Phones     1            1   103442432

我想做的是根据名称对 MEM NUM_VCPU 和 CAPACITY 求和,但也保留名称:

NAME      MEM     NUM_VCPU  CAPACITY
Core      13         4      263057293312
Phones    6          6      64562774016

更新:

根据答案,我调整了 SQL 代码,它现在应该像现在这样工作:

select t1.NAME, t1.STORAGE, t2.CPU, t2.MEMORY
from 
(SELECT vapp.NAME [NAME]
     ,SUM(ceiling(convert(decimal(21, 0), hdd.CAPACITY)/(1024*1024*1024))) AS STORAGE
     FROM [VCenterServer].[dbo].[VPX_VM] AS memcpu
     INNER JOIN [VCenterServer].[dbo].[VPX_GUEST_DISK] AS hdd
     ON memcpu.ID = hdd.VM_ID
     INNER JOIN [VCenterServer].[dbo].[VPXV_RESOURCE_POOL] AS vapp
     ON memcpu.RESOURCE_GROUP_ID = vapp.RESOURCEPOOLID
     GROUP BY vapp.NAME  
) t1
left join
(SELECT vapp.NAME [NAME]
     ,SUM(memcpu.[MEM_SIZE_MB]/1024) AS [MEMORY]
     ,SUM(memcpu.[NUM_VCPU]) AS [CPU]
     FROM [VCenterServer].[dbo].[VPX_VM] AS memcpu
     INNER JOIN [VCenterServer].[dbo].[VPXV_RESOURCE_POOL] AS vapp
     ON memcpu.RESOURCE_GROUP_ID = vapp.RESOURCEPOOLID
     GROUP BY vapp.NAME  
) t2
on
t1.NAME = t2.NAME
ORDER BY t1.NAME
4

2 回答 2

1

您可以简单地从 GROUP BY 子句中删除不必要的列

  SELECT vapp.NAME
         ,SUM(memcpu.[MEM_SIZE_MB]/1000) AS [MEM_SIZE_MB]
         ,SUM(memcpu.[NUM_VCPU]) AS [NUM_VCPU]
         ,SUM(convert(bigint, hdd.CAPACITY)) AS CAPACITY
  FROM [VCenterServer].[dbo].[VPX_VM] AS memcpu
    INNER JOIN [VCenterServer].[dbo].[VPX_GUEST_DISK] AS hdd
      ON memcpu.ID = hdd.VM_ID
    INNER JOIN [VCenterServer].[dbo].[VPXV_RESOURCE_POOL] AS vapp
      ON memcpu.RESOURCE_GROUP_ID = vapp.RESOURCEPOOLID
  GROUP BY vapp.NAME;
于 2013-09-21T21:27:40.240 回答
1

请试试:

SELECT
    Name,
    SUM(MEM) MEM,
    SUM(NUM_VCPU) NUM_VCPU,
    SUM(CAPACITY) CAPACITY
FROM(
    SELECT       
        vapp.NAME, 
        memcpu.[MEM_SIZE_MB]/1000 MEM, 
        memcpu.[NUM_VCPU] NUM_VCPU, 
        SUM(CONVERT(BIGINT, hdd.CAPACITY)) OVER(PARTITION BY
            memcpu.[ID], 
            memcpu.[DNS_NAME],
            memcpu.[MEM_SIZE_MB], 
            memcpu.[NUM_VCPU], 
            hdd.PATH, 
            hdd.CAPACITY, 
            vapp.RESOURCEPOOLID, 
            vapp.NAME) CAPACITY 

      FROM [VCenterServer].[dbo].[VPX_VM] AS memcpu
          INNER JOIN [VCenterServer].[dbo].[VPX_GUEST_DISK] AS hdd
          ON memcpu.ID = hdd.VM_ID
          INNER JOIN [VCenterServer].[dbo].[VPXV_RESOURCE_POOL] AS vapp
          ON memcpu.RESOURCE_GROUP_ID = vapp.RESOURCEPOOLID
    )x
    GROUP BY NAME
于 2013-09-21T04:13:02.913 回答