SQL查询导致Microsoft Access挂起

时间:2012-12-19 11:15:06

标签: sql ms-access subquery

以下是我在Microsoft Access中的SQL。我想根据子查询选择数据。

单独运行子查询是可以的。它将在30秒内返回结果。但是当我运行以下所有SQL时,ms访问没有响应。运行查询似乎很忙,但没有结果返回。

SELECT '2012/8'      AS [Period],
       monthno,
       [cluster],
       'Transfer in' AS Remark,
       Count(*)      AS [number],
       '4'           AS [sorting]
FROM   [2012],
       namesort
WHERE  monthno = 244
       AND ( div <> 'XXX'
              OR div IS NULL )
       AND hc = 1
       AND [2012].post = namesort.post
       AND monthno BETWEEN namesort.monthno_start AND namesort.monthno_end
       AND [2012].term <> 'Temporary'
       AND empid IN (SELECT empid
                     FROM   (SELECT empid,
                                    [cluster],
                                    Count(*) AS [empid no]
                             FROM   (SELECT empid,
                                            cluster
                                     FROM   [assignment]
                                            INNER JOIN yearmonth
                                                    ON [assignment].[year month]
                                                       =
                                                       yearmonth.[year month]
                                     WHERE  yearmonth.[monthno] = 243
                                            AND [assignment].hc = 1
                                            AND [assignment].term <> 'Temporary'
                                     UNION
                                     SELECT empid,
                                            [cluster]
                                     FROM   [2012]
                                     WHERE  monthno = 244
                                            AND [2012].term <> 'Temporary'
                                            AND ( div <> 'XXX'
                                                   OR div IS NULL )
                                            AND hc = 1)
                             GROUP  BY [empid],
                                       [cluster]
                             HAVING Count(*) = 1)
                     GROUP  BY empid
                     HAVING Count(*) > 1)
GROUP  BY monthno,
          [cluster] 

1 个答案:

答案 0 :(得分:1)

试试这个,在MS Access中,显式JOIN可以快得多

SELECT '2012/8'      AS [Period],
       monthno,
       [cluster],
       'Transfer in' AS Remark,
       Count(*)      AS [number],
       '4'           AS [sorting]
FROM  ( [2012] 
INNER JOIN namesort 
ON [2012].post = namesort.post )
INNER JOIN
       (SELECT empid
                     FROM   (SELECT empid,
                                    [cluster],
                                    Count(*) AS [empid no]
                             FROM   (SELECT empid,
                                            cluster
                                     FROM   [assignment]
                                            INNER JOIN yearmonth
                                                    ON [assignment].[year month]
                                                       =
                                                       yearmonth.[year month]
                                     WHERE  yearmonth.[monthno] = 243
                                            AND [assignment].hc = 1
                                            AND [assignment].term <> 'Temporary'
                                     UNION
                                     SELECT empid,
                                            [cluster]
                                     FROM   [2012]
                                     WHERE  monthno = 244
                                            AND [2012].term <> 'Temporary'
                                            AND ( div <> 'XXX'
                                                   OR div IS NULL )
                                            AND hc = 1)
                             GROUP  BY [empid],
                                       [cluster]
                             HAVING Count(*) = 1)
                     GROUP  BY empid
                     HAVING Count(*) > 1) As Emps
ON [2012].EmpID = Emps.EmpID
WHERE  monthno = 244
       AND ( div <> 'XXX'
              OR div IS NULL )
       AND hc = 1

       AND monthno BETWEEN namesort.monthno_start AND namesort.monthno_end
       AND [2012].term <> 'Temporary'

GROUP  BY monthno,
          [cluster]