How can data from a query be evaluated multiple times?

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 Sum(VisitingCount)as VisitingCount, [Time] from #Temp group by [Time] Union All select count(page) as VisitingCount, (datepart(hour,Date)*60+datepart(minute,Date))/30 as [Time] from scr_SecuristLog where Date between '2009-05-04 10:30' and '2009-05-04 12:30' GROUP BY (datepart(hour,Date)*60+datepart(minute,Date))/30--scr order by 2 asc


This query creates this table for me, but if you look below "22", it is repeated twice. in this situation. i have to reset the value to 0. i need

 VisitingCount ------------ Time ----------
Repeating ------------------ Repeating -----
Repeating ------------------ repeating -----
repeating ------------------ repeating -----
 526 ----------------------- 21 ----------
0 --------------- ----------- 22 ----------
0 -------------------------- 22 ----------
0 -------------------------- 23 ----------
825 - ----------------------- 23 ----------
Repeating --------------- --- repeating -----
repeating ------------------ repeating -----

I need this table

 VisitingCount ------------ Time ----------
Repeating ------------------ Repeating -----
Repeating ------------------ repeating -----
repeating ------------------ repeating -----
 526 ----------------------- 21 ----------
0 --------------- ----------- 22 ----------
825 ------------------------ 23 - --------
Repeating ------------------ Repeating -----
Repeating ---------------- - repeating -----
0


a source to share


1 answer


Create another nesting level:



select Sum(VisitingCount)as VisitingCount, [Time]
from (
  select Sum(VisitingCount)as VisitingCount, [Time]
    from #Temp group by [Time]
  Union All
    select count(page) as VisitingCount, 
    (datepart(hour,Date)*60+datepart(minute,Date))/30 as [Time]
    from scr_SecuristLog
    where Date between '2009-05-04 10:30' and '2009-05-04 12:30'
    GROUP BY (datepart(hour,Date)*60+datepart(minute,Date))/30--scr
  ) X
group by [Time]
order by 2 asc

      

+1


a source







All Articles