2014-05-07 28 views
2

我正在修改一個Crystal Report來計算每班次的員工數量。在查看SQL時,我注意到一些看起來不正確的東西。我正在複製和粘貼除兩個計數(一個不同計數)基本相同的SQL語句外,另外兩個列出了員工的personID(一個不同的列表)。 DISTINCT Count語句不會產生預期的結果。希望有人能幫助我瞭解什麼是要去:需要幫助瞭解這些SQL結果

(select (personid) from rpt_peoplestaffingroledetail where roleid in (select roleid 
from rpt_peopleroledef where affectdash = 1) and shiftid in 
(select shiftid from rpt_staffingeventshiftdetail where driveshiftid = '623044')) 

給出: PERSONID:51135,51135,51135,61905,62926

這是合理的有有三個人配備了這種轉變,一個有三個角色。

當我執行不同的清單:

(select distinct (personid) from rpt_peoplestaffingroledetail where roleid in 
(select roleid from rpt_peopleroledef where affectdash = 1) and shiftid in 
(select shiftid from rpt_staffingeventshiftdetail where driveshiftid = '623044')) 

我得到: PERSONID:51135,61905,62926

這又似乎是正確的,有三種人分配給該移(一個帶多張角色,但不同使得它的工作)

所以,當我想算人員的人數:

(select count (personid) from rpt_peoplestaffingroledetail where roleid in 
(select roleid from rpt_peopleroledef where affectdash = 1) and shiftid in 
(select shiftid from rpt_staffingeventshiftdetail where driveshiftid = '623044')) 

結果是5.哪個是合乎邏輯的,3個人員配備+ 1個有3個角色的人員。

因此,這是我遇到了麻煩,我只是想不同的人的ID數:

(select distinct count (personid) from rpt_peoplestaffingroledetail where roleid in 
(select roleid from rpt_peopleroledef where affectdash = 1) and shiftid in 
(select shiftid from rpt_staffingeventshiftdetail where driveshiftid = '623044')) 

而結果在這裏設置仍然是5。而且我相信它應該是3,但我想不出爲什麼它是錯的。

對此提出建議?

回答

4

select distinct count (personid)表示獲得一個計數(這只是一個數字),然後是一個清晰的列表(仍然是一個數字)。

您需要select count (distinct personid)這意味着要計數不同的personid

+1

太棒了,非常感謝。 – MISNole