简体   繁体   中英

How to exclude a group of items from a report in SQL for Lansweeper

I have a SQL query that runs a report for unauthorized software. There is some computers that I want to exclude from the report, I created a group containing the computers that need to be exempt. I can not get the where not command to work in conjunction with the other variables. I am green to SQL so this might be simple but I can not figure out what is wrong with my syntax.

The group name that I want excluded is 'Test'. Without the not command it will show only the computers in the group. So the group works I just need it to do the inverse and only show what is not in the group.

Select Top 1000000 tblAssets.AssetID,
  tblAssets.AssetUnique,
  tblAssets.Description,
  tblSoftwareUni.softwareName As Software,
  tblSoftware.softwareVersion As Version,
  tblSoftware.Lastchanged,
  tsysOS.Image As icon,
  tblAssetGroups.AssetGroup
From tblSoftware
  Inner Join tblAssets On tblSoftware.AssetID = tblAssets.AssetID
  Inner Join tblSoftwareUni On tblSoftware.softID = tblSoftwareUni.SoftID
  Inner Join tblAssetCustom On tblAssets.AssetID = tblAssetCustom.AssetID
  Inner Join tsysOS On tblAssets.OScode = tsysOS.OScode
  Inner Join tblAssetGroupLink On tblAssets.AssetID = tblAssetGroupLink.AssetID
  Inner Join tblAssetGroups On tblAssetGroups.AssetGroupID =
    tblAssetGroupLink.AssetGroupID
Where Not tblAssetGroups.AssetGroup = 'Noah Test' And tblSoftwareUni.Approved =
  2 And tblAssetCustom.State = 1
Order By tblAssets.AssetName,
  Software

For this operation, you can use the LIKE keyword. Except, as you indicated, we need NOT

So,

Select Top 1000000 tblAssets.AssetID,
  tblAssets.AssetUnique,
  tblAssets.Description,
  tblSoftwareUni.softwareName As Software,
  tblSoftware.softwareVersion As Version,
  tblSoftware.Lastchanged,
  tsysOS.Image As icon,
  tblAssetGroups.AssetGroup
From tblSoftware
  Inner Join tblAssets On tblSoftware.AssetID = tblAssets.AssetID
  Inner Join tblSoftwareUni On tblSoftware.softID = tblSoftwareUni.SoftID
  Inner Join tblAssetCustom On tblAssets.AssetID = tblAssetCustom.AssetID
  Inner Join tsysOS On tblAssets.OScode = tsysOS.OScode
  Inner Join tblAssetGroupLink On tblAssets.AssetID = tblAssetGroupLink.AssetID
  Inner Join tblAssetGroups On tblAssetGroups.AssetGroupID =
    tblAssetGroupLink.AssetGroupID
Where tblAssetGroups.AssetGroup NOT LIKE '%Noah Test%' And tblSoftwareUni.Approved =
  2 And tblAssetCustom.State = 1
Order By tblAssets.AssetName,
  Software

If the text is always going to just be Noah Test , you could use NOT EQUALS , which is evaluated with != or <>

Select Top 1000000 tblAssets.AssetID,
  tblAssets.AssetUnique,
  tblAssets.Description,
  tblSoftwareUni.softwareName As Software,
  tblSoftware.softwareVersion As Version,
  tblSoftware.Lastchanged,
  tsysOS.Image As icon,
  tblAssetGroups.AssetGroup
From tblSoftware
  Inner Join tblAssets On tblSoftware.AssetID = tblAssets.AssetID
  Inner Join tblSoftwareUni On tblSoftware.softID = tblSoftwareUni.SoftID
  Inner Join tblAssetCustom On tblAssets.AssetID = tblAssetCustom.AssetID
  Inner Join tsysOS On tblAssets.OScode = tsysOS.OScode
  Inner Join tblAssetGroupLink On tblAssets.AssetID = tblAssetGroupLink.AssetID
  Inner Join tblAssetGroups On tblAssetGroups.AssetGroupID =
    tblAssetGroupLink.AssetGroupID
Where tblAssetGroups.AssetGroup != 'Noah Test' And tblSoftwareUni.Approved =
  2 And tblAssetCustom.State = 1
Order By tblAssets.AssetName,
  Software

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM