我如何离开外部连接这些#temp(默认数据)和另一个表?

时间:2009-05-08 11:21:04

标签: sql sql-server sql-server-2005 tsql

我尝试使用查询(下面)生成一个表(查看表1)。

CREATE TABLE #temp(
[VisitingCount] int,
[Time] nvarchar(50) )
DECLARE @DateNow DATETIME,@i int

SET @DateNow='00:00'
set @i=1;


    while(@i<1440)
begin
    set @DateNow=DATEADD(minute, 1, @DateNow)
    insert into #temp ([VisitingCount], [Time]) values(0, right(left(convert(nvarchar, @DateNow, 121), 16), 5))
    set @i=@i+1
end


select Count(VisitingCount) as VisitingCount,[Time]      
from   
#temp as Alltimes
left outer join   
( SELECT Page,Date,[user],      
        dbo.fn_GetActivityLogArranger2(Date,'hour') as [Time]
        FROM scr_SecuristLog      
) scr_SecuristLog      
on Alltimes.[Time] = scr_SecuristLog.[Time]
where      
        Date between '2009-04-30' and '2009-05-02'      
and      
        [user] in      
(       select USERNAME      
        from scr_CustomerAuthorities      
        where customerID=Convert(varchar,4)      
        and ID=Convert(varchar,43)      
)      
group by [Time] order by [Time] asc
drop table #temp


<小时/>

我需要这个结果(如下)。这个代码关于系统入口日志, 但是在12:30到13:30之间有一顿午餐。 所以在12:30到13:30之间不要进入我们的系统。 如果你绘制日志Graghic。由于突破间隙,你可以看到“M”字符

我的梦想表1结果:

计数--------- ---------时间
10 ------------ 10:30
22 ------------ 10:40
30 ------------ 10:50
44 ------------ 11:00





0 ------------- 12:30
0 ------------- 12:40
0 ------------- 12:50
0 ------------- 13:00





10 ------------- 16:00
21 ------------- 16:10
56 ------------- 16:20
46 ------------- 16:30

1 个答案:

答案 0 :(得分:2)

这是你的where子句,是什么过滤了一些时间范围。 您需要为内部选择设置该子句:

select Count(VisitingCount) as VisitingCount,[Time]      
from   
#temp as Alltimes
left outer join   
( SELECT Page,Date,[user],      
        dbo.fn_GetActivityLogArranger2(Date,'hour') as [Time]
        FROM scr_SecuristLog      
        where Date between '2009-04-30' and '2009-05-02'      
        and      
        [user] in      
        (       select USERNAME      
                from scr_CustomerAuthorities      
                where customerID=Convert(varchar,4)      
                and ID=Convert(varchar,43)      
        )      
) scr_SecuristLog      
on Alltimes.[Time] = scr_SecuristLog.[Time]
group by [Time] order by [Time] asc