4

我有一组表,其中包含周、产品、库存和每周预测,我想从中选择 X 周产品库存和最新预测。但我就是无法掌握 SQL:

create table products (
    product_id integer
);
create table inventory (
    product_id integer,
    asof_week integer,
    qoh float8
);
create table forecast (
    product_id integer,
    for_week integer,
    asof_week integer,
    projection float8
);
create table weeks (
    wkno integer
);
insert into weeks values (4),(5),(6),(7);
insert into products values(1),(2);
insert into inventory values(1,5,10),(1,6,20),(2,6,200);
insert into forecast values(1,4,1,10),(1,4,2,11),(1,4,3,12),(1,4,4,13),
                           (1,5,1,11),(1,5,2,11),(1,5,3,21),(1,5,4,31),
--corr:one too many        (1,6,1,10),(1,6,2,11),(1,6,3,12),(1,6,4,22),(1,6,5,32),(1,6,5,42),(1,6,6,42),
                           (1,6,1,10),(1,6,2,11),(1,6,3,12),(1,6,4,22),(1,6,5,42),(1,6,6,42),
                           (1,7,1,10),(1,7,6,16),
                           (2,6,5,2000),(2,7,5,2100),(2,8,5,30);

和一个查询:

select p.product_id "product",
        i.asof_week "inven asof",
        i.qoh "qoh",
        f.for_week "fcast for",
        f.projection "fcast qty",
        f.asof_week "fcast asof"
from weeks w, products p
    left join inventory i on(p.product_id = i.product_id)
    left join forecast f on(p.product_id = f.product_id)
where
    (i.asof_week is null or i.asof_week = w.wkno)
    and (f.for_week is null or f.for_week = w.wkno)
    and (f.asof_week is null
        or f.asof_week = (select max(f2.asof_week)
                          from forecast f2
                          where f2.product_id = f.product_id
                             and f2.for_week = f.for_week))
order by p.product_id, i.asof_week, f.for_week, f.asof_week

例如,对于第 4-7 周,我正在寻找一个结果集:

product week    qoh     projection
1       4       -       13
1       5       10      31
1       6       20      42
1       7       -       16
2       6       200     2000
2       7       -       2100

但实际上我只得到 3 行:

 product | inven asof | qoh | fcast for | fcast qty | fcast asof 
---------+------------+-----+-----------+-----------+------------
       1 |          5 |  10 |         5 |        31 |          4
       1 |          6 |  20 |         6 |        42 |          6
       2 |          6 | 200 |         6 |      2000 |          5
(3 rows)
Time: 2.531 ms

我对 SQL 比较陌生,可以使用一些有用的指针。

关于数据的一些注释:我有几个其他数据表要加入,我在示例中省略了这些数据表,以专注于这个问题,其中至少一个在性质上与预测数量表相似(即,每个产品都有多个版本行x 周)。每个产品 X 周大约有 100 个预测行,所以在某个地方我还必须担心效率……但首先要正确结果。

我在 postgresql 9.2 上。

谢谢。

4

3 回答 3

2

在不了解数据模型的其余部分的情况下很难给出一般性的指示,但我应该这样说:我通常发现当我将查询保持为“平坦”时更容易推理。此外,一旦我有一堆空检查,我要么尝试向我的数据添加保证,要么围绕不同的“根”表重新旋转我的查询。

无论如何,以下内容应该适合您(尽管我不能保证它适用于任何数据,尤其是在存在重复的情况下):

select
  products.product_id,
  weeks.wkno,
  inventory.qoh,
  max(projection)
from forecast
join products on products.product_id = forecast.product_id
join weeks on weeks.wkno = forecast.for_week
left join inventory on
  inventory.product_id = products.product_id
  and inventory.asof_week = weeks.wkno
group by
  products.product_id,
  weeks.wkno,
  inventory.qoh

对不起,我不能给你那么多建议。我希望这有帮助。

编辑:调整查询以删除交叉联接。原版在这里。如果您想在缺少某些预测的情况下离开连接预测,您可能需要交叉连接。对于您的具体示例,它是不需要的。

编辑 2:上述查询在语义上不正确。以下是正确的,但不能说明我的观点

select
  p.product_id,
  p.wkno,
  p.qoh,
  f.projection
