我的SQL有点生疏,所以请容忍我。
我有一个疑问,联合,两个选择不同的语句,从不同的来源。来自两个select语句的数据重叠,但每个语句都提取一个日期列,该列并不总是填充,这取决于源。
我试图找到一种方法来删除这两个语句之间的重复记录,而忽略日期列。
与之一样,如果两个或多个记录具有相同的PCName、供应商、产品和版本,则不管日期列如何,都会筛选其中一个记录。
SELECT DISTINCT
SYS.Netbios_Name0 as PCName,
ARP.Publisher0 as Vendor,
ARP.DisplayName0 as Product,
ARP.Version0 as Version,
replace(replace(convert(varchar,ARP.InstallDate0,120),'-',''),' 00:00:00','') as InstallDate,
replace(replace(convert(varchar,ARP.InstallDate0,120),'-',''),' 00:00:00','') as InstallDate2
FROM v_Add_Remove_Programs ARP
JOIN v_R_System SYS
ON ARP.ResourceID=SYS.ResourceID
WHERE SYS.Netbios_Name0 like 'CH-%' and InstallDate0 NOT LIKE ''
UNION
Select DISTINCT
SYS.Netbios_Name0 as PCName,
SP.CompanyName as Vendor,
SP.ProductName as Product,
SP.ProductVersion as Version,
replace(replace(convert(varchar,MARP.InstallDate0,120),'-',''),' 00:00:00','') as InstallDate,
replace(replace(convert(varchar,GSI.InstallDate0,120),'-',''),' 00:00:00','') as InstallDate2
FROM v_GS_SoftwareProduct SP
JOIN v_R_System SYS
ON SP.ResourceID=SYS.ResourceID
LEFT JOIN v_GS_Mapped_Add_Remove_Programs MARP
ON SP.ResourceID = MARP.ResourceID
AND RTRIM(LTRIM(UPPER(SP.ProductName)))
LIKE RTRIM(LTRIM(UPPER(MARP.DisplayName0)))
AND RTRIM(LTRIM(UPPER(SP.ProductVersion)))
LIKE RTRIM(LTRIM(UPPER(MARP.Version0)))
LEFT JOIN v_GS_INSTALLED_SOFTWARE GSI
ON SP.ResourceID = GSI.ResourceID
AND RTRIM(LTRIM(UPPER(SP.ProductName)))
LIKE RTRIM(LTRIM(UPPER(GSI.ProductName0)))
AND RTRIM(LTRIM(UPPER(SP.ProductVersion)))
LIKE RTRIM(LTRIM(UPPER(GSI.ProductVersion0)))
Where SYS.Netbios_Name0 Like 'CH-%'
AND (MARP.InstallDate0 NOT LIKE ''
OR GSI.InstallDate0 NOT LIKE '')
ORDER By PCName, Vendor, Product, Version
发布于 2018-05-29 11:54:40
结束整个查询,然后按主键进行分区,只过滤rownum=1
select * from (
select *, row_number() over(partition by PCName, Vendor, Product, Version order by PCName, Vendor, Product, Version) rownum from (
SELECT
SYS.Netbios_Name0 as PCName,
ARP.Publisher0 as Vendor,
ARP.DisplayName0 as Product,
ARP.Version0 as Version,
replace(replace(convert(varchar,ARP.InstallDate0,120),'-',''),' 00:00:00','') as InstallDate,
replace(replace(convert(varchar,ARP.InstallDate0,120),'-',''),' 00:00:00','') as InstallDate2
FROM v_Add_Remove_Programs ARP
JOIN v_R_System SYS
ON ARP.ResourceID=SYS.ResourceID
WHERE SYS.Netbios_Name0 like 'CH-%' and InstallDate0 NOT LIKE ''
UNION
Select
SYS.Netbios_Name0 as PCName,
SP.CompanyName as Vendor,
SP.ProductName as Product,
SP.ProductVersion as Version,
replace(replace(convert(varchar,MARP.InstallDate0,120),'-',''),' 00:00:00','') as InstallDate,
replace(replace(convert(varchar,GSI.InstallDate0,120),'-',''),' 00:00:00','') as InstallDate2
FROM v_GS_SoftwareProduct SP
JOIN v_R_System SYS
ON SP.ResourceID=SYS.ResourceID
LEFT JOIN v_GS_Mapped_Add_Remove_Programs MARP
ON SP.ResourceID = MARP.ResourceID
AND RTRIM(LTRIM(UPPER(SP.ProductName)))
LIKE RTRIM(LTRIM(UPPER(MARP.DisplayName0)))
AND RTRIM(LTRIM(UPPER(SP.ProductVersion)))
LIKE RTRIM(LTRIM(UPPER(MARP.Version0)))
LEFT JOIN v_GS_INSTALLED_SOFTWARE GSI
ON SP.ResourceID = GSI.ResourceID
AND RTRIM(LTRIM(UPPER(SP.ProductName)))
LIKE RTRIM(LTRIM(UPPER(GSI.ProductName0)))
AND RTRIM(LTRIM(UPPER(SP.ProductVersion)))
LIKE RTRIM(LTRIM(UPPER(GSI.ProductVersion0)))
Where SYS.Netbios_Name0 Like 'CH-%'
AND (MARP.InstallDate0 NOT LIKE ''
OR GSI.InstallDate0 NOT LIKE '')
)a)b
where rownum=1
发布于 2018-05-29 11:55:16
暂时忘掉UNION
,假设您在Table1
中插入了UNION
结果,然后只使用ROW_NUMBER()
来选择您需要的字段组合的第一个幻影。
SELECT *
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY PCName, Vendor, Product, Version
ORDER BY InstallDate) as rn
FROM Table1
) as T
WHERE rn = 1
https://stackoverflow.com/questions/50591933
复制相似问题