Friday, May 6, 2011

How to generate this sql query?

"0" is VisitingCount ---Numeric Value Datepart, 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 return below 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
From stackoverflow
  • add Order By 2 desc to your select

      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 in from UNION (Transact-SQL)

    Phsika : instead of "GROUP BY (datepart(hour,Date)*60+datepart(minute,Date))/30"; how to change GROUP BY [Time]
    Phsika : However, not fully accurate you query.
    Dead account : Oh- should it have been ascending? I always forget, guess and edit :)
    Phsika : No, i want to add comment section in this question but; i added new one look please?http://stackoverflow.com/questions/852206/how-can-evaluate-more-than-once-data-from-a-query
    Phsika : Please help me lan Quigley. This query is urgent....

0 comments:

Post a Comment