简体   繁体   English

SQL / Microsoft访问

[英]SQL/Microsoft Access

I am trying to figure out this problem but, it's already taking me a few days and I cant seem to solve it. 我正在尝试找出这个问题,但是,这已经花了我几天时间,而且我似乎无法解决。

The questions demands: 问题要求:

Display a list of products (show ProductID and ProductDescription as the first two columns), the number of times each product has been ordered (the third column), and the total quantity each product has been ordered over all orders (the forth column). 显示产品列表(在前两列中显示ProductID和ProductDescription),订购每种产品的次数(第三列)以及在所有订单中订购每种产品的总数量(第四列)。

The Database: 数据库:

**Product_T**                                      **OrderLine_T**
ProductID  ProductDescription                 ProductID    OrderedQuantity
1          End Table                             1          2
2          Coffe Table                           2          2
3          Computer Desk                         4          1
4          Entertainment Center                  3          5
5          Writers Desk                          3          3
6          8-Drawer Desk                         6          2
7          Dining Table                          8          2
8          Computer Desk                         4          4
                                                 4          1
                                                 5          2
                                                 7          2
                                                 1          3
                                                 2          2
                                                 3          3
                                                 8          3
                                                 4          2
                                                 7          3
                                                 8          10

Just a JOIN and GROUP BY 只是加入和分组

SELECT p.ProductID,
       p.ProductDescription,
       COUNT(*) AS time_ordered,
       SUM(o.OrderedQuantity) AS qty_ordered
FROM Product_T as p
LEFT JOIN OrderLine_T AS o
    ON p.ProductID = o.ProductID
GROUP BY p.ProductID,
         p.ProductDescription;

Try this: 尝试这个:

SELECT t1.ProductID,
       t1.ProductDescription,
       COALESCE(t2.num_times_ordered, 0) AS num_times_ordered,
       COALESCE(t2.total_quantity, 0)    AS total_quantity
FROM Product_T t1
LEFT JOIN
(
    SELECT ProductID,
           COUNT(*) AS num_times_ordered,
           SUM(OrderedQuantity) AS total_quantity
    FROM OrderLine-T
    GROUP BY ProductID
) t2
    ON t1.ProductID = t2.ProductID

The answer given by @GurV is more concise than this and works for this particular problem, but in general you would need to use a subquery to obtain stats from the OrderLine-T table, assuming that you wanted to include non-aggregate columns from Product_T in your report. @GurV给出的答案比这更简洁,并且可以解决此特定问题,但是通常,您需要使用子查询从OrderLine-T表中获取统计信息, OrderLine-T是您想要包括Product_T非聚合列在您的报告中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM