&#39 ;;'附近的语法不正确SQL

时间:2014-04-18 15:07:25

标签: sql tsql sql-server-2012

我查询了我正在处理的存储过程,如下所示。我在';'附近有一个不正确的语法,这是在我的查询的最后。还是T-sql的新手,无法弄清楚为什么我收到这个错误。我有一种感觉它与我有子查询,但我不确定。如何更改此项以允许执行?

Declare @startTime DateTime
Declare @endTime DateTime

SET @startTime = '2014-03-28 8:00:00';
SET @endTime = '2014-03-28 17:00:00';

SELECT sum(datepart(hour, HoursRan) * 3600 + datepart(minute, HoursRan) * 60 +      datepart(second, HoursRan)) as TotalHoursRan, sum(datepart(hour, DownTime) * 3600 + datepart(minute, DownTime) * 60 + datepart(second, DownTime)) as TotalDownTime, (TotalDownTime / HoursRan) as PercentDown
from (select cast(@endTime - @startTime as time) AS HoursRan
from (select cast(max(DateAndTime) - min(DateAndTime) as time) as DownTime
    from (select pt.*,
         sum(case when datediff(second, prevdt, DateAndTime) <= 1 then 0 else 1 end) over 
             (order by DateAndTime) as grp
        from (select pt.*, lag(DateAndTime) over (order by DateAndTime) as prevdt
            from IncomingProductTracker pt
                where Line4AFaultStatus = 1 and
                DateAndTime > '2014-03-28 8:00:00' and
                DateAndTime < '2014-03-28 17:00:00'
       ) pt
 ) pt
group by grp) pt;

2 个答案:

答案 0 :(得分:1)

正如其他人所说,您可以使用不同的别名来更轻松地查找此类错误。在这种情况下,您缺少一个子查询的名称。

DECLARE @startTime DATETIME
DECLARE @endTime DATETIME

SET @startTime = '2014-03-28 8:00:00';
SET @endTime = '2014-03-28 17:00:00';

SELECT  SUM(DATEPART(hour, HoursRan) * 3600 + DATEPART(minute, HoursRan) * 60
            + DATEPART(second, HoursRan)) AS TotalHoursRan
      , SUM(DATEPART(hour, DownTime) * 3600 + DATEPART(minute, DownTime) * 60
            + DATEPART(second, DownTime)) AS TotalDownTime
      , ( TotalDownTime / HoursRan ) AS PercentDown
FROM    ( SELECT    CAST(@endTime - @startTime AS TIME) AS HoursRan
          FROM      ( SELECT    CAST(MAX(DateAndTime) - MIN(DateAndTime) AS TIME) AS DownTime
                      FROM      ( SELECT    pt.*
                                          , SUM(CASE WHEN DATEDIFF(second,
                                                              prevdt,
                                                              DateAndTime) <= 1
                                                     THEN 0
                                                     ELSE 1
                                                END) OVER ( ORDER BY DateAndTime ) AS grp
                                  FROM      ( SELECT    pt.*
                                                      , lag(DateAndTime) OVER ( ORDER BY DateAndTime ) AS prevdt
                                              FROM      IncomingProductTracker pt
                                              WHERE     Line4AFaultStatus = 1
                                                        AND DateAndTime > '2014-03-28 8:00:00'
                                                        AND DateAndTime < '2014-03-28 17:00:00'
                                            ) pt
                                ) pt
                      GROUP BY  grp
                    ) pt
        ) pt;

使用不同的别名:

DECLARE @startTime DATETIME
DECLARE @endTime DATETIME

SET @startTime = '2014-03-28 8:00:00';
SET @endTime = '2014-03-28 17:00:00';

SELECT  SUM(DATEPART(hour, HoursRan) * 3600 + DATEPART(minute, HoursRan) * 60
            + DATEPART(second, HoursRan)) AS TotalHoursRan
      , SUM(DATEPART(hour, DownTime) * 3600 + DATEPART(minute, DownTime) * 60
            + DATEPART(second, DownTime)) AS TotalDownTime
      , ( TotalDownTime / HoursRan ) AS PercentDown
FROM    ( SELECT    CAST(MAX(DateAndTime) - MIN(DateAndTime) AS TIME) AS DownTime
                  , CAST(@endTime - @startTime AS TIME) AS HoursRan
          FROM      ( SELECT    a.*
                              , SUM(CASE WHEN DATEDIFF(second, prevdt,
                                                       DateAndTime) <= 1
                                         THEN 0
                                         ELSE 1
                                    END) OVER ( ORDER BY DateAndTime ) AS grp
                      FROM      ( SELECT    pt.*
                                          , lag(DateAndTime) OVER ( ORDER BY DateAndTime ) AS prevdt
                                  FROM      IncomingProductTracker pt
                                  WHERE     Line4AFaultStatus = 1
                                            AND DateAndTime > '2014-03-28 8:00:00'
                                            AND DateAndTime < '2014-03-28 17:00:00'
                                ) a
                    ) b
          GROUP BY  grp
        ) c; 

答案 1 :(得分:0)

我认为declare语句应以分号结束。

http://technet.microsoft.com/en-us/library/ms188927.aspx