SQL Server:连接两个表

时间:2016-03-14 18:47:54

标签: sql sql-server tsql

我正在尝试将两个表连接在一起,以下列方式显示信息。

ServerName DatabaseName MessageText RestoreTime BackupTime Servername

但是我遇到了问题,我收到了多条记录。结果应该只显示31条记录,但我得到961条记录。我相信我没有正确地编写以下脚本,并希望得到一些帮助。

select * 
from 
    (SELECT 
         ServerName, DatabaseName, MessageText,
         [datetime] as RestoreTime 
     FROM 
         [dbo].[CheckDBHist]
     WHERE 
         datetime < (SELECT getdate()) 
         AND datetime > (SELECT getdate() - 1) 
         AND ServerName = 'servername' 
    ) allnames
inner join
    (select 
         BackupStartDate, ServerName
     from [dbo].[DBBackupFileDate]
     where BackupStartDate < (SELECT getdate()) 
       and BackupStartDate > (SELECT getdate()-1)
    ) servern on servern.ServerName = allnames.ServerName
order by 
    RestoreTime desc

3 个答案:

答案 0 :(得分:0)

尝试:

SELECT 
    ServerName, DatabaseName, MessageText, [datetime] as RestoreTime 
FROM
    [dbo].[CheckDBHist] dbhist
INNER JOIN 
    [dbo].[DBBackupFileDate] bckpdate ON dbhist.ServerName = bckpdate.ServerName
WHERE 
    dbhist.datetime < (SELECT getdate()) AND 
    dbhist.datetime > (SELECT getdate()-1) AND
    ServerName = 'servername' AND 
    bckpdate.BackupStartDate < (SELECT getdate()) AND   
    bckpdate.BackupStartDate > (SELECT getdate()-1) 
ORDER BY 
    dbhist.RestoreTime DESC

答案 1 :(得分:0)

我不时会运行类似的报告来获取数据库信息以及上次备份日期。试试这个

WITH cte_MostRecent
AS
(
SELECT sdb.Name AS DatabaseName, MAX(bus.backup_finish_date) MostRecentBkp
FROM    sys.sysdatabases sdb
        LEFT JOIN msdb.dbo.backupset bus ON bus.database_name = sdb.name
WHERE bus.type = 'D' --full
GROUP BY sdb.name
),
    cte_EndDate
AS
(
SELECT p.name DatabaseName, MAX(p.backup_finish_date) EndDate
FROM cte_MostRecent mr
    LEFT JOIN  ( SELECT sdb.name, bus.backup_finish_date
                 FROM    sys.sysdatabases sdb
                        LEFT JOIN msdb.dbo.backupset bus ON bus.database_name = sdb.name 
                 WHERE bus.type = 'D'
                ) p ON p.name = mr.DatabaseName AND p.backup_finish_date < mr.MostRecentBkp
GROUP BY p.name
)
SELECT mf.DatabaseName, mf.TotalSizeMB, mf.TotalSizeGB
     , COALESCE(CONVERT(VARCHAR(23), MAX(p.StartDate), 121), '-') AS StartDate
     , COALESCE(CONVERT(VARCHAR(23), ed.EndDate, 121), '-') AS EndDate
     , DATEDIFF(hh,MAX(p.StartDate),ed.EndDate) BackupInterval_Hours
FROM
(SELECT  DB_NAME(database_id) AS DatabaseName
      , SUM((size*8)/1024.00) TotalSizeMB
      , SUM((size*8)/1024)/1024.00 TotalSizeGB
 FROM sys.master_files
 GROUP BY database_id) mf
    LEFT JOIN cte_EndDate ed ON ed.DatabaseName = mf.DatabaseName
    LEFT JOIN ( SELECT  sdb.Name AS DatabaseName, bus.backup_finish_date StartDate
                FROM    sys.sysdatabases sdb
                        LEFT OUTER JOIN msdb.dbo.backupset bus ON bus.database_name = sdb.name
                WHERE bus.type = 'D' --full
               ) p ON p.DatabaseName = mf.DatabaseName AND p.StartDate < ed.EndDate 
GROUP BY mf.DatabaseName, mf.TotalSizeMB, mf.TotalSizeGB, ed.EndDate    
ORDER BY mf.TotalSizeMB DESC

答案 2 :(得分:0)

您好,请您尝试以下查询

select * from (SELECT checkHist.ServerName servername, checkHist.DatabaseName, checkHist.MessageText, checkHist.[datetime] as RestoreTime ,bkFileDate.BackupStartDate bkstartDate 
    FROM [dbo].[CheckDBHist] checkHist inner join [dbo].[DBBackupFileDate]  bkFileDate on bkFileDate.ServerName = checkHist.ServerName) allnames 
where (RestoreTime < (SELECT getdate()) 
and datetime > (SELECT getdate()-1)) 
and (bkstartDate < (SELECT getdate()) 
and bkstartDate > (SELECT getdate()-1)) 
and servername = 'servername' 
order by RestoreTime desc