Sql 如何将这些#temp(默认数据)与另一个表连接起来?

Sql 如何将这些#temp(默认数据)与另一个表连接起来?,sql,sql-server,sql-server-2005,tsql,Sql,Sql Server,Sql Server 2005,Tsql,我尝试用下面的查询生成一个表(请看表1) CREATE TABLE#temp( [VisitingCount]int, [时间]nvarchar(50)) 声明@DateNow DATETIME,@i int 设置@DateNow='00:00' 设置@i=1; 当(@i是where子句时,过滤某些时间范围的内容。 您需要为内部select设置该子句: select Count(VisitingCount) as VisitingCount,[Time] from #temp

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

CREATE TABLE#temp(
[VisitingCount]int,
[时间]nvarchar(50))
声明@DateNow DATETIME,@i int
设置@DateNow='00:00'
设置@i=1;

当(@i是where子句时,过滤某些时间范围的内容。 您需要为内部select设置该子句:

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

+1,where会丢弃[USER]不在子查询中的所有行,尤其是那些没有scr_SecurityLog数据的行!
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