为库存项目选择多个供应商的最新,最低价格

时间:2013-08-23 19:37:22

标签: mysql sql postgresql greatest-n-per-group distinct-on

我对SQL非常熟练,但是这个问题已经让我自己难以忍受了一段时间。从最基本的意义上讲,只有两个表:

Items
+----+--------+
| id | title  |
+----+--------+
|  1 | socks  |
|  2 | banana |
|  3 | watch  |
|  4 | box    |
|  5 | shoe   |
+----+--------+

...和价格表:

Prices
+---------+-----------+-------+------------+
| item_id | vendor_id | price | created_at |
+---------+-----------+-------+------------+
|       1 |         1 | 5.99  | Today      |
|       1 |         2 | 4.99  | Today      |
|       2 |         1 | 6.99  | Today      |
|       2 |         2 | 6.99  | Today      |
|       1 |         1 | 3.99  | Yesterday  |
|       1 |         1 | 4.99  | Yesterday  |
|       2 |         1 | 6.99  | Yesterday  |
|       2 |         2 | 6.99  | Yesterday  |
+---------+-----------+-------+------------+

(请注意:created_at实际上是一个时间戳,单词“Today”和“Yesterday”仅用于快速传达概念)。

我的目标是获得一个简单的结果,其中包含与最新的最低价格相关联的库存项目,包括对提供所述价格的vendor_id的引用。

但是,我发现绊脚石似乎是要处理的声明(或声明)的绝对数量:

  • 每个项目都有多个供应商,因此我们需要确定每个项目的所有供应商之间的价格是最低的
  • 物品的新价格会定期附加,因此我们只想考虑每个供应商的每件商品的最新价格
  • 我们希望将所有内容整合到一个结果中,每行一项,包括商品,价格和供应商

看起来很简单,但我发现这个问题难以解决。

作为一个注释,我正在使用Postgres,因此它提供的所有功能都可以使用(即:窗口功能)。

3 个答案:

答案 0 :(得分:4)

在Postgres中使用DISTINCT ON更简单:

每个供应商的每件商品的当前价格

SELECT DISTINCT ON (p.item_id, p.vendor_id)
       i.title, p.price, p.vendor_id
FROM   prices p
JOIN   items  i ON i.id = p.item_id
ORDER  BY p.item_id, p.vendor_id, p.created_at DESC;

每个项目的最佳供应商

SELECT DISTINCT ON (item_id) 
       i.title, p.price, p.vendor_id -- add more columns as you need
FROM (
   SELECT DISTINCT ON (item_id, vendor_id)
          item_id, price, vendor_id -- add more columns as you need
   FROM   prices p
   ORDER  BY item_id, vendor_id, created_at DESC
   ) p
JOIN   items i ON i.id = p.item_id
ORDER  BY item_id, price;

->SQLfiddle demo

详细说明:
Select first row in each GROUP BY group?

答案 1 :(得分:0)

试试这个

CREATE TABLE #Prices ( Iid INT, Vid INT, Price Money, Created DateTime)
INSERT INTO #Prices 
SELECT 1, 1, 5.99 ,GETDATE()    UNION
SELECT 1, 2, 4.99 ,GETDATE()    UNION
SELECT 2, 1, 6.99 ,GETDATE()    UNION
SELECT 2, 2, 6.99 ,GETDATE()    UNION
SELECT 1, 1, 3.99 ,GETDATE()-1  UNION
SELECT 1, 2, 4.99 ,GETDATE()-1  UNION
SELECT 2, 1, 6.99 ,GETDATE()-1  UNION
SELECT 2, 2, 6.99 ,GETDATE()-1 

WITH CTE AS
(
    SELECT 
        MyPriority = ROW_NUMBER() OVER ( partition by Iid, Vid ORDER BY Created DESC, Price ASC) 
    ,   Iid
    ,   Vid
    ,   price
    ,   Created
    FROM #Prices 
)

SELECT * FROM CTE WHERE MyPriority = 1

答案 2 :(得分:0)

也可以使用窗口函数执行此操作,它将适用于SQL Server版本> 2005:

with cte1 as (
    select
        *,
        row_number() over(partition by vendor_id, item_id order by created_at desc) as row_num
    from prices
), cte2 as (
    select
        *,
        row_number() over(partition by item_id order by price asc) as row_num2
    from cte1
    where row_num = 1
)
select i.title, c.price, c.vendor_id
from cte2 as c
    inner join items as i on i.id = c.item_id
where c.row_num2 = 1;

sql fiddle demo(感谢Erwin)