我有以下 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