Microsoft Access中的SQL查询

时间:2012-08-29 09:06:09

标签: ms-access

我有用Microsoft Access编写的查询:

SELECT p.artnbr                            AS [Number], 
   p.name                              AS Name, 
   s.sizename                          AS Sizes, 
   s.sizeindex                         AS SizeIndex, 
   s.oid                               AS SizeId, 
   l.name                              AS LocationName, 
   (SELECT od.quantity 
    FROM   orderdetails od 
    WHERE  od.ORDER = (SELECT o.oid 
                       FROM   [order] o 
                       WHERE  o.active = -1 
                              AND o.location = l.oid) 
           AND od.productsize = s.oid) AS Quantity 
FROM   [size] AS s 
   INNER JOIN (product AS p 
               INNER JOIN (favorite AS f 
                           INNER JOIN location AS l 
                                   ON f.customer = l.customer) 
                       ON p.oid = f.product) 
           ON p.oid = s.product 
WHERE  f.customer = @customer 

下面的链接显示了我使用的表格及其关系。 https://dl.dropbox.com/u/18377860/QueryTables.png

此查询返回正确的结果,但正如您所见,我正在使用Sub查询来获取数量。我无法弄清楚如何使用连接语句而不是那个长子查询重写此查询。任何帮助将不胜感激 此致

编辑:为了更清楚,我的查询需要获得(产品,尺寸,位置)的所有独特组合,无论他们是否有[订单]并显示订购数量。如果特定组合的订单不存在,则数量应为空。

EDIT2: 我设法构建了查询,它看起来像这样:

SELECT p.ArtNbr                    AS [Number], 
   p.Name                          AS Name, 
   s.SizeName                      AS Sizes, 
   s.SizeIndex                     AS SizeIndex, 
   s.Oid                           AS SizeId, 
   l.Name                          AS LocationName, 
   so.qty                          AS Quantity
FROM ([Size] AS s 
         INNER JOIN (Product AS p 
               INNER JOIN (Favorite AS f 
                           INNER JOIN Location AS l 
                                   ON f.Customer = l.Customer) ON p.OID = f.Product) 
                       ON p.OID = s.Product) 
      LEFT JOIN 
          (SELECT od.ProductSize       AS PS, 
                  od.Quantity          AS qty, 
                  o.Location as Location 
           FROM OrderDetails AS od INNER JOIN [Order] AS o ON od.Order = o.OID 
           WHERE o.Active = -1)  AS so ON so.PS = s.OID
WHERE f.Customer = @customer AND (l.OID = so.Location OR so.Location is null)

我确实喜欢Matt说,我将子查询移到了FROM部分,我在派生表的select查询中包含了ProductSize和Location,然后将结果与“Size”表相关联,最后我添加了一个条件where部分,以消除每个位置重复相同记录。

1 个答案:

答案 0 :(得分:1)

我设法构建了查询,它看起来像这样:

SELECT p.ArtNbr                    AS [Number], 
   p.Name                          AS Name, 
   s.SizeName                      AS Sizes, 
   s.SizeIndex                     AS SizeIndex, 
   s.Oid                           AS SizeId, 
   l.Name                          AS LocationName, 
   so.qty                          AS Quantity
FROM ([Size] AS s 
         INNER JOIN (Product AS p 
               INNER JOIN (Favorite AS f 
                           INNER JOIN Location AS l 
                                   ON f.Customer = l.Customer) ON p.OID = f.Product) 
                       ON p.OID = s.Product) 
      LEFT JOIN 
          (SELECT od.ProductSize       AS PS, 
                  od.Quantity          AS qty, 
                  o.Location as Location 
           FROM OrderDetails AS od INNER JOIN [Order] AS o ON od.Order = o.OID 
           WHERE o.Active = -1)  AS so ON so.PS = s.OID
WHERE f.Customer = @customer AND (l.OID = so.Location OR so.Location is null)

我确实喜欢Matt说,我将子查询移到了FROM部分,我在派生表的select查询中包含了ProductSize和Location,然后将结果与“Size”表相关联,最后我添加了一个条件where部分,以消除每个位置重复相同记录。