关于sqlserver数据库表去重的有关问题
关于sqlserver数据库表去重的问题


查询语句如下:
SELECT
distinct A.AccountingCode AS memberID,
A.AccountingName AS memberName,
B.Filed1 AS filed1,
B.Filed2 AS filed2,
ER.EmployeeCode as employeeCode,
ER.DepartName as departmentName,
PT.Name as positionName
FROM
Basic.FL_AccountingInfo A WITH(NOLOCK)
LEFT JOIN Basic.FL_AccountingDetail B WITH(NOLOCK)
ON A.AccountingCode = B.AccountingCode
AND A.AccountingCategoryCode = B.AccountingCategoryCode
AND A.SystemCode = B.SystemCode
AND A.AccountCode = B.AccountCode
left join Basic.FL_EmployeeResume ER with(nolock)
on A.accountingCode = ER.employeeCode
left join Basic.FL_Position PT with(nolock)
on ER.PositionCode = PT.Code
where
A.AccountingCategoryCode = 'ZY'
GROUP BY A.AccountingCode,A.AccountingName,B.Filed1,B.Filed2,ER.EmployeeCode,ER.DepartName,PT.Name
我单表查询没有出现重复的结果

但是左连接后查询就出现重复的结果了,很多人说distinct只能去重行中重复的,不能去掉列重复,那怎么去掉重复列呢,求大神们指导一下,谢谢啦!
------解决思路----------------------
有Detail明细表,会出现一对多的情况,也就是楼主截图里一个memberID有多条记录的情况,首先楼主要明确自己要查找什么,统计什么数据,为什么要去掉重复。
------解决思路----------------------
直接
SELECT
A.AccountingCode AS memberID,
A.AccountingName AS memberName,
max(B.Filed1) AS filed1,
max(B.Filed2) AS filed2,
max(ER.EmployeeCode) as employeeCode,
max(ER.DepartName) as departmentName,
max(PT.Name) as positionName
from ...
where ...
GROUP BY A.AccountingCode,A.AccountingName
试试吧!
查询语句如下:
SELECT
distinct A.AccountingCode AS memberID,
A.AccountingName AS memberName,
B.Filed1 AS filed1,
B.Filed2 AS filed2,
ER.EmployeeCode as employeeCode,
ER.DepartName as departmentName,
PT.Name as positionName
FROM
Basic.FL_AccountingInfo A WITH(NOLOCK)
LEFT JOIN Basic.FL_AccountingDetail B WITH(NOLOCK)
ON A.AccountingCode = B.AccountingCode
AND A.AccountingCategoryCode = B.AccountingCategoryCode
AND A.SystemCode = B.SystemCode
AND A.AccountCode = B.AccountCode
left join Basic.FL_EmployeeResume ER with(nolock)
on A.accountingCode = ER.employeeCode
left join Basic.FL_Position PT with(nolock)
on ER.PositionCode = PT.Code
where
A.AccountingCategoryCode = 'ZY'
GROUP BY A.AccountingCode,A.AccountingName,B.Filed1,B.Filed2,ER.EmployeeCode,ER.DepartName,PT.Name
我单表查询没有出现重复的结果
但是左连接后查询就出现重复的结果了,很多人说distinct只能去重行中重复的,不能去掉列重复,那怎么去掉重复列呢,求大神们指导一下,谢谢啦!
------解决思路----------------------
有Detail明细表,会出现一对多的情况,也就是楼主截图里一个memberID有多条记录的情况,首先楼主要明确自己要查找什么,统计什么数据,为什么要去掉重复。
------解决思路----------------------
直接
SELECT
A.AccountingCode AS memberID,
A.AccountingName AS memberName,
max(B.Filed1) AS filed1,
max(B.Filed2) AS filed2,
max(ER.EmployeeCode) as employeeCode,
max(ER.DepartName) as departmentName,
max(PT.Name) as positionName
from ...
where ...
GROUP BY A.AccountingCode,A.AccountingName
试试吧!