简体   繁体   English

每组最多N个加入:错误的最大/最小值

[英]Greatest N Per Group with Joins: Getting Wrong Max/Min Values

Sadly, another variation on the popular "Greatest-N-Per-Group" query, but it's been kicking my butt and I could really use some clarity (problem simplified as much as possible): 令人遗憾的是,流行的“每组最多N个”查询的另一种形式,但它使我不寒而栗,我确实可以使用一些清晰度(问题尽可能地简化):

I've got two tables: 我有两个桌子:

Items
-----
ItemID 
ProductName


Inventory
---------
InventoryID
ItemID
Condition
Price

And I'm trying to formulate a query which shows the InventoryID, ItemID, ItemName, Condition, and Max prices for each item In a Given Condition 我正在尝试制定一个查询,以显示给定条件下每个商品的InventoryID,ItemID,ItemName,Condition和Max price

ie given 即给

Inventory
---------
InventoryID  ItemID  Condition   Price
INV123       ITEM001 NEW         $3.99
INV001       ITEM001 NEW         $3.79
INV031       ITEM001 USED        $1.23
INV234       ITEM001 USED        $1.99

And Items: 和项目:

ItemID    ItemName
ITEM001   Lg Widget
ITEM002   Sm Widget

I'd Expect: 我期望:

ItemID  ItemName  Condition  MaxPrice MaxPriceInventoryID  
ITEM001 Lg Widget NEW        $3.99    INV123
ITEM001 Lg Widget USED       $1.99    INV234  

I'm trying this: 我正在尝试:

SELECT 
  ItemID, ItemName, b.condition, b.maxprice, 
    InventoryID as MaxPriceInventoryID
FROM
  Items I join inventory v On i.ItemID= v.ItemID
    join (
        select inventory.ItemID, max(Price) as MaxPrice, condition
            from inventory join Items on inventory.ItemID = Items.ItemID
            group by inventory.ItemID, condition) as b 
    on b.ItemID = v.ItemID and b.MaxPrice = v.Price
 ORDER BY
    ItemName, Condition

Unfortunately, this doesn't give the desired result: it seems to sporadically return max price for the item across all conditions, not max price for the item in a given condition 不幸的是,这并没有得到预期的结果:它似乎偶尔会返回所有条件下该商品的最高价格,而不是给定条件下该商品的最高价格

Ideas? 有想法吗?

It's not entirely clear to me precisely how your data are related or how you need to filter. 对于我来说,还不是很完全清楚,您的数据如何关联或需要如何过滤。

I'd either do it this way: 我要么这样做:

SELECT ii.ItemID
    ,ii.ItemName
    ,ii.Condition
    ,ii.Price AS MaxPrice
    ,ii.MaxPriceInventoryID
FROM (
    SELECT i.ItemID
        ,i.ItemName
        ,i.ProductName
        ,v.InventoryID
        ,v.Condition
        ,v.Price
        ,DENSE_RANK() OVER (PARTITION BY v.ItemID, v.Condition ORDER BY v.Price DESC) AS R
    FROM Items i
    INNER JOIN Inventory v
        ON  i.ItemID = v.ItemID
    WHERE 1 = 1 /* Place your filtering conditionals here */
    ) AS ii
WHERE ii.R = 1
ORDER BY ii.ItemName
    ,ii.Condition

Or this way: 或者这样:

SELECT i.ItemID
    ,i.ItemName
    ,vv.Condition
    ,vv.Price AS MaxPrice
    ,vv.MaxPriceInventoryID
FROM Items i
INNER JOIN (
    SELECT v.ItemID
        ,v.InventoryID
        ,v.Condition
        ,v.Price
        ,DENSE_RANK() OVER (PARTITION BY v.ItemID, v.Condition ORDER BY v.Price DESC) AS R
    FROM  Inventory v
    WHERE 1 = 1 /* Place your filtering conditionals here */
    ) AS vv
    ON  i.ItemID = vv.ItemID
WHERE vv.R = 1
ORDER BY i.ItemName
    ,vv.Condition

If you only want one ItemID even when you have ties on price, then you can replace DENSE_RANK() with ROW_NUMBER() . 如果即使在价格上有约束,即使您只想要一个ItemID,也可以将DENSE_RANK()替换为ROW_NUMBER() However, unless you add a key field to your ORDER BY in the OVER() clause, the result will not be deterministic. 但是,除非您在OVER()子句中向ORDER BY添加键字段,否则结果将不确定。

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

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