结合2个查询的结果

时间:2016-06-20 13:38:42

标签: sql sql-server

美好的一天, 我是这个论坛的新手,我很高兴因为我是Sql的新手。 如何使用Union all或任何方式组合这两个查询的结果。进行查询A是否很长

查询 (A)

select 
    a.SiteName,
    a.Hostname,
    a.FreeSpace,
    a.FreeSpacePerc,
    a.Color,
    b.TotalMemory,
    a.Createdon,
    'ProLiant DL585 G7' as Model

from
(
SELECT 
    [SiteName],
    [Hostname],
    [SystemDriveName] as DriveName,
    cast([SystemFreeSpace] as nvarchar(10)) + ' GB (' + [SystemDriveName] + ':)' as FreeSpace,
    cast(([SystemFreeSpacePercentage]*100) as nvarchar(10)) + '% (' + [SystemDriveName] + ':)' as FreeSpacePerc,
    case when [SystemFreeSpace] <= 20 then 'RED' else 'BLACK' end as Color,
    Createdon
  FROM [RednaDBA].[dbo].[HostDriveStatus]
  where 
    active = 1 and
    Hostname like 'AC%'and Createdon >= DATEADD(day, DATEDIFF(day,0,GETDATE()),0)

union   
select * from
(   
SELECT 
    [SiteName],
    [Hostname],
    [DataDriveName],
    cast([DataFreeSpace] as nvarchar(10)) + ' GB (' + [DataDriveName] + ':)' as FreeSpace,
    cast(([DataFreeSpacePercentage]*100) as nvarchar(10)) + '% (' + [DataDriveName] + ':)' as FreeSpacePerc,
    case when [DataFreeSpace] <= 20 then 'RED' else 'BLACK' end as Color,
    CreatedOn
  FROM [RednaDBA].[dbo].[HostDriveStatus]
  where 
    active = 1 and
    Hostname like 'AC%'and Createdon >= DATEADD(day, DATEDIFF(day,0,GETDATE()),0)

union
SELECT 
    [SiteName],
    [Hostname],
    [BackupDriveName],
    cast([BackupFreeSpace] as nvarchar(10)) + ' GB (' + [BackupDriveName] + ':)' as FreeSpace,
    cast(([BackupFreeSpacePercentage]*100) as nvarchar(10)) + '% (' + [BackupDriveName] + ':)' as FreeSpacePerc,
    case when [BackupFreeSpace] <= 20 then 'RED' else 'BLACK' end as Color,
    Createdon
  FROM [RednaDBA].[dbo].[HostDriveStatus]
  where 
    active = 1 and
    Hostname like 'AC%'and Createdon >= DATEADD(day, DATEDIFF(day,0,GETDATE()),0)

    Union
SELECT 
    [SiteName],
    [Hostname],
    [Backup2DriveName],
    cast([Backup2FreeSpace] as nvarchar(10)) + ' GB (' + [Backup2DriveName] + ':)' as FreeSpace,
    cast(([Backup2FreeSpacePercentage]*100) as nvarchar(10)) + '% (' + [Backup2DriveName] + ':)' as FreeSpacePerc,
    case when [BackupFreeSpace] <= 20 then 'RED' else 'BLACK' end as Color,
    CreatedOn
  FROM [RednaDBA].[dbo].[HostDriveStatus]
  where 
    active = 1 and
    Hostname like 'AC%'and Createdon >= DATEADD(day, DATEDIFF(day,0,GETDATE()),0)


) as a
) as a
join 
(
SELECT
    [SiteName],
    [Hostname],
    [TotalMemory] + ' GB' as TotalMemory,
    Createdon
  FROM [RednaDBA].[dbo].[HostMemoryCPUStatus]
  where 
    active = 1 and
    Hostname like 'AC%'and Createdon >= DATEADD(day, DATEDIFF(day,0,GETDATE()),0)

) as b on a.SiteName = a.SiteName and a.Hostname = b.Hostname
 order by
    a.[SiteName], DriveName

查询 (B)

SELECT  
    [SiteName],
    [Hostname],
    cast([SystemFreeSpace] as nvarchar(10)) + ' GB (' + [SystemDriveName] + ':)' as FreeSpace,
    cast(([SystemFreeSpacePercentage]*100) as nvarchar(10)) + '% (' + [SystemDriveName] + ':)' as FreeSpacePerc,
    case when [SystemFreeSpace] <= 20 then 'RED' else 'BLACK' end as Color,
     cast([Memory] as nvarchar(10)) + ' GB ' as TotalMemory,
    Createdon,
    'ProLiant DL585 G7' as Model
  FROM [RednaDBA].[dbo].[DriveStatus_Solarwinds]
  where 
    Hostname like 'AC%'and Createdon >= DATEADD(day, DATEDIFF(day,0,GETDATE()),0)

0 个答案:

没有答案