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 に答える