我们公司目前在全国拥有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;
答案 0 :(得分:2)
听起来你想要最新的记录。那么为什么不按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
必须通过PayrollFile
加入P1.SSN = P2.SSN
的方式,您才会获得重复项。每次员工使用相同的SSN
转移时,您都会添加新记录。因此,PersonnelFile
中的每条记录都会链接到PayrollFile
中与SSN
相同的每条记录。