我有这个查询会列出没有安装软件的所有服务器,但问题是它返回了多个资产ID。 我希望它只为每个资产ID返回一个结果。
我一直在尝试使用DISTINCT
,但无法做到正确。它返回的是数千行,而不是几百行。
这是SQL:
Select Top 1000000 tblAssets.AssetID,
tblAssets.AssetUnique,
tblAssets.Domain,
tblAssets.Username,
tblSoftwareUni.softwareName As software,
tblSoftware.softwareVersion As version,
tblSoftwareUni.SoftwarePublisher As publisher,
tsysOS.OSname As OS,
tsysOS.Image As icon
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
Where tblSoftwareUni.softwareName Not Like 'Hawkeye G Host Sensor%' And
tblAssetCustom.State = 1
AND tsysOS.OSname Not Like 'Win 7'
AND tsysOS.OSname Not Like 'Win 10'
AND tsysOS.OSname Not Like 'Win 8.1'
AND tsysOS.OSname Not Like 'Win XP'
AND tsysOS.OSname Not Like 'Win 8'
Order By AssetID Asc;
我在这里看到过类似的问题,并尝试实施这些建议,但似乎无法让它发挥作用。
修改的
对于不包含此内容的道歉,无法正确格式化:
以下是我获得的结果示例:
AssetID AssetUnique Domain Username software version publisher OS icon
3133 COMPUTERNAME DOMAIN Administrator Realtek High Definition Audio Driver 5.10.0.5404 Realtek Semiconductor Corp. Win 2003 R2 win200310.png
3133 COMPUTERNAME DOMAIN Administrator Certero AssetStudio Client 3.2.0 Certero Win 2003 R2 win200310.png
3133 COMPUTERNAME DOMAIN Administrator Intel(R) PRO Network Connections 12.1.12.0 12.1 Intel Win 2003 R2 win200310.png
3133 COMPUTERNAME DOMAIN Administrator Certero AssetStudio 5.0 Client 5.0.703.20109 Certero Win 2003 R2 win200310.png
3133 COMPUTERNAME DOMAIN Administrator LiveReg (Symantec Corporation) 2.4.2.2295 Symantec Corporation Win 2003 R2 win200310.png
3133 COMPUTERNAME DOMAIN Administrator MopUPS Express Win 2003 R2 win200310.png
3133 COMPUTERNAME DOMAIN Administrator Sophos AutoUpdate 5.2.0.276 Sophos Limited Win 2003 R2 win200310.png
3133 COMPUTERNAME DOMAIN Administrator MSXML 6 Service Pack 2 (KB954459) 6.20.1099.0 Microsoft Corporation Win 2003 R2 win200310.png
3133 COMPUTERNAME DOMAIN Administrator Snow Inventory Client for Windows (x86) 3.7.04 Snow Software AB Win 2003 R2 win200310.png
我每个资产ID都会获得多个结果,而我只想要一个结果。
答案 0 :(得分:1)
使用ROW_NUMBER
窗口功能
select * from
(
select row_number()over(partition by tblAssets.AssetID order by (select NULL)) Rn,
...
from your query
) A
Where RN = 1
如果您有任何其他列来订购结果并获取第一个或最后一个,请替换(select NULL)
中的Order by
。
现在结果将是任意的,可以为每个AssetID
答案 1 :(得分:0)
使用ROW_NUMBER()函数获取每个AssetID的不同行。像......那样......
SELECT Top 1000000 *
FROM (
Select tblAssets.AssetID,
tblAssets.AssetUnique,
tblAssets.Domain,
tblAssets.Username,
tblSoftwareUni.softwareName As software,
tblSoftware.softwareVersion As version,
tblSoftwareUni.SoftwarePublisher As publisher,
tsysOS.OSname As OS,
tsysOS.Image As icon,
ROW_NUMBER() OVER (PARTITION BY tblAssets.AssetID ORDER BY tblAssets.AssetID)rn
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
Where tblSoftwareUni.softwareName Not Like 'Hawkeye G Host Sensor%' And
tblAssetCustom.State = 1
AND tsysOS.OSname Not Like 'Win 7'
AND tsysOS.OSname Not Like 'Win 10'
AND tsysOS.OSname Not Like 'Win 8.1'
AND tsysOS.OSname Not Like 'Win XP'
AND tsysOS.OSname Not Like 'Win 8'
)A
WHERE rn = 1
Order By AssetID Asc