SQL - Select 根据特定列不同
SQL - Select distinct based on specific columns
我的SQL有点生疏,多多包涵
我有一个查询联合两个 select 来自不同来源的不同语句。来自两个 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
结束整个查询,然后按主键分区并仅过滤 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
暂时忘掉 UNION
,假设您将 UNION
结果插入 Table1
,然后只需使用 ROW_NUMBER()
到 select您需要的字段组合初现
SELECT *
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY PCName, Vendor, Product, Version
ORDER BY InstallDate) as rn
FROM Table1
) as T
WHERE rn = 1
我的SQL有点生疏,多多包涵
我有一个查询联合两个 select 来自不同来源的不同语句。来自两个 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
结束整个查询,然后按主键分区并仅过滤 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
暂时忘掉 UNION
,假设您将 UNION
结果插入 Table1
,然后只需使用 ROW_NUMBER()
到 select您需要的字段组合初现
SELECT *
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY PCName, Vendor, Product, Version
ORDER BY InstallDate) as rn
FROM Table1
) as T
WHERE rn = 1