我得到一个语法错误附近和关键字

时间:2017-12-07 10:46:47

标签: sql-server

SELECT   wrhwr.bl_id, 
         Count(wrhwr.wr_id) AS ct_wr_id 
FROM     ( 
                SELECT wrhwr.bl_id, 
                       wrhwr.wr_id, 
                       wrhwr.site_id, 
                       wrhwr.date_requested 
                FROM   wrhwr 
                WHERE 
                And    ( 
                              wrhwr.bl_id LIKE 'F0006440-B0001%') 
                AND    wrhwr.bl_id IN 
                       ( 
                                SELECT TOP 10 
                                         q.bl_id 
                                FROM     ( 
                                                  SELECT   bl_id, 
                                                           Count(wr_id) AS wrcnt 
                                                  FROM     afm.wrhwr 
                                                  GROUP BY bl_id) AS q 
                                WHERE    wrhwr.[status] IN ('Com', 
                                                            'I', 
                                                            'HL', 
                                                            'HP', 
                                                            'S') 
                                ORDER BY q.wrcnt DESC))wrhwr 
WHERE    (( 
                  And      ( 
                                    wrhwr.bl_id LIKE 'F0006440-B0001%') )) 
AND      ( 
                  wrhwr.site_id IN 
                  ( 
                         SELECT site_id 
                         FROM   site 
                         WHERE  state_id = 'JHB') 
         OR       wrhwr.site_id IS NULL) 
GROUP BY wrhwr.bl_id 
ORDER BY wrhwr.bl_id DESC

2 个答案:

答案 0 :(得分:0)

您已将AND + WHERE用于提供错误的同一语句 像这样

SELECT wrhwr.bl_id, 
                       wrhwr.wr_id, 
                       wrhwr.site_id, 
                       wrhwr.date_requested 
                FROM   wrhwr 
                WHERE 
                And    ( 
                              wrhwr.bl_id LIKE 'F0006440-B0001%') 

此处不需要AND,因为您只有1个过滤条件,请尝试此

SELECT   wrhwr.bl_id,
         Count(wrhwr.wr_id) AS ct_wr_id
FROM     (
                SELECT wrhwr.bl_id,
                       wrhwr.wr_id,
                       wrhwr.site_id,
                       wrhwr.date_requested
                FROM   wrhwr
                WHERE wrhwr.bl_id LIKE 'F0006440-B0001%'
                AND    wrhwr.bl_id IN
                       (
                                SELECT TOP 10
                                         q.bl_id
                                FROM     (
                                                  SELECT   bl_id,
                                                           Count(wr_id) AS wrcnt
                                                  FROM     afm.wrhwr
                                                  GROUP BY bl_id) AS q
                                WHERE    wrhwr.[status] IN ('Com',
                                                            'I',
                                                            'HL',
                                                            'HP',
                                                            'S')
                                ORDER BY q.wrcnt DESC)
        )wrhwr
WHERE   wrhwr.bl_id LIKE 'F0006440-B0001%'
AND      (
                  wrhwr.site_id IN
                  (
                         SELECT site_id
                         FROM   site
                         WHERE  state_id = 'JHB')
         OR       wrhwr.site_id IS NULL)
GROUP BY wrhwr.bl_id
ORDER BY wrhwr.bl_id DESC

答案 1 :(得分:0)

这至少是可读的......

SELECT wrhwr.bl_id, 
       Count(wrhwr.wr_id) AS ct_wr_id 
FROM ( SELECT wrhwr.bl_id, 
              wrhwr.wr_id, 
              wrhwr.site_id, 
              wrhwr.date_requested 
        FROM   wrhwr 
        WHERE AND ( wrhwr.bl_id LIKE 'F0006440-B0001%') 
          AND wrhwr.bl_id IN  ( SELECT TOP 10 q.bl_id 
                                FROM ( SELECT bl_id, 
                                              Count(wr_id) AS wrcnt 
                                       FROM afm.wrhwr 
                                       GROUP BY bl_id) AS q 
                                WHERE wrhwr.[status] IN ('Com','I','HL','HP','S') 
                                ORDER BY q.wrcnt DESC))wrhwr 
WHERE ((And(wrhwr.bl_id LIKE 'F0006440-B0001%') )) 
  AND ( wrhwr.site_id IN ( SELECT site_id 
                               FROM [site]
                               WHERE  state_id = 'JHB') 
   OR wrhwr.site_id IS NULL)
GROUP BY wrhwr.bl_id 
ORDER BY wrhwr.bl_id DESC;

然后,因为它的可读,您可以轻松查看问题所在(我已经注释掉了)。

SELECT wrhwr.bl_id, 
       Count(wrhwr.wr_id) AS ct_wr_id 
FROM ( SELECT wrhwr.bl_id, 
              wrhwr.wr_id, 
              wrhwr.site_id, 
              wrhwr.date_requested 
        FROM   wrhwr 
        WHERE /*AND ( */wrhwr.bl_id LIKE 'F0006440-B0001%'/*) */
          AND wrhwr.bl_id IN  ( SELECT TOP 10 q.bl_id 
                                FROM ( SELECT bl_id, 
                                              Count(wr_id) AS wrcnt 
                                       FROM afm.wrhwr 
                                       GROUP BY bl_id) AS q 
                                WHERE wrhwr.[status] IN ('Com','I','HL','HP','S') 
                                ORDER BY q.wrcnt DESC))wrhwr 
WHERE /*((And(*/wrhwr.bl_id LIKE 'F0006440-B0001%'/*) )) */
  AND /*( */wrhwr.site_id IN ( SELECT site_id 
                               FROM [site]
                               WHERE  state_id = 'JHB') 
   OR wrhwr.site_id IS NULL/*)*/
GROUP BY wrhwr.bl_id 
ORDER BY wrhwr.bl_id DESC;

但是,我说实话,这个查询仍然很糟糕,但在我的SQL OCD开始后,至少修复了"看起来"对于查询,我没兴趣使其可用。 :)