0

以下是我在 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] 
4

1 回答 1

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] 
于 2012-12-19T11:52:53.283 回答