2011-08-04 52 views
-1

每次用戶登錄時,站點日誌都會記錄一個日期時間戳記。我需要一個查詢來計算用戶登錄的天數。只需在[DateTime]字段上進行計數即可無效因爲用戶每天可以登錄多次。所以,我很爲難如何僅計算不同天,他們登錄基本上,我們需要查詢產生:如何編寫查詢來計算用戶登錄的天數?

UserID 
NumberOfDaysLoggedIn 

例如,用戶約翰(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查詢結果。請提出任何想法

回答

2

你需要做類似的事情;

select userID, count(distinct trunc(date)) 
from table 
3

你應該做的是圍繞日期,然後把它們放在不同的位置。

圓形日期的函數是trunc()。沿着

select name, count(*) 
from (select distinct name, trunc(date) 
     from table) 
group by name; 
0

你可以統計不同日期,東西:

你可以做這樣的:

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

,如果你想獲得每個用戶的結果,你可以那樣做

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

trunc()截斷日期當日的價值,但也可以採取形式:的行在參數截斷到一個特定的度量單位。

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 
; 
相關問題