2016-02-08 148 views
0

我正在尝试编写一个基于employeeID(每个employeeID不同范围)的特定日期范围的Oracle SQL查询,然后为每个员工在该日期范围内的特定绩效指标数字求和。根据我的选择,我似乎对同一个employeeID的结果不一致。这是我的查询:Oracle SQL - 不一致的查询结果

select c.employeeID, b.assessmentDate, c.startDate, c.endDate, 
sum(case when employeeProductivityMetric='15' then 1 else 0 end) as Metric1, 
sum(case when employeeProductivityMetric='20' then 1 else 0 end) as Metric2 
from assessmentTable b 
inner join performanceMetricTable c on b.badgeID = c.badgeID 
where b.assessmentDate between c.startDate and c.endDate 
group by c.employeeID, b.assessmentDate, c.startDate, c.endDate 
order by c.employeeID, b.assessmentDate; 

请注意,给定的employeeID可以与多个badgeID关联。

当我选择一个特定的雇员(比如2)通过添加以下where子句:

where c.employeeID=2 

我得到一些特别的号码Metric1和Metric2:

employeeID assessmentDate startDate endDate Metric1 Metric2 
2   02-Jul-15  01-Jul-15 31-Jul-15 4  5 

然而,当我做

where c.employeeID between 1 and 3 

我得到不同的员工2号码,如:

employeeID assessmentDate startDate endDate Metric1 Metric2 
2   02-Jul-15  01-Jul-15 31-Jul-15 3  0 

有没有人知道为什么会这样?我的查询设计有问题吗?

谢谢你的指点!

纳塔利娅

+0

有没有可能将'badgeID'链接到多个'employeeID's? –

+0

您正在通过'employeeId'进行分组。我没有看到该列上的过滤条件如何影响给定值的行内结果。 –

回答

0

您可以通过检索详细记录调试这种类型的情况:

尝试最有可能比较以下查询

-- matched records for employeeID=2 
select c.employeeID, c.badgeID, b.assessmentDate, c.startDate, c.endDate, 
employeeProductivityMetric 
from assessmentTable b 
inner join performanceMetricTable c on b.badgeID = c.badgeID 
where b.assessmentDate between c.startDate and c.endDate 
     and c.employeeID=2 
order by c.employeeID, c.badgeID, b.assessmentDate; 

-- matched records for employeeID between 1 and 3 
select c.employeeID, c.badgeID, b.assessmentDate, c.startDate, c.endDate, 
employeeProductivityMetric 
from assessmentTable b 
inner join performanceMetricTable c on b.badgeID = c.badgeID 
where b.assessmentDate between c.startDate and c.endDate 
     and c.employeeID between 1 and 3 
order by c.employeeID, c.badgeID, b.assessmentDate 

,你会注意到badgeIds的差异由每个查询返回。