0

这是我的访问查询。我返回的每个值都是必需的(不幸的是)。然而,这个查询最糟糕的部分。是它有时会错过符合 WHERE 子句中条件的行。我尝试进行的每项优化都无济于事,或者无法在 Access 中完成(了解 SQL)。如您所见,WHERE 子句在三个日期字段之间进行检查。这是棘手的部分。对于 Inventory Masterfile 中的每一行信息,Inventory Stores 中有五行(每个“BranchID”一个),并且我在 where 子句中检查的日期列可以在这五个中的任何一个中更新日期行,到 Inventory Masterfile 中的一行。有没有人有任何可以帮助我的建议?我已经调试了好几个星期,对此我束手无策。

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 回答