0

これが私のアクセスクエリです。私が返す値はすべて必須です (残念ながら)。ただし、このクエリの最悪の部分です。WHERE 句の条件に一致する行を見逃すことがあるということです。私が試みたすべての最適化は役に立たないか、Access では実行できません (SQL を知っている)。ご覧のとおり、WHERE 句は 3 つの日付フィールドをチェックします。ここがトリッキーな部分です。Inventory Masterfile の 1 行の情報ごとに、Inventory Stores に 5 行 (「BranchID」ごとに 1 行) があり、where 句でチェックしている Date 列では、これらの 5 つのいずれかで日付が更新されている可能性があります。行、在庫マスターファイルの 1 つの行に。誰にも私を助けることができる提案はありますか? 私は何週間もデバッグしてきましたが、これについては私の知恵の終わりです。

SELECT a.Item1,im.Secondary,im.Description,im.Author_Artist,im.RetailPrice,im.CatalogID,im.Publisher, im.Binding,im.PubDate,im.Grouping,im.CategoryId,im.Alt_Category,

(SELECT MAX(ls.OnHand) FROM [Inventory Store] ls WHERE (ls.Item1 = a.Item1 AND ls.BranchID = 5)) AS 1Stock,

(SELECT MAX(ls.OnHand) FROM [Inventory Store] ls WHERE (ls.Item1 = a.Item1 AND ls.BranchID = 3)) AS 2Stock,

(SELECT MAX(ls.OnHand) FROM [Inventory Store] ls WHERE (ls.Item1 = a.Item1 AND ls.BranchID = 4)) AS 3Stock,

(SELECT MAX(ls.Maximum) FROM [Inventory Store] ls WHERE (ls.Item1 = a.Item1 AND ls.BranchID = 4)) AS Maximum,

(SELECT MAX(ls.LastSold1) FROM [Inventory Store] ls WHERE (ls.Item1 = a.Item1)) AS LastSold1,

(SELECT MAX(ls.LastUpdate) FROM [Inventory Store] ls WHERE (ls.Item1 = a.Item1)) AS LastUpdate,

(SELECT MAX(ls.LastReceived) FROM [Inventory Store] ls WHERE (ls.Item1 = a.Item1)) AS LastReceived

FROM [Inventory Store] AS a

INNER JOIN [Inventory Masterfile] im ON a.Item1 = im.Item1

WHERE (a.LastSold1 > #6/12/2012# OR a.LastUpdate > #6/12/2012# OR a.LastReceived > #6/12/2012#)

GROUP BY a.Item1, im.Secondary, im.Description, im.Author_Artist, im.RetailPrice, 
im.CatalogID,im.Publisher, im.Binding,im.PubDate,im.Grouping,im.CategoryId,im.Alt_Category
4

1 に答える 1

1

これを試してみてください。サブクエリがあり、遅いです。

SELECT a.item1,
       im.secondary,
       im.description,
       im.author_artist,
       im.retailprice,
       im.catalogid,
       im.publisher,
       im.binding,
       im.pubdate,
       im.grouping,
       im.categoryid,
       im.alt_category,
       z.[1stock],
       y.[2stock],
       x.[3stock],
       w.mmaximum,
       v.mlastsold1,
       u.mlastupdate,
       t.mlastreceived
FROM   (((((((([inventory store] AS a
               LEFT JOIN (SELECT ls.item1,
                                 Max(ls.onhand) AS [1Stock]
                          FROM   [inventory store] ls
                          WHERE  ls.branchid = 5
                          GROUP  BY ls.item1) AS z
                      ON a.item1 = z.item1)
              LEFT JOIN (SELECT ls.item1,
                                Max(ls.onhand) AS [2Stock]
                         FROM   [inventory store] ls
                         WHERE  ls.branchid = 3
                         GROUP  BY ls.item1) AS y
                     ON a.item1 = y.item1)
             LEFT JOIN (SELECT ls.item1,
                               Max(ls.onhand) AS [3Stock]
                        FROM   [inventory store] ls
                        WHERE  ls.branchid = 4
                        GROUP  BY ls.item1) AS x
                    ON a.item1 = x.item1)
            LEFT JOIN (SELECT ls.item1,
                              Max(ls.maximum) AS [mMaximum]
                       FROM   [inventory store] ls
                       WHERE  ls.branchid = 4
                       GROUP  BY ls.item1) AS w
                   ON a.item1 = w.item1)
           LEFT JOIN (SELECT ls.item1,
                             Max(ls.lastsold1) AS mLastSold1
                      FROM   [inventory store] ls
                      GROUP  BY ls.item1) AS v
                  ON a.item1 = v.item1)
          LEFT JOIN (SELECT ls.item1,
                            Max(ls.lastupdate) AS mLastUpdate
                     FROM   [inventory store] ls
                     GROUP  BY ls.item1) AS u
                 ON a.item1 = u.item1)
         LEFT JOIN (SELECT ls.item1,
                           Max(ls.lastreceived) AS mLastReceived
                    FROM   [inventory store] ls
                    GROUP  BY ls.item1) AS t
                ON a.item1 = t.item1)
        LEFT JOIN [inventory masterfile] im
               ON a.item1 = im.item1)
WHERE  ( a.lastsold1 > #2012/6/12#
          OR a.lastupdate > #2012/6/12#
          OR a.lastreceived > #2012/6/12# )
GROUP  BY a.item1,
          im.secondary,
          im.description,
          im.author_artist,
          im.retailprice,
          im.catalogid,
          im.publisher,
          im.binding,
          im.pubdate,
          im.grouping,
          im.categoryid,
          im.alt_category,
          z.[1stock],
          y.[2stock],
          x.[3stock],
          w.mmaximum,
          v.mlastsold1,
          u.mlastupdate,
          t.mlastreceived
于 2012-06-14T15:38:33.703 に答える