2016-09-30 107 views
0

我想從查詢中刪除所有重複返回的記錄重複行...刪除從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查詢中列重複...

+4

'DISTINCT'可以處理該行中的所有值,而不僅僅是第一個值。你有沒有搜索如何刪除重複?這類問題之前已經有很多很多次的問題。 – HoneyBadger

+0

第一次離開連接執行爲常規內部連接。將where子句條件移至on子句以獲得真正的左連接行爲。 – jarlh

+0

我已經看到使用'MIN'函數作爲替代,但沒有工作要麼 – coolstoner

回答

1

免責聲明

如果DISTINCT「不工作」就意味着有IS「重複」列之間的差別。所以,「重複刪除」你會失去一些東西 - 我上面提到的差異。但是...如果你不關心這種差異,你可以使用類似這樣的東西:

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 
+0

賓果工作...刪除重複....與我的前端數組中的計數相同後調用'removeDuplicates'函數.... 現在我得到谷歌的'帶'條款....整潔的小技巧有.... 編輯:一個現代的子查詢讓我們說 – coolstoner