我有两个表:一个表(好吧,它是一个视图),vComputer,按唯一GUID列出许多计算机。另一个表Inv_Screensaver_File_Info列出了计算机是否已运行自定义清单并报告其屏幕保护程序文件的当前状态。
我需要列出Inv_Screensaver_File_Info中不存在的所有计算机的GUID,以及任何存在但其Inv_Screensaver_File_Info.LastModified值小于该表中特定记录结果的计算机的任何计算机,该表代表权威来源。 / p>
这是我希望所有行都要比较的记录: MYSERVER B5A85423-26C4-40C0-DA28-237FB4CB0B33 2012-02-09 11:38:50.000 唯一的GUID以vc.Guid和sfi._ResourceGuid
的形式存在执行此操作的SQL方法是什么?
SELECT vc.Name,
vc.Guid,
sfi.LastModified
FROM vComputer vc
LEFT OUTER JOIN Inv_Screensaver_File_Info sfi ON sfi._ResourceGuid=vc.Guid
WHERE sfi.LastModified IS NULL
ORDER BY sfi.LastModified DESC
答案 0 :(得分:0)
SELECT vc.Name, vc.Guid
FROM vComputer vc
LEFT JOIN Inv_Screensaver_File_Info sfi ON sfi._ResourceGuid=vc.Guid
INNER JOIN vComputer vc2 ON vc2.Guid = 'B5A85423-26C4-40C0-DA28-237FB4CB0B3'
INNER JOIN Inv_Screensaver_File_Info sfi2 ON sfi2._ResourceGuid = vc2.Guid
WHERE sfi.LastModified IS NULL
OR sfi.LastModified < sfi2.LastModified
答案 1 :(得分:0)
在没有架构和所需输出的示例的情况下回答查询问题总是有点困难,但这是一次尝试。
select vc.Name, vc.Guid, vc.LastModified
from vComputer vc
where not exists(select 1
from Inv_Screensaver_File_Info sfi
where sfi._ResourceGuid = vc.Guid)
or exists(select 1
from Inv_Screensaver_File_Info sfi1
inner join Inv_Screensaver_File_Info sfi2
on sfi2.ResourceGuid = sfi1.ResourceGuid and
sfi2.LastModified < sfi1.LastModified
where sfi._ResourceGuid = vc.Guid)
答案 2 :(得分:0)
你说你需要guid,如果你需要的话,也可以在select和group by中添加vc.Name:
DECLARE @CutoffDateTime UNIQUEIDENTIFIER
SELECT @CutoffDateTime = LastModified FROM MyServer WHERE Guid = 'B5A85423-26C4-40C0-DA28-237FB4CB0B33'
SELECT vc.Guid
FROM vComputer vc
LEFT OUTER JOIN Inv_Screensaver_File_Info sfi ON sfi._ResourceGuid = vc.Guid
GROUP BY vc.Guid
HAVING MAX(sfi.LastModified) < @CutoffDate || MAX(sfi.LastModified) IS NULL