【问题标题】:sql - select all rows that have all multiple same colssql - 选择所有具有多个相同列的所有行
【发布时间】:2020-02-09 08:24:22
【问题描述】:

我有一个有 4 列的表格。

  • 日期
  • store_id
  • product_id
  • label_id

我需要在一天内找到所有 products_id 和 label_id 相同(例如 4)的 store_id。

例如:

store_id | label_id | product_id | data|
   4          4           5         9/2
   5          4           7         9/2
   4          3           12        9/2
   4          4           7         9/2   

所以它应该返回 4,因为它是唯一一家在一天内包含所有可能带有标签 4 的产品的商店。

我尝试过这样的事情:

(select store_id, date
from table
where label_id = 4
group by store_id, date
order by date)

外部查询不知道怎么写,我试过了:

select * from table
where product_id = all(Inner query)

但它没有工作。

谢谢

【问题讨论】:

  • 你试过where label_id = 4 and date = ... 吗?
  • 商店 4 也将显示标签 3,因为标签 3 在 9 月 2 日仅售出一种产品,而商店 4 售出了该产品。您的查询应该查看一个特定标签还是所有标签?您的查询应该查找某一天还是所有天?你能展示一下预期的结果吗?您只想显示商店编号吗?还是存储和约会?或者还有什么?
  • @ThorstenKettner 我的查询应该返回所有 store_id,其中包含当天 label_id =4 的所有可能产品,想要的输出应该是 4,因为它是唯一包含当天所有产品的商店。在商店 4 我有产品 ID - 5,7 而在 store_id = 5 我只有 product_id = 7..
  • 好的,您想显示所有符合此要求的商店,无论是在哪一天。结果可能是 4,您会知道商店 4 在某天销售了所有标签 4 的产品,但您不知道是哪一天。你的 DBMS 是什么?
  • 对.. postgres 11

标签: sql postgresql relational-division postgresql-11


【解决方案1】:

您的问题不清楚标签是特定于特定日期还是整个时期。但是蒂姆的答案的变体似乎是合适的。对于任何标签:

SELECT t.date, t.label, t.store_id
FROM t
GROUP BY t.date, t.label, t.store_id
HAVING COUNT(DISTINCT t.product_id) = (SELECT COUNT(DISTINCT t2product_id)
                                       FROM t t2
                                       WHERE t2.label = t.label
                                      );

对于特定标签:

SELECT t.date, t.store_id
FROM t
WHERE t.label = 4
GROUP BY t.date,t.store_id
HAVING COUNT(DISTINCT t.product_id) = (SELECT COUNT(DISTINCT t2product_id)
                                       FROM t t2
                                       WHERE t2.label = t.label
                                      );

如果标签特定于日期,那么您也需要在外部查询中进行比较。

【讨论】:

    【解决方案2】:

    这是一种方法:

    SELECT date, store_id
    FROM yourTable
    GROUP BY date, store_id
    HAVING COUNT(DISTINCT product_id) = (SELECT COUNT(DISTINCT product_id)
                                         FROM yourTable t2
                                         WHERE t2.date = t1.date)
    ORDER BY date, product_id;
    

    此查询以一种非常简单的方式读取,它表示要在某个日期查找所有商店的不同产品计数与同一天的不同产品计数相同的所有产品。

    【讨论】:

      【解决方案3】:

      我可能会聚合到字符串或数组中的产品列表:

      with products_per_day_and_store as
      (
        select
          store_id,
          date,
          string_agg(distinct product_id order by product_id) as products
        from mytable
        where label_id = 4
        group by store_id, date
      )
      , products_per_day
      (
        select
          date,
          string_agg(distinct product_id order by product_id) as products
        from mytable
        where label_id = 4
        group by date
      )
      select distinct ppdas.store_id
      from products_per_day_and_store ppdas
      join products_per_day ppd using (date, products);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-16
        • 2020-10-05
        • 1970-01-01
        相关资源
        最近更新 更多