2016-02-08 188 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的差異由每個查詢返回。