灵活的搜索在数据库中查找重复项

Flexible search find duplicates in database

我有两个表 - Warehouse(id) 和 StockLevel (id, productCode)。
商店有很多 StockLevels。​​

我想为同一商店中的同一产品查找重复的 StockLevel。
例如:Warehouse"Minimarket" 有两个产品 "Apple".

的 StockLevel 条目

目前,我有这个,但我不确定它的工作方式是否正确:

select {wh.code}, {sl.productCode}, count({sl.pk}) as cnt 
from {StockLevel as sl  
left join Warehouse as wh on {sl.warehouse}={wh.pk}}  
group by ({wh.code}, {sl.productCode})
having count({sl.pk}) > 1

| id | productCode | warehouse |
--------------------------
| 1 | "apple"     | "mini" |
--------------------------
| 2 | "apple"     | "mini" |
--------------------------
| 3 | "apple"     | "maxi" |
--------------------------
| 4 | "apple"     | "macro" |
--------------------------
| 5 | "orange"    | "mini" |
--------------------------

我想要选择前两个条目(Id 1 和 2)中的一个(我真的不在乎哪个条目)。
我也有问题 - 如果我在 flexibleSearch 中使用此查询,我无法将其转换为实体 (StockLevel) 并添加 {sl.pk} 来查询损坏它。

如果您只想要 "first" 行,您可以这样做:

select w.*
from warehouse w
where exists (select 1
              from warehouse w2
              where w2.warehouse = w.warehouse and w2.code = w.code and w2.id > w.id
             ) and
      not exists (select 1
                  from warehouse w2
                  where w2.warehouse = w.warehouse and w2.code = w.code and w2.id < w.id
                ) ;