CREATE procedure sp_ActivityFrequencyOneUserGraph
(
@date1 nvarchar(100) ,
@date2 nvarchar(100),
@customerID int,
@user int,
@type nvarchar(50)
)
as
select Count(Page) as VisitingCount,[Time]
from
( SELECT Page,Date,[user],
dbo.fn_GetActivityLogArranger2(Date,@type) as [Time]
FROM scr_SecuristLog
) scr_SecuristLog
where
Date between @date1 and @date2
and
[user] in
( select USERNAME
from scr_CustomerAuthorities
where customerID=Convert(varchar,@customerID)
and ID=Convert(varchar,@user)
)
group by [Time] order by [Time] asc
return
我的功能:
CREATE FUNCTION [dbo].[fn_GetActivityLogArranger2]
(
@t AS datetime,
@type AS nvarchar(50)
)
RETURNS nvarchar(max)
AS
BEGIN
declare @Return nvarchar(max)
set @t = cast (@t as smalldatetime)
if(@type='hour')
begin
set @t= dateadd(minute, -(datepart(minute, @t)), @t)
end
else if(@type='halfhour')
begin
set @t=
case
when datepart(minute, @t) >= 30
then dateadd(minute, 60-(datepart(minute, @t)), @t)
else dateadd(minute, 30-(datepart(minute, @t)), @t)
end
end
else if(@type='tenminutes')
begin
set @t= case
when datepart(minute,@t)%10 >=5
then dateadd(minute,10-(datepart(minute,@t)%10),@t)
else dateadd(minute,-(datepart(minute,@t)%10),@t)
end
end
select @Return=CONVERT(VARCHAR(5),@t, 108)
Return @Return
end
如果我使用 dbo.fn_GetActivityLogArranger2('2009-04-30','小时')我的结果表
VisitingCount ---------- -----------时间
23 --------------------- 10:30 ----------
33 --------------------- 11:00 ----------
43 --------------------- 11:30 ----------
53 --------------------- 12:00 ----------
0 ---------------------- 12:30 ----------
0 ---------------------- 13:00 ----------
0 ---------------------- 13:30 ----------
23 --------------------- 14:00 ----------
33 --------------------- 14:30 ----------
37 --------------------- 15:00 ----------
但是我需要动态方法而不是文本值“当'12:30'和'13:30'之间的时间......”我需要Genarator:'12:30 ... 22:30 ...... 24: 30'---< 0,0,0,0 .....和Union(这是我的猜测)
答案 0 :(得分:1)
创建一个包含所有时间的表(您可以使用数字表并使用dateadd(mm,值* 30),但时间可能更容易)您需要在报表中覆盖并加入查询中的那个以便使用作为时间价值;这将让你填补你的空白。
创建Times表(下例中的tblTimes),并将需要报告的时间范围内的所有值插入表中。然后更改您的sql,以便从该表中进行选择并加入到scr_SecuristLog表中。以这种方式执行此操作只会获得在两个表之间映射的值;因此,如果您的scr_SecuristLog表由于某种原因而不能在您的tblTimes表中运行10.36,那么您将看不到该记录。
select Count(Page) as VisitingCount,[Time]
from
tblTimes Alltimes
left outer join
( SELECT Page,Date,[user],
dbo.fn_GetActivityLogArranger2(Date,@type) as [Time]
FROM scr_SecuristLog
) scr_SecuristLog
on Alltimes.Time = scr_SecuristLog.[Time]
where
Date between @date1 and @date2
and
[user] in
( select USERNAME
from scr_CustomerAuthorities
where customerID=Convert(varchar,@customerID)
and ID=Convert(varchar,@user)
)
group by [Time] order by [Time] asc