SQL查询中的group by导致输出差异

SQL查询中的group by导致输出差异,sql,oracle,group-by,sql-order-by,Sql,Oracle,Group By,Sql Order By,我有一个查询,它给出了一周内表中有活动的用户总数。看起来像这样 select count(distinct(t.employeeid)) as "User Count" from t, g where t.eventdate between '07-SEP-13' AND '14-SEP-13' and t.resourceid=g.id and g.resourcename = 'XYZ'` select count(distinct(t.employeeid)) as "User

我有一个查询,它给出了一周内表中有活动的用户总数。看起来像这样

select count(distinct(t.employeeid)) as "User Count" 
from t, g  
where t.eventdate between '07-SEP-13' AND '14-SEP-13' 
and t.resourceid=g.id and g.resourcename = 'XYZ'`
select count(distinct(t.employeeid)) as "User Count", EXTRACT(DAY from t.eventdate) as      "Day" 
from t, g  
where t.eventdate between '07-SEP-13' AND '14-SEP-13' 
and t.resourceid=g.id and g.resourcename = 'XYZ' 
group by EXTRACT(DAY from t.eventdate)
order by EXTRACT(DAY from t.eventdate) ASC;`
我想知道在同一时间段内每天进行活动的用户数。因此,我对查询进行了如下修改

select count(distinct(t.employeeid)) as "User Count" 
from t, g  
where t.eventdate between '07-SEP-13' AND '14-SEP-13' 
and t.resourceid=g.id and g.resourcename = 'XYZ'`
select count(distinct(t.employeeid)) as "User Count", EXTRACT(DAY from t.eventdate) as      "Day" 
from t, g  
where t.eventdate between '07-SEP-13' AND '14-SEP-13' 
and t.resourceid=g.id and g.resourcename = 'XYZ' 
group by EXTRACT(DAY from t.eventdate)
order by EXTRACT(DAY from t.eventdate) ASC;`

但我在总数上有所不同。第二个查询比第一个查询多出大约80%。有人能告诉我哪里出了问题,哪个查询有问题吗?提前感谢。

看起来第二次员工事件和第一次员工事件的数量都在统计中


员工在一天内发生事件的次数将被计算。

您正在计算不同的员工ID,并且您的员工ID必须包含多个事件日期。在您的第一个查询中,他们将被计算为一个人,但在您的第二个查询中,他们将被计算为每个eventdate的一个人。

@Dave,Bob-感谢您的回答。我知道问题出在哪里了。我试图做的事情本身似乎在逻辑上是错误的。我可以对整个期间进行不同的计数,也可以对每天进行不同的计数。但是,由于同一用户可以在另一天进行活动,因此每天的不同计数可能会更多。