加入来自不同表的特定列,包含GROUP BY

时间:2014-05-20 17:11:19

标签: sql sql-server tsql join group-by

我正在尝试将这两个查询合并为一个查询。我尝试了各种JOINS并没有运气......

这是两个SELECTS ......

SELECT     BlockSize, BootVolume, Compressed, SystemName , Label, Caption, PageFilePresent,
           [dbo].[CCS_DIGITAL_STORAGE_CONVERTER]('B', 'GB', Capacity) AS Capacity,
           [dbo].[CCS_DIGITAL_STORAGE_CONVERTER]('B', 'GB', FreeSpace) AS [Free Space], 
           [dbo].[CCS_DIGITAL_STORAGE_CONVERTER]('B', 'GB', Capacity - FreeSpace) AS [Used Space],
           100 * FreeSpace / Capacity AS [Free Space %]

FROM       CCS_Win32_Volume

WHERE      ((@p_ServerName = SystemName) OR (@p_ServerName = 'all'))

SELECT     [CLE_ENV_SHORT], [CLE_ENV_CAT_SHORT]

FROM       [dbo].[CCS_V_SERVER_INSTANCE_DETAILS]

WHERE      [CLE_ENV_SHORT] = @p_Env 
      AND  [CLE_ENV_CAT_SHORT] = @p_EnvCat

我希望所有列都显示在一个表和GROUP BY [CLE_ENV_CAT_SHORT],[CLE_ENV_SHORT]中。

这可能吗? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

SELECT     c1.BlockSize, c1.BootVolume, c1.Compressed, c1.SystemName, c1.Label, c1.Caption, c1.PageFilePresent,
           [dbo].[CCS_DIGITAL_STORAGE_CONVERTER]('B', 'GB', c1.Capacity) AS Capacity,
           [dbo].[CCS_DIGITAL_STORAGE_CONVERTER]('B', 'GB', c1.FreeSpace) AS [Free Space], 
           [dbo].[CCS_DIGITAL_STORAGE_CONVERTER]('B', 'GB', c1.Capacity - c1.FreeSpace) AS [Used Space],
           100 * c1.FreeSpace / c1.Capacity AS [Free Space %],
           [CLE_ENV_SHORT], [CLE_ENV_CAT_SHORT]
FROM       CCS_Win32_Volume c1
JOIN       [dbo].[CCS_V_SERVER_INSTANCE_DETAILS] c2 on c1.SystemName = c2.CSL_SERVER_NAME
WHERE      ((@p_ServerName = c1.SystemName) OR (@p_ServerName = 'all')) and
      c2.[CLE_ENV_SHORT] = @p_Env 
      c2.[CLE_ENV_CAT_SHORT] = @p_EnvCat
ORDER BY c2.[CLE_ENV_CAT_SHORT]