views:

36

answers:

2

This query give me error:Msg 156, Level 15, State 1, Line 10 Incorrect syntax near the keyword 'where'.

declare @date1 datetime,@date2 datetime , @COUNT INT , @countgap int, @order int
seLECT @date1='2009-05-11' , @date2 = '2009-05-12'
seLECT @countgap = 30 , @COUNT = 0, @order=23 

select VisitingCount from (
select count(page) as VisitingCount,   
    (datepart(hour,Date)*60+datepart(minute,Date))/@countgap as OrderNumber 
    from scr_SecuristLog  
    where Date between @date1 and  @date2
    GROUP BY (datepart(hour,Date)*60+datepart(minute,Date))/@countgap) where OrderNumber=@order
+3  A: 

Pherhaps:

GROUP BY (datepart(hour,Date)*60+datepart(minute,Date))/@countgap) 
HAVING OrderNumber=@order

or:

where (Date between @date1 and  @date2) AND OrderNumber=@order
GROUP BY (datepart(hour,Date)*60+datepart(minute,Date))/@countgap)
PQW
+2  A: 

You need to give your derived table a name e.g. DT1. Here I've reformatted the SQL text to my personal liking :) to make it easier to read:

select DT1.VisitingCount 
  from (
        select count(page) as VisitingCount,   
               (datepart(hour,Date)*60+datepart(minute,Date))/@countgap 
                  as OrderNumber 
          from scr_SecuristLog  
         where Date between @date1 and  @date2
         GROUP 
            BY (datepart(hour,Date)*60+datepart(minute,Date))/@countgap
       ) AS DT1 
 where DT1.OrderNumber=@order
onedaywhen