You have to remember that you're (the SQL server is) processing the records one-by-one. As presented, along with the other conditions, you're checking
- is the current antivirus record "Antivirus A"
- is the current antivirus record also expired
- is the current antivirus record also "Antivirus B"
- is the current antivirus record also expired
The current antivirus record being checked will never be both "Antivirus A" and "Antivirus B". You need to make two links to the antivirus table: one to check the status of A, the second to check the status of B.
Try this:
Select Top 1000000
tsysAssetTypes.AssetTypeIcon10 As icon,
tblAssets.AssetID,
tblAssets.AssetName,
tblAssets.Domain,
tsysAssetTypes.AssetTypename As Type,
tblAssets.IPAddress As [IP Address],
tblAssets.Description,
tblAssetCustom.Manufacturer,
tblAssetCustom.Model,
tblAssetCustom.Location,
tsysIPLocations.IPLocation,
tblAssets.Lasttried,
tblAssets.Firstseen,
tblAssets.Lastseen,
AntivirusA.DisplayName AS A_name,
AntivirusB.DisplayName AS B_name
From
tblAssets
Inner Join tblAssetCustom On tblAssets.AssetID = tblAssetCustom.AssetID
Inner Join tsysAssetTypes On tsysAssetTypes.AssetType = tblAssets.Assettype
Inner Join tblComputerSystem On tblComputerSystem.AssetID = tblAssets.AssetID
Left Join tsysIPLocations On tblAssets.LocationID = tsysIPLocations.LocationID
Left Join (Select tblAntivirus.AssetID,
tblAntivirus.DisplayName
From tblAntivirus
Where tblAntivirus.DisplayName Like '%ANTIVIRUS A%'
And tblAntivirus.productUpToDate = 0) AS AntivirusA On AntivirusA.AssetID = tblAssets.AssetID
Left Join (Select tblAntivirus.AssetID,
tblAntivirus.DisplayName
From tblAntivirus
Where tblAntivirus.DisplayName Like '%ANTIVIRUS B%'
And tblAntivirus.productUpToDate = 0) AS AntivirusB On AntivirusB.AssetID = tblAssets.AssetID
Where
tblAssetCustom.State = 1 -- active
AND tblAssets.AssetName Not Like '%SERV%'
AND AntivirusA.AssetID IS NOT NULL -- expired Antivirus A found
AND AntivirusB.AssetID IS NOT NULL -- expired Antivirus B found
I don't have any records in my inventory that meet the conditions you're after, but when I modified the code to look for machines that have two specific non-expired antivirus programs, I did produce results, so I think this should do what you're after.