从SQL Server查询中删除列的重复条目

时间:2016-09-30 09:50:43

标签: sql sql-server

我想从查询中删除所有重复项以返回不同的记录行...

SELECT 
    DISTINCT (rme.RoleMappingEmployeeKey) AS role_key_one,
    me.EmpSAPID AS emp_sap_id,
    me.SrKey AS emp_sr_key,
    CONCAT(me.EmpFirstName,' ',me.EmpLastName) AS emp_name,
    rm.RoleName AS emp_role_name,
    rm.SubmittedDate AS emp_role_given_date,
    ms.SiteName AS site_name,
    hd.HRDeptName AS dept_name,
    hsd.HRSDName AS sub_dept_name,
    mc2.ClientName AS client_name,
    mb.BusinessTypeName AS bus_type,
    mc3.CompName AS comp_name,
    hem.SeparationDate AS lwd
FROM 
    dbo.RoleMappingEmployee rme
LEFT JOIN 
    dbo.MasterEmp me ON me.SrKey = rme.SrKey
LEFT JOIN 
    dbo.MasterCountry mc ON (mc.CountryKey = rme.CountryKey OR mc.CountryKey = me.CountryKey)
LEFT JOIN 
    dbo.RoleMaster rm ON rm.RoleKey = rme.RoleKey
LEFT JOIN 
    dbo.HRMasterEmployeeMain hem ON (hem.SrKey = me.SrKey AND hem.EmployeeStatus=10)
LEFT JOIN 
    dbo.HRMapping_SubDept_SBand_Desig_SubFunction hsdsdsf ON hsdsdsf.HRSubDeptBDSbKey = hem.HRSubDeptBDSbKey
LEFT JOIN 
    dbo.HRMasterSubDepartment hsd ON (hsd.HRSDKey = rme.SubDeptKey OR hsd.HRSDKey = hsdsdsf.HRSDKey)
LEFT JOIN 
    dbo.HRMasterDepartment hd ON ( hd.HRDeptKey = rme.DeptKey OR hd.HRDeptKey = hsd.HRDeptKey)
LEFT JOIN 
    dbo.MasterSite ms ON (ms.SiteKey = rme.SiteKey OR ms.SiteKey = me.SiteKey)
LEFT JOIN 
    dbo.MasterClient mc2 ON (mc2.ClientKey = rme.ClientKey OR mc2.ClientKey = hd.ClientKey)
LEFT JOIN 
    dbo.MasterBusiness mb ON (mb.BusinessTypeKey = rme.BusinessTypeKey OR mb.BusinessTypeKey = hd.BusinessTypeKey)
LEFT JOIN 
    dbo.MasterComp mc3 ON (mc3.CompKey = rme.CompKey OR mc3.CountryKey = mc.CountryKey)
WHERE
    me.IsActive = 0
ORDER BY
    rme.RoleMappingEmployeeKey DESC

此查询返回结果,如此屏幕截图所示:

Check the value highlighted below

RoleMappingEmployeeKey AS role_key_one,虽然我已经提到了查询中列的关键字DISTINCT,但仍在重复...

1 个答案:

答案 0 :(得分:1)

<强>声明

如果DISTINCT&#34;没有工作&#34;这意味着 IS 重复&#34;重复&#34;列。因此,使用&#34;重复删除&#34;你会失去一些东西 - 我上面提到的差异。但是......如果你不关心这种差异,你可以使用这样的东西:

WITH dedup as
(
    SELECT 
        (rme.RoleMappingEmployeeKey) AS role_key_one,
        row_number() over (partition by rme.RoleMappingEmployeeKey order by rm.SubmittedDate) [role_key_one_rank],
        me.EmpSAPID AS emp_sap_id,
        me.SrKey AS emp_sr_key,
        CONCAT(me.EmpFirstName,' ',me.EmpLastName) AS emp_name,
        rm.RoleName AS emp_role_name,
        rm.SubmittedDate AS emp_role_given_date,
        ms.SiteName AS site_name,
        hd.HRDeptName AS dept_name,
        hsd.HRSDName AS sub_dept_name,
        mc2.ClientName AS client_name,
        mb.BusinessTypeName AS bus_type,
        mc3.CompName AS comp_name,
        hem.SeparationDate AS lwd
    FROM 
        dbo.RoleMappingEmployee rme
    LEFT JOIN 
        dbo.MasterEmp me ON me.SrKey = rme.SrKey
    LEFT JOIN 
        dbo.MasterCountry mc ON (mc.CountryKey = rme.CountryKey OR mc.CountryKey = me.CountryKey)
    LEFT JOIN 
        dbo.RoleMaster rm ON rm.RoleKey = rme.RoleKey
    LEFT JOIN 
        dbo.HRMasterEmployeeMain hem ON (hem.SrKey = me.SrKey AND hem.EmployeeStatus=10)
    LEFT JOIN 
        dbo.HRMapping_SubDept_SBand_Desig_SubFunction hsdsdsf ON hsdsdsf.HRSubDeptBDSbKey = hem.HRSubDeptBDSbKey
    LEFT JOIN 
        dbo.HRMasterSubDepartment hsd ON (hsd.HRSDKey = rme.SubDeptKey OR hsd.HRSDKey = hsdsdsf.HRSDKey)
    LEFT JOIN 
        dbo.HRMasterDepartment hd ON ( hd.HRDeptKey = rme.DeptKey OR hd.HRDeptKey = hsd.HRDeptKey)
    LEFT JOIN 
        dbo.MasterSite ms ON (ms.SiteKey = rme.SiteKey OR ms.SiteKey = me.SiteKey)
    LEFT JOIN 
        dbo.MasterClient mc2 ON (mc2.ClientKey = rme.ClientKey OR mc2.ClientKey = hd.ClientKey)
    LEFT JOIN 
        dbo.MasterBusiness mb ON (mb.BusinessTypeKey = rme.BusinessTypeKey OR mb.BusinessTypeKey = hd.BusinessTypeKey)
    LEFT JOIN 
        dbo.MasterComp mc3 ON (mc3.CompKey = rme.CompKey OR mc3.CountryKey = mc.CountryKey)
    WHERE
        me.IsActive = 0
)
select distinct *
from dedup
where [role_key_one_rank] = 1
ORDER BY role_key_one DESC