-1

每次用户登录时,站点日志都会记录一个日期时间戳。我需要一个查询来计算用户登录的天数。仅仅对 [DateTime] 字段进行计数是行不通的,因为用户每天可以登录多次。所以,我很困惑如何计算他们登录的不同天数。基本上,我们需要查询来生成:

UserID
NumberOfDaysLoggedIn

例如,用户 John (ID 33) 登录如下:

3/26/2008 12:20 PM
3/26/2008 5:58 PM
3/27/2008 11:46 AM

我们想要的结果是:

UserID    NumberofDaysLoggedIn
    33                       2

使用 oracle query 产生该结果的任何想法。请提出任何想法

4

4 回答 4

3

你应该做的是围绕日期,然后将它们放在不同的位置。

圆形日期的功能是trunc()

你可以这样做:

select count(*)
from (select name, trunc(date)
      from table
      group by name, trunc(date))
where name = 'John';

如果您想获得每个用户的结果,您可以这样做:

select name, count(*)
from (select distinct name, trunc(date)
      from table)
group by name;
于 2011-08-04T10:13:04.387 回答
2

你需要做类似的事情;

select userID, count(distinct trunc(date))
from table
于 2011-08-04T10:07:04.967 回答
0

您可以计算不同的日期,类似于:

select count(distinct trunc(t.date))
  from table t
 where t.userId = some user id

trunc()将日期截断为日期值,但也可以采用格式参数来截断为特定的度量单位。

于 2011-08-04T10:13:41.383 回答
0
with t as 
(
select 1 as id, sysdate as d from dual
union all
select 1 as id, sysdate - 0.5 as d from dual
union all
select 1 as id, sysdate - 0.6 as d from dual
union all
select 1 as id, sysdate - 1 as d from dual
union all
select 2 as id, sysdate - 1 as d from dual
)
select id, count(distinct trunc(d))
from t 
group by id
;
于 2011-08-04T10:39:49.473 回答