1

我有以下查询产生波纹管表:

  SELECT 
    memcpu.[ID]
  , memcpu.[DNS_NAME]
  , memcpu.[MEM_SIZE_MB] 
  , hdd.PATH
  , hdd.CAPACITY 
  FROM [VCenterServer].[dbo].[VPX_VM] AS memcpu
  INNER JOIN [VCenterServer].[dbo].[VPX_GUEST_DISK] AS hdd
  ON memcpu.ID = hdd.VM_ID
  GROUP BY 
    memcpu.[ID]
  , memcpu.[DNS_NAME] 
  , memcpu.[MEM_SIZE_MB]
  , hdd.PATH
  , hdd.CAPACITY
  ;

桌子:

ID  DNS_NAME    MEM_SIZE_MB PATH    CAPACITY
27  name27      2048        C:\     42947571712
28  name28      4096        C:\     42947571712
28  name28      4096        E:\     134214578176
37  name37      6144        C:\     42947571712
38  name38      2048        C:\     42947571712
39  name39      8192        C:\     145077825536

我想了解如何在所有相同 PATH 变量的条件下对 ID 进行 SUM。基本上总结了每个虚拟机的存储,所以我可以看到配置的内存和总存储。

所需的表(注意 ID 28,它有 C: 和 E: 总结):

问题是 CAPACITY 被定义为 nvarchar(21)

ID  DNS_NAME    MEM_SIZE_MB     CAPACITY
27  name27      2048            42947571712
28  name28      4096            177162149888
37  name37      6144            42947571712
38  name38      2048            42947571712
39  name39      8192            145077825536
4

2 回答 2

3

从子句中删除PATHandCAPACITY字段并在.GROUP BYCAPACITYSELECT

这样的事情应该做:

SELECT 
  memcpu.[ID]
, memcpu.[DNS_NAME]
, memcpu.[MEM_SIZE_MB] 
, sum(convert(decimal(21, 0), hdd.CAPACITY))
FROM [VCenterServer].[dbo].[VPX_VM] AS memcpu
INNER JOIN [VCenterServer].[dbo].[VPX_GUEST_DISK] AS hdd
ON memcpu.ID = hdd.VM_ID
GROUP BY 
  memcpu.[ID]
, memcpu.[DNS_NAME] 
, memcpu.[MEM_SIZE_MB]
;
于 2013-09-20T20:37:44.867 回答
0
SELECT 
  memcpu.[ID]
 , memcpu.[DNS_NAME]
 , memcpu.[MEM_SIZE_MB] 

 , sum(hdd.CAPACITY ) CAPACITY 
FROM [VCenterServer].[dbo].[VPX_VM] AS memcpu
INNER JOIN [VCenterServer].[dbo].[VPX_GUEST_DISK] AS hdd
ON memcpu.ID = hdd.VM_ID
GROUP BY 
 memcpu.[ID]
 , memcpu.[DNS_NAME] 
 , memcpu.[MEM_SIZE_MB] ;
于 2013-09-20T20:38:19.603 回答