我该如何评估此查询?

时间:2009-05-14 09:22:01

标签: sql sql-server sql-server-2005

此查询给出错误:消息156,级别15,状态1,行10 关键字'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

2 个答案:

答案 0 :(得分:3)

Pherhaps:

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

或:

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

答案 1 :(得分:2)

您需要为派生表提供一个名称,例如DT1。在这里,我将SQL文本重新格式化为我个人喜欢的:),以便于阅读:

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