不确定如何连接表3次以获得所需的结果

时间:2010-10-27 10:17:40

标签: mysql

我正在尝试整理一个查询,该报告向我提供了3个单独位置(商店分支机构)的产品销售数量及其库存水平的报告

我们的想法是将结果分组如下:

    Prod.  | Loc1 Sales | Loc1 Stk | Loc2 Sales | Loc2 Stk | Loc3 Sales | Loc3 Stk 
    Item 1 | 323        | 34       | 23         | 7        |        119 | 54 
    Item 2 | 653        | 566      | 84         | 45       |        476 | 3434
    Item 3 | 121        | 23       | 300        | 5643     |        12  | 3434

我可以从一个地方抓住产品和数量开始:

SELECT    col.productname, sum(col.quantity) as qty
FROM      customerorderlines col
JOIN      customerorders co
ON        co.id = col.customerorder_id
WHERE     co.orderdate BETWEEN 'start date here' AND 'end date here'
AND       co.location_id = 1
GROUP BY  product_id
ORDER BY  qty DESC

结果:

productname | qty
item 1      | 146
item 2      | 74
item 3      | 63
item 4      | 49

现在我尝试加入那里的股票表:

SELECT     col.productname, sum(col.quantity) AS qty, sum(s.stocklevel) AS stocklevel
FROM       customerorderlines col
JOIN       customerorders co
ON         co.id = col.customerorder_id
JOIN       stock s 
ON         s.product_id = col.product_id
WHERE      co.orderdate BETWEEN 'start date here' AND 'end date here'
AND        co.location_id = 1
AND        s.location_id = 1
GROUP BY   col.product_id
ORDER BY   qty DESC

当然,这不起作用:

productname | qty | stocklevel
item 1      | 246 | 89123
item 2      | 98  | 18454
item 3      | 78  | 22565

不仅库​​存水平错了,它也会增加数量。 尽管如此,我知道这不是写它的方式,所以我试着加入:

SELECT     col.productname, sum(col.quantity) AS qty, sl.stocklevel
FROM       customerorderlines col
JOIN       ( SELECT
             product_id
             ,location_id
             ,SUM(stocklevel) AS stocklevel
             FROM stock 
             GROUP BY product_id
           ) sl
ON         sl.product_id = col.product_id
JOIN       customerorders co
ON         co.id = col.customerorder_id
JOIN       stock s 
ON         s.product_id = col.product_id
WHERE      co.orderdate BETWEEN 'start date here' AND 'end date here'
AND        co.location_id = 1
AND        s.location_id = 1
GROUP BY   col.product_id
ORDER BY   qty DESC

通过这个连接,它可以作为一个查询,我等待大约8分钟,然后我就杀了这个过程,因为它就在那里。

有什么建议吗?

提前致谢, 罗布

2 个答案:

答案 0 :(得分:1)

SELECT  p.name,
        (
        SELECT  SUM(col.quantity)
        FROM    customerorder co
        JOIN    customerorderlines col
        ON      col.customerorder_id = co.id
        WHERE   co.localtion_id = 1
                AND co.orderdate BETWEEN @start AND @end
                AND col.product_id = p.id
        ),
        (
        SELECT  SUM(stocklevel)
        FROM    stock s
        WHERE   s.location_id = 1
                AND s.product_id = p.id
        ),
        (
        SELECT  SUM(col.quantity)
        FROM    customerorder co
        JOIN    customerorderlines col
        ON      col.customerorder_id = co.id
        WHERE   co.localtion_id = 2
                AND co.orderdate BETWEEN @start AND @end
                AND col.product_id = p.id
        ),
        (
        SELECT  SUM(stocklevel)
        FROM    stock s
        WHERE   s.location_id = 2
                AND s.product_id = p.id
        ),
        (
        SELECT  SUM(col.quantity)
        FROM    customerorder co
        JOIN    customerorderlines col
        ON      col.customerorder_id = co.id
        WHERE   co.localtion_id = 3
                AND co.orderdate BETWEEN @start AND @end
                AND col.product_id = p.id
        ),
        (
        SELECT  SUM(stocklevel)
        FROM    stock s
        WHERE   s.location_id = 3
                AND s.product_id = p.id
        )
FROM    product p

答案 1 :(得分:0)

您是否考虑过为中间结果创建临时表。看看这里:http://www.tutorialspoint.com/mysql/mysql-temporary-tables.htm