【问题标题】:Flexible search find duplicates in database灵活搜索在数据库中查找重复项
【发布时间】:2018-06-05 19:22:55
【问题描述】:

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

我想在同一家商店中查找同一产品的 StockLevel 重复项。
例如:Warehouse"Minimarket" 有两个 StockLevel 条目用于产品 "Apple"。

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

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)中的一个(我真的不在乎其中一个)。
我也遇到了问题 - 如果我在灵活搜索中使用此查询,我无法将其转换为实体 (StockLevel) 并添加 {sl.pk} 以查询损坏它。

【问题讨论】:

  • 您的查询似乎正确,因为您需要编辑问题添加一些示例数据和所需结果。
  • 用表格更新了问题。无法创建属性表,这里似乎不可能。

标签: sql hybris


【解决方案1】:

如果你只想要“第一”行,你可以这样做:

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
                ) ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-21
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    相关资源
    最近更新 更多