SQL查询 – 重复数据

我们公司目前在全国拥有150个部门.对于某些职位,我们通过报销每个工资期来支付我们的员工制服.每个部门都有自己的人员和薪资办公室,但所有薪水都由第三方集中处理.由于公司内部的转移,员工在每个支付期间收到重复的统一报销的风险很小.例如,员工A转移到新部门并继续从旧部门和新部门获得统一报销.鉴于我们的检查是集中处理的,我们不应该这样做,但我希望能够通过从公司数据仓库中提取数据来确认它.

我编写的用于测试此查询的查询似乎根据员工所在的不同部门的数量不必要地重复了PAYDATE和统一津贴(UACPPD).对于这名员工,他曾在五个不同的部门工作,每次他转移当地的人力资源办公室在他们的本地系统中输入一个新的雇用日期,因此他在同一个PAYDATE有五个不同的行.我确定这与重复值有关,我只是不确定如何构造我的查询以防止它.我必须从两个表中提取,因此这不是一个选项,并且看起来复制存在,无论我使用的连接类型如何.在此先感谢任何指导!

SELECT
P1.store_number as ST#
,P1.store_transferred_from as XST#
,P1.employee_name as NAME
,P1.ssn as SSN
,P2.pay_date as PAYDATE
,P2.uniform_allowance_amt_cppd AS UACPPD
,P1.job_series as JOBCODE
,P1.hire_date as HIREDATE


FROM PersonnelFile as P1
LEFT JOIN PayrollFile as P2
ON P1.SSN = p2.SSN
WHERE P2.pay_date > '2010-05-14'
and P2.uniform_allowance_amt_cppd in (8.25,8.50,300)
and P1.jobs_series in ('2380','1458')
and P1.ssn = '123456789'

ORDER BY P1.ssn,P2.pay_date;

最佳答案 听起来你想要最新的记录.那么为什么不通过P1.hire_date DESC订购,然后将结果限制为1:

SELECT TOP(1) P1.store_number as ST#,
       P1.store_transferred_from as XST#,
       P1.employee_name as NAME,
       P1.ssn as SSN,
       P2.pay_date as PAYDATE,
       P2.uniform_allowance_amt_cppd AS UACPPD, 
       P1.job_series as JOBCODE,
       P1.hire_date as HIREDATE
FROM PersonnelFile as P1
LEFT JOIN PayrollFile as P2 ON P1.SSN = p2.SSN
WHERE P2.pay_date > '2010-05-14'
  AND P2.uniform_allowance_amt_cppd in (8.25,8.50,300)
  AND P1.jobs_series in ('2380','1458')
  AND P1.ssn = '123456789'
ORDER BY P1.hire_date DESC, P1.ssn, P2.pay_date

编辑:

SELECT P1.store_number as ST#,
       P1.store_transferred_from as XST#,
       P1.employee_name as NAME,
       P1.ssn as SSN,
       P2.pay_date as PAYDATE,
       P2.uniform_allowance_amt_cppd AS UACPPD, 
       P1.job_series as JOBCODE,
       P1.hire_date as HIREDATE
FROM 
(
  SELECT TOP(1) * FROM PersonnelFile as PS1
  WHERE PS1.jobs_series in ('2380','1458')
    AND PS1.ssn = '123456789'
  ORDER BY PS1.hire_date DESC
) AS P1
LEFT JOIN PayrollFile as P2 ON P1.SSN = p2.SSN
WHERE P2.pay_date > '2010-05-14'
  AND P2.uniform_allowance_amt_cppd in (8.25,8.50,300)
ORDER BY P1.ssn, P2.pay_date

由于PersonnelFile必须通过P1.SSN = P2.SSN加入PayrollFile,因此您将获得重复项.每次员工使用相同的SSN进行转移时,您都会添加新记录.因此,PersonnelFile中的每条记录都将链接到PayrollFile中具有相同SSN的每条记录.

点赞