declare
@cmdtext
varchar
(8000
);declare
@userindex
int;
set@cmdtext=''
;set
@userindex=0
;while
@userindex
<
30beginif(
@userindex
!=29
)select
@cmdtext
=@cmdtext+'
select convert(varchar(10), create_time, 120) as create_time,
datediff(d, create_time, last_login_time) loginday
from '+
'dbo.user'+
convert
(varchar
,@userindex) +
'union all'+
char(10
); --
換行else
select
@cmdtext
=@cmdtext+'
select convert(varchar(10), create_time, 120) as create_time,
datediff(d, create_time, last_login_time) loginday
from '+
'dbo.user'+
convert
(varchar
,@userindex
) ;set
@userindex
=@userindex+1
;end
;select
@cmdtext='
select u.create_time,sum(registernum) as registernum,
sum(firstday) as firstday, sum(twoday) as twoday,
sum(thirdday) as thirdday, sum(fourday) as fourday,
sum(fiveday) as fiveday,sum(sixday) as sixday,
sum(sevenday) as sevenday, sum(others) as others
from (
select create_time, count(create_time) as registernum,
case when loginday = 0 then count(create_time) else 0 end as firstday,
case when loginday = 1 then count(create_time) else 0 end as twoday,
case when loginday = 2 then count(create_time) else 0 end as thirdday,
case when loginday = 3 then count(create_time) else 0 end as fourday,
case when loginday = 4 then count(create_time) else 0 end as fiveday,
case when loginday = 5 then count(create_time) else 0 end as sixday,
case when loginday = 6 then count(create_time) else 0 end as sevenday,
case when loginday > 6 then count(create_time) else 0 end as others
from
(select t.create_time, t.loginday
from('
+@cmdtext+'
) t) tt
group by tt.create_time, tt.loginday
) ugroup by u.create_time
order by u.create_time';
--print @cmdtext
exec
(@cmdtext
);
本來如果到此為止,問題就完了,但是這個資料庫例項所在伺服器是美國時間,我們要按中國時間來統計,所以我就把上面其中的一段的指令碼做了如下改寫
@userindex
!=29
)select
@cmdtext
=@cmdtext+'
select convert(varchar(10), dateadd(hh, 15,create_time), 120) as create_time,
datediff(d, create_time, last_login_time) loginday
from '+
'dbo.user'+
convert
(varchar
,@userindex) +
'union all'+
char(10
); --
換行else
select
@cmdtext
=@cmdtext+'
select convert(varchar(10), dateadd(hh, 15,create_time), 120) as create_time,
datediff(d, create_time, last_login_time) loginday
from '+
'dbo.user'+
convert
(varchar
,@userindex
) ;set
@userindex
=@userindex+1
; 下面問題來了,如圖所示(執行時間時間是2007-7-26),怎麼2010-7-26的資料在twoday列是198,2010-7-25註冊的使用者第三天還有148個登入,明顯錯了
乙個錯誤
select
datediff
(d,
'2010-7-25 0:30',
'2010-7-26 23:59')
--加上15個小時後
select
datediff
(d,
'2010-7-25 15:30',
'2010-7-27 15:59')
上面查詢結果是1 和 2 ,問題就出在這裡。所以把指令碼改寫後,就ok了。如果以後碰到類似問題,一定要注意!
日期函式 datediff
命令格式 datediff datetime1,datetime2,datepart 用途 計算兩個時間的差值,並轉換成指定的單位,如 秒。引數說明 datetime1 datetime2 datetime型別,被減數和減數,若輸入為string型別會隱式轉換為datetime型別後參與運算,其它型...
DateDiff 函式詳解
描述 返回兩個日期之間的時間間隔。語法datediff interval,date1,date2 firstdayofweek firstweekofyear datediff 函式的語法有以下引數 interval 必選。字串表示式,表示用於計算 date1 和 date2 之間的時間間隔。有關數...
我使用DateDiff函式
最近做的 專案,多使用者,要求發文章的時候有積分獎勵,修改文章時也有相同獎勵。但要求每天修改同乙個文章只能獎勵一次,不管修改多少次。我用資料庫儲存過程來實現此功能 判斷當前時間與其最晚修改時間是否相差一天以上,是的話可以加分,反之不加 用到乙個函式 datediff d 時間1,時間2 函式介紹 返...