复合Mysql连接sql中每个组所需的最大n
我正在使用此查询连接3个表复合Mysql连接sql中每个组所需的最大n,mysql,sql,join,greatest-n-per-group,Mysql,Sql,Join,Greatest N Per Group,我正在使用此查询连接3个表 SELECT DISTINCT a.number, c.quantity, c.retail_price FROM catalog.product `a` JOIN catalog.product_variation `b` ON a.id = b.product_id JOIN catalog.price_regular `c` ON b.id = c.product_variation_id WHERE c.retail_price BETWEEN 5 AND
SELECT DISTINCT a.number, c.quantity, c.retail_price
FROM catalog.product `a`
JOIN catalog.product_variation `b` ON a.id = b.product_id
JOIN catalog.price_regular `c` ON b.id = c.product_variation_id
WHERE c.retail_price BETWEEN 5 AND 6 AND a.status_id = 1
ORDER BY a.number, c.retail_price DESC
我得到了这个结果集
number|quantity|retail_price
---------------------
1007 | 288 | 5.750
1007 | 48 | 5.510
1007 | 576 | 5.460
1007 | 96 | 5.240
1007 | 576 | 5.230
1007 | 144 | 5.120
1006 | 200 | 5.760
1006 | 100 | 5.550
1006 | 200 | 5.040
1006 | 500 | 5.010
我需要的是结果只包含数量
列中值最大的行,以及零售价格
最大的行。所以我需要的结果集是这样的
number|quantity|retail_price
---------------------
1006 | 500 | 5.010
1007 | 576 | 5.460
我在上找到了一些帖子,但在连接多个表时,没有一个帖子是有用的。我需要一条sql语句来获取上面指定的结果集这是一个简单的GROUPBY查询
SELECT a.number, max(c.quantity) as qty, max(c.retail_price) as price
FROM catalog.product `a`
JOIN catalog.product_variation `b` ON a.id = b.product_id
JOIN catalog.price_regular `c` ON b.id = c.product_variation_id
WHERE c.retail_price BETWEEN 5 AND 6
AND a.status_id = 1
GROUP BY a.number;
实际上有两行,编号为1007,数量为576。你怎么知道该选哪一个?没错。我还需要选择零售价格最高的那一家。我已经修改了问题,如果你喜欢,考虑下面这个简单的两步行动:1。如果您还没有这样做,请提供适当的DDL(和/或SQLFIDLE),以便我们可以更轻松地复制问题。2.如果您还没有这样做,请提供与步骤1中提供的信息相对应的所需结果集。这是一个简单的查询,但不是这个查询。至少,如果OP所需的结果集是可供参考的,则不是这样。