SQL - 根据特定列

时间:2018-05-29 19:51:10

标签: sql sql-server sccm-2007

我的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

Example duplicate

2 个答案:

答案 0 :(得分:1)

总结整个查询,然后按主键分区,只过滤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

答案 1 :(得分:0)

暂时忘记UNION,假设您在UNION中插入了Table1结果,然后只使用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