views:

122

answers:

2

How can i join ( or union) #Temp with scr_SecuristLog Time. Look please [Time] column


CREATE TABLE #Temp (VisitingCount int, [Time] int )

 DECLARE @DateNow DATETIME,@i int,@Time int
    set @DateNow='00:00'  
    set @i=1;  
    while(@i<48)  
        begin  
set @DateNow = DATEADD(minute, 30, @DateNow)
set @Time = (datepart(hour,@DateNow)*60+datepart(minute,@DateNow))/30 
insert into #Temp(VisitingCount,[Time]) values(0,@Time )
set @i=@i+1
end





select Count(Page) as VisitingCount,[Time]      
from     
( SELECT Page,Date,[user],      
        (datepart(hour,Date)*60+datepart(minute,Date))/30 as [Time]        
        FROM scr_SecuristLog      
) scr_SecuristLog      
where      
        Date between '2009-05-04' and '2009-05-05'    

group by [Time]  order by [Time]  asc        
return
+1  A: 

EDIT: Added the GROUP BY Clause to the inner query and added table aliases to the SELECT statement

Here's what your join syntax might look like:

CREATE TABLE #Temp (VisitingCount int, [Time] int )
--
--
-- Insert logic here
--
--
select t.VisitingCount, t.[Time]
from #Temp as t
inner join (
    select count(page) as VisitingCount, (datepart(hour,Date)*60+datepart(minute,Date))/10 as [Time]
    from scr_SecuristLog
    where Date between '2009-05-04' and '2009-05-05'
    group by Date
) as s
    on t.VisitingCount = s.VisitingCount
        and t.Time = s.Time
Justin Niessner
Justin; Your codes give me error:
Phsika
Msg 8120, Level 16, State 1, Line 1Column 'scr_SecuristLog.Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Phsika
Msg 8120, Level 16, State 1, Line 1Column 'scr_SecuristLog.Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.Msg 209, Level 16, State 1, Line 1Ambiguous column name 'VisitingCount'.Msg 209, Level 16, State 1, Line 1Ambiguous column name 'Time'.
Phsika
Updated my answer to attempt to fix your errors...it's hard since I don't have the tables available to me.
Justin Niessner
A: 

Since you're using SQL 2005, Instead of the temp table you might want to use a common table expression:

DECLARE
    @date_start DATETIME,
    @date_end DATETIME

SET @date_start = '2009-05-04'
SET @date_end = '2009-05-04'

;WITH Times (start_time, end_time) AS
(
    SELECT
     @date_start AS start_time,
     @date_start AS end_time
    UNION ALL
    SELECT
     DATEADD(mi, 30, start_time) AS start_time,
     DATEADD(mi, 30, end_time) AS end_time
    FROM
     Times
    WHERE
     end_time <= DATEADD(dy, 1, @date_end)
)
SELECT
    start_time,
    end_time,
    COUNT(*)
FROM
    Times T
INNER JOIN dbo.scr_SecuristLog SL ON
    SL.date >= T.start_time AND
    SL.date < T.end_time
GROUP BY
    start_time,
    end_time
Tom H.