from
  (select
      products.product_id,
      weeks.wkno,
      inventory.qoh,
      max(forecast.asof_week) max_p
    from forecast
    join products on products.product_id = forecast.product_id
    join weeks on weeks.wkno = forecast.for_week
    left join inventory on
      inventory.product_id = products.product_id
      and inventory.asof_week = weeks.wkno
    group by
      products.product_id,
          weeks.wkno,
      inventory.qoh) as p
  join forecast f on
    f.product_id = p.product_id
    and  f.for_week = p.wkno
    and f.asof_week = p.max_p
于 2013-04-29T05:01:05.420 回答
1

数据中似乎缺少一些 PK/FK 约束:

CREATE TABLE products (
    product_id INTEGER PRIMARY KEY
    );
CREATE TABLE weeks (
    wkno INTEGER PRIMARY KEY
    );
CREATE TABLE inventory (
    product_id INTEGER REFERENCES products(product_id)
    , asof_week INTEGER REFERENCES weeks(wkno)
    , qoh float8
    , PRIMARY KEY (product_id,asof_week)
    );
CREATE TABLE forecast (
    product_id INTEGER REFERENCES products(product_id)
    , for_week INTEGER REFERENCES weeks(wkno)
    , asof_week INTEGER REFERENCES weeks(wkno)
    , projection FLOAT8
    , PRIMARY KEY (product_id,for_week,asof_week)
    );
INSERT INTO weeks VALUES (4),(5),(6),(7)
    , (1),(2),(3), (8) -- need these, too
    ;
-- et cetera.

如果该weeks表旨在用作“日历”表,则可以(并且应该)将其替换为generate_series(4,7)伪表。(并且 FK 约束下降)

该查询受到 LEFT JOIN + MAX(aggregate) 构造的影响。以下应该做同样的事情并且看起来更简单(NOT EXISTS救援......):

SELECT p.product_id "product"
        , i.asof_week "inven asof"
        , i.qoh "qoh"
        , f.for_week "fcast for"
        , f.projection "fcast qty"
        , f.asof_week "fcast asof"
FROM products p
CROSS JOIN weeks w
LEFT JOIN inventory i ON i.product_id = p.product_id AND i.asof_week = w.wkno
LEFT JOIN forecast f ON f.product_id = p.product_id AND f.for_week = w.wkno
WHERE NOT EXISTS (
    SELECT * FROM forecast f2
    WHERE f2.product_id = f.product_id
      AND f2.for_week = f.for_week
    AND  f2.asof_week < f.asof_week
    )
AND COALESCE(i.asof_week,f.for_week) IS NOT NULL
ORDER BY p.product_id, i.asof_week, f.for_week, f.asof_week
    ;
于 2013-04-29T08:14:46.853 回答
0

感谢朱利安的提示。这得到了结果,尽管我不确定这是否是最好的方法,或者一旦我有 100+ 百万行,它会如何运行,因为我仍在使用玩具数据集。可能下面的第一个坏事pw是未索引的。

with pw as ( select * from products, weeks )         
    select pw.product_id "product",
            pw.wkno,         
            i.asof_week "inven asof",    
            coalesce(i.qoh::text,'missing') "qoh",   
            f.for_week "fcast for",       
            coalesce(f.projection::text,'no fcast') "fcast qty",      
            f.asof_week "fcast asof"     
    from pw      
        left join inventory i on(pw.product_id = i.product_id and pw.wkno = i.asof_week )
        left join forecast f on(pw.product_id = f.product_id  
                                and f.for_week = pw.wkno          
                                and f.asof_week = (select max(f2.asof_week)               
                                                from forecast f2                          
                                                where f2.product_id = pw.product_id                       
                                                    and f2.asof_week < pw.wkno                            
                                                    and f2.for_week = pw.wkno))                               
    where        
        not (i.asof_week is null and f.asof_week is null)     
    order by pw.product_id,  
                pw.wkno,     
                f.for_week,                          
                f.asof_week           

产生

 product | wkno | inven asof |   qoh   | fcast for | fcast qty | fcast asof
---------+------+------------+---------+-----------+-----------+------------
       1 |    4 |            | missing |         4 | 12        |          3
       1 |    5 |          5 | 10      |         5 | 31        |          4
       1 |    6 |          6 | 20      |         6 | 42        |          5
       1 |    7 |            | missing |         7 | 16        |          6
       2 |    6 |          6 | 200     |         6 | 2000      |          5
       2 |    7 |            | missing |         7 | 2100      |          5
(6 rows)
Time: 2.999 ms
于 2013-04-29T07:22:08.873 回答