简体   繁体   English

如何按聚合列过滤非聚合查询结果?

[英]How to filter nonaggregated query results by an aggregated column?

I'm creating a price comparison service.我正在创建一个价格比较服务。 Products from one Site are compared to products from one or more Sites .来自一个Site的产品与来自一个或多个Sites产品进行比较。 Products are matched from one Site to another using a ProductMatch table:使用ProductMatch表将产品从一个Site匹配到另一个Site

UML图

Given the following query to extract products, together with their matches:给定以下查询以提取产品及其匹配项:

SELECT
    p1.id AS p1_id, p1.name AS p1_name, p1.price AS p1_price,
    p2.id AS p2_id, p2.name AS p2_name, p2.price AS p2_price,
    m.time
FROM Product p1
LEFT JOIN ProductMatch m ON m.fromProduct_id = p1.id
LEFT JOIN Product p2 ON m.toProduct_id = p2.id

WHERE p1.site_id = 1;

How can I filter products whose price ( p1.price ) is lower than the minimum competitor price ( MIN(p2.price) )?如何过滤价格 ( p1.price ) 低于最低竞争对手价格 ( MIN(p2.price) ) 的产品?

Using subqueries, here's how I would do it:使用子查询,这是我的做法:

SELECT
    p1.id AS p1_id, p1.name AS p1_name, p1.price AS p1_price,
    p2.id AS p2_id, p2.name AS p2_name, p2.price AS p2_price,
    m.time
FROM Product p1
LEFT JOIN ProductMatch m ON m.fromProduct_id = p1.id
LEFT JOIN Product p2 ON m.toProduct_id = p2.id

WHERE p1.id IN (
    SELECT x.id FROM (
        SELECT _p1.id, _p1.price
        FROM Product _p1
        JOIN ProductMatch _m ON _m.fromProduct_id = _p1.id
        JOIN Product _p2 ON _m.toProduct_id = _p2.id
        WHERE _p1.site_id = 1
        GROUP BY _p1.id
        HAVING _p1.price < MIN(_p2.price)
    ) x
);

Is it possible to simplify this query to not use subqueries?是否可以简化此查询以不使用子查询?

My concerns:我的顾虑:

  • it feels weird to repeat the exact same joins in the subquery在子查询中重复完全相同的连接感觉很奇怪
  • I have concerns about the performance of subqueries on larger data sets我担心子查询在较大数据集上的性能
  • subqueries don't play very well with my ORM子查询不能很好地与我的 ORM 配合使用

With MIN() window function inside a CTE which will be filtered:在将被过滤的CTE内使用MIN()窗口函数:

WITH cte AS (
  SELECT
    p1.id AS p1_id, p1.name AS p1_name, p1.price AS p1_price,
    p2.id AS p2_id, p2.name AS p2_name, p2.price AS p2_price,
    m.time,
    MIN(p2.price) OVER (PARTITION BY p1.id) AS min_price
  FROM Product p1
  LEFT JOIN ProductMatch m ON m.fromProduct_id = p1.id
  LEFT JOIN Product p2 ON m.toProduct_id = p2.id
  WHERE p1.site_id = 1
)
SELECT 
  p1_id, p1_name, p1_price,
  p2_id, p2_name, p2_price,
  time
FROM cte
WHERE p1_price < min_price

暂无
暂无

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

相关问题 在没有GROUP BY的聚合查询中,SELECT列表的表达式#2包含非聚合列 - In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 如何在 MySql 中使用 group by 查询非聚合列? - How to query a nonaggregated column using group by in MySql? 在没有GROUP BY的聚合查询中,SELECT列表的表达式#2包含非聚合列&#39;abid&#39;; - In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'a.b.id'; 在没有 GROUP BY 的聚合查询中,SELECT 列表的表达式 #1 包含非聚合列“jquzntys.posts.id” - In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'jquzntys.posts.id' GROUP BY子句,并且在查询中包含未聚合的列 - GROUP BY clause and contains nonaggregated column in Query SQL从一个表中返回聚合结果作为查询中的列 - SQL returning aggregated results from one table as a column in a query 如何在mysql查询中将聚合与非聚合相结合 - How to combine aggregate with nonaggregated in mysql query 具有复杂聚合结果的MYSQL查询 - MYSQL query with complex aggregated results MySQL 5.7.25通过子查询分组并按“非聚合列”错误排序 - MySQL 5.7.25 group by sub-query and order by “nonaggregated column” error mysql查询错误SELECT列表不在GROUP BY子句中并且包含未聚合的列 - mysql query error SELECT list is not in GROUP BY clause and contains nonaggregated column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM