How do I generate this sql query?

"0" - VisitingCount - numeric Datepart value, for example: 00: 00-1, 00: 30-2, 01: 00-2, 01: 30-3


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), [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_SecuristLog.Date 
      

My query is returning below the table


0   1
0 2
..(removed repeating)..
0 45
0 46
0 47
825 23
526 21
1064 24
885 22




This is my dream table. I need this:



0   1
0 2
..(removed repeating)..
0 19
0 20
526 21
885 22
825 23
1064 24
0 25
0 26
..(removed repeating)..
0 46
0 47


0


a source to share


1 answer


add Order By 2 desc

to your choice

  select Sum(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 desc

      



See Example A from UNION (Transact-SQL)

+3


a source







All Articles