查询返回SINGLE DISTINCT行

时间:2014-04-18 19:45:56

标签: sql sql-server

我的查询在下面工作,事情是我只需要列出每个唯一的" VolumeSerialNumber0"一旦。在SO上,这个问题并不缺少问题和方法,但他们建议使用子查询和group by子句,但是当我尝试这样做时,我得到一个错误"列名在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

我觉得必须要接近我只是没有让神奇的语法完全正确。

SELECT        
   dbo.v_R_System.Netbios_Name0, 
   dbo.v_GS_LOGICAL_DISK.TimeStamp, 
   dbo.v_GS_LOGICAL_DISK.Description0, 
   dbo.v_GS_LOGICAL_DISK.DeviceID0, 
   dbo.v_GS_LOGICAL_DISK.DriveType0,
   dbo.v_GS_LOGICAL_DISK.Name0, 
   dbo.v_GS_LOGICAL_DISK.SystemName0, 
   dbo.v_GS_LOGICAL_DISK.VolumeName0, 
   dbo.v_GS_LOGICAL_DISK.VolumeSerialNumber0, 
   dbo.v_GS_PARTITION.Size0, 
   dbo.v_GS_LOGICAL_DISK.FileSystem0
FROM            
   dbo.v_R_System 

   INNER JOIN dbo.v_GS_LOGICAL_DISK 
   ON dbo.v_R_System.ResourceID = dbo.v_GS_LOGICAL_DISK.ResourceID 

   INNER JOIN dbo.v_GS_PARTITION 
   ON dbo.v_GS_LOGICAL_DISK.ResourceID = dbo.v_GS_PARTITION.ResourceID

1 个答案:

答案 0 :(得分:4)

SELECT        
   MAX(S.Netbios_Name0), 
   MAX(L.TimeStamp), 
   MAX(L.Description0), 
   MAX(L.DeviceID0), 
   MAX(L.DriveType0),
   MAX(L.Name0), 
   MAX(L.SystemName0), 
   MAX(L.VolumeName0), 
   L.VolumeSerialNumber0, 
   MAX(P.Size0), 
   MAX(L.FileSystem0)
FROM            
   dbo.v_R_System S

   INNER JOIN dbo.v_GS_LOGICAL_DISK L
   ON S.ResourceID = L.ResourceID 

   INNER JOIN dbo.v_GS_PARTITION P
   ON L.ResourceID = P.ResourceID
GROUP BY
   L.VolumeSerialNumber0