使用MAX()函数获得最大的价格

时间:2015-05-11 08:26:16

标签: sql oracle

我正在使用SQL Oracle,我想使用MAX函数而不是ROWNUM更改我的查询。

SELECT * 
FROM (SELECT a.name, price.price
FROM price
LEFT JOIN a 
ON a.id = price.tk_a
ORDER BY price.price DESC)
WHERE ROWNUM <=1;

请提供任何帮助或建议?

4 个答案:

答案 0 :(得分:2)

您可以将MAX聚合函数与KEEP ( DENSE_RANK FIRST ORDER BY ... )一起使用,以获得另一列的最大值:

SELECT MAX( a.name ) KEEP ( DENSE_RANK FIRST ORDER BY p.price DESC ) AS name,
       MAX( p.price ) AS price
FROM   a
       LEFT OUTER JOIN price p
       ON ( a.id = p.tk_a );

答案 1 :(得分:1)

MAX和分组

SELECT a.name, MAX(p.price)
FROM price
LEFT JOIN a ON a.id = p.tk_a
GROUP BY a.name
ORDER BY p.price DESC

如果您只想要单个MAX价格,请使用此

SELECT MAX(p.price), (SELECT MAX(b.name) FROM a b LEFT JOIN price pp ON b.id = pp.tk_a WHERE a.id = b.id AND p.price = pp.price) AS name
FROM price
LEFT JOIN a ON a.id = p.tk_a
ORDER BY p.price DESC

答案 2 :(得分:1)

试试这个:

select a1.name, p1.price
from price p1
JOIN a a1 ON a1.id = p1.tk_a
JOIN (SELECT MAX(p.price)
      FROM price p) C on p1.price = c.price

答案 3 :(得分:0)

使用此

SELECT * 
FROM (SELECT a.name,MAX( price.price)
FROM price
LEFT JOIN a 
ON a.id = price.tk_a
GROUP BY a.name 
ORDER BY price.price DESC)