从表中获取带有最小值和最大值的2行

时间:2019-01-23 04:22:14

标签: sql oracle

我有一个包含以下各列的产品表:

id  | product_name | price

1   | Red Shirt    | 10.0
2   | White Shirt  | 15.0
3   | Black Shirt  | 9.0
4   | Yellow Shirt | 12.0

如何进行查询以返回包含max(price)和min(price)的行? 使用上面的示例,输出应为:

id  | product_name | price

3   | Black Shirt  | 9.0
2   | White Shirt  | 15.0

对于重复值,选择哪个值都无所谓,只要输出只有2行即可。

4 个答案:

答案 0 :(得分:2)

还有两个答案不能满足最后一个要求...“如果重复值,则选择哪个值都无所谓,只要输出仅2行即可。”

如果有两个具有相同值的产品,它们将返回三行。

下面通过添加rownum = 1子句来解决这个问题。

with testtab (id, product_name, price) as
(select 1, 'Red Shrit', 10.00 from dual
 union 
 select 2, 'Whtie Shrit', 15.00 from dual
 union 
 select 3, 'Black Shrit', 9.00 from dual
 union 
 select 4, 'Yellow Shrit', 12.00 from dual
 union 
 select 4, 'Pink Shrit', 15.00 from dual)
select id, product_name, price 
from testtab 
where price in (select max(price) from testtab)
and rownum = 1
union
select id, product_name, price 
from testtab where price in (select min(price) from testtab)
and rownum = 1

答案 1 :(得分:2)

您可以使用ROW_NUMBER()分析函数来完成此操作,该函数仅使用一次表扫描(与使用UNION或子查询的解决方案不同)。

Oracle设置

CREATE TABLE product ( id, product_name, price ) AS
SELECT 1, 'Red Shirt',    10.0 FROM DUAL UNION ALL
SELECT 2, 'White Shirt',  15.0 FROM DUAL UNION ALL
SELECT 3, 'Black Shirt',   9.0 FROM DUAL UNION ALL
SELECT 4, 'Yellow Shirt', 12.0 FROM DUAL UNION ALL
SELECT 5, 'Blue Shirt',    9.0 FROM DUAL

查询

SELECT id, product_name, price
FROM   (
  SELECT p.*,
         ROW_NUMBER() OVER ( ORDER BY price ASC  ) As min_price_rn,
         ROW_NUMBER() OVER ( ORDER BY price DESC ) As max_price_rn
  FROM   product p
)
WHERE min_price_rn = 1
OR    max_price_rn = 1;

输出

ID | PRODUCT_NAME | PRICE
-: | :----------- | ----:
 2 | White Shirt  |    15
 3 | Black Shirt  |     9

db <>提琴here

答案 2 :(得分:0)

您可以在where条件中使用Or条件。

"ArgumentCountError: Too few arguments to function App\Tests\Controller\EvaplyTest::testGenerate_evaply_clid(), 0 passed and exactly 1 expected".  KIndly have a look and suggest me a solution.

输出:

with cte as (select 1 as ID, 'Red Shirt' as Product_name, 10.0 as price from dual 
union all 
select 2 as ID, 'White Shirt' as Product_name, 15.0 as price from dual 
union all 
select 3 as ID, 'Black Shirt' as Product_name, 9.0 as price from dual 
union all 
select 4 as ID, 'Yellow Shirt' as Product_name, 12.0 as price from dual ) 

select * from cte 
where price = (select min(price) minprice from cte) or price = (select max(price) 
maxprice from cte); 

答案 3 :(得分:0)

您也可以执行以下操作:

select *
from product
where price in (
          (select min(price) from product),
          (select max(price) from product)
);

或者您也可以像下面这样进行联合:

-UNION方法

Select *
from Product
where price = (select min(price) from Product)
union
select *
from Product
where price = (select max(price) from Product);

使用了以下内容:

Create Table Product(id int, product_name varchar(20), price decimal);

Insert into Product values (1, 'Red Shirt', 10.0);
Insert into Product values (2, 'White Shirt', 15.0);
Insert into Product values (3, 'Black Shirt', 9.0);
Insert into Product values (4, 'Yellow Shirt', 12.0);

Select *
from Product
where Price in (
          (select min(Price) from Product),
          (select max(Price) from Product)
);

最终输出:

Id  Product_Name    Price
2   White Shirt     15
3   Black Shirt     9