繁体   English   中英

需要帮助了解这些SQL结果

[英]Need Help Understanding these SQL Results

我想修改一个Crystal Report,该报告可以计算每个班次的人员数量。 在查看SQL时,我发现似乎有些不对劲。 我正在复制和粘贴4个基本相同的SQL语句,除了两个计数(一个不同的计数),而其他两个列出了人员的personID(一个不同的列表)。 DISTINCT Count语句不会产生预期的结果。 希望有人可以帮助我了解发生了什么事情:

(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'))

提供:人身号码: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,但是我不知道为什么这是错误的。

建议?

select distinct count (personid)意味着先获得一个计数(仅是一个数字),然后是一个独特的列表(仍是一个数字)。

您需要select count (distinct personid) ,这意味着要计算不同的personid

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM