如何在 Oracle 中找到负最大值和正最小值?
How to find Negative maximum and positive minimum in Oracle?
我有一个 number
类型的列。它既有正值也有负值。我需要获得 4 个值:正最大值、正最小值、负最大值、负最小值:
a) 获得正最大值:我可以使用 Check out the Fiddle
select max(cola) from test;
b) 获得负最小值:我可以使用 Check out the Fiddle
select min(cola) from test;
我有两个问题:
1)现在我不确定如何获得其他两个值。指导我得到那个
2)与此同时,在尝试此操作时我又产生了疑问。当我有一些 varchar2
类型的列并且它有数字作为值时。我在本专栏中执行上述操作。正最大值同上。但是负最小值很奇怪。 Check Fiddle Here .为什么这里没有进行适当的隐式转换。有人请解释这背后的原因吗?
在聚合函数中使用案例表达式。
例如
max(当可乐 < 0 时可乐结束)max_neg
min(cola) min_neg -- 这里不需要大小写表达式
Oracle 11g R2 架构设置:
Create table test(COLA number);
Insert into test values(1);
Insert into test values(50);
Insert into test values(-65);
Insert into test values(25);
Insert into test values(-2);
Insert into test values(-8);
Insert into test values(5);
Insert into test values(-11);
Create table test1(COLA varchar2(10));
Insert into test1 values('1');
Insert into test1 values('50');
Insert into test1 values('-65');
Insert into test1 values('25');
Insert into test1 values('-2');
Insert into test1 values('-8');
Insert into test1 values('5');
Insert into test1 values('-11');
查询 1:
select
max(case when cola < 0 then cola end) max_neg_cola
, min(cola)
, min(case when cola > 0 then cola end) min_pos_cola
, max(cola)
from test
| MAX_NEG_COLA | MIN(COLA) | MIN_POS_COLA | MAX(COLA) |
|--------------|-----------|--------------|-----------|
| -2 | -65 | 1 | 50 |
对于问题 1,您可以轻松地使用 case 来确定您对哪些值执行 min/max。例如:
select max(case when cola >= 0 then cola end) max_positive,
min(case when cola >= 0 then cola end) min_positive,
max(case when cola < 0 then cola end) max_negative,
min(case when cola < 0 then cola end) min_negative
from test;
对于问题 2,当您对 varchar 对象执行 min/max 时,您将进行字符串比较,不是 数字比较。您必须将这些值显式转换为数字,因为 Oracle 不知道您希望进行隐式转换。而且你不应该真的依赖隐式转换。例如:
select max(case when to_number(cola) >= 0 then to_number(cola) end) max_positive,
min(case when to_number(cola) >= 0 then to_number(cola) end) min_positive,
max(case when to_number(cola) < 0 then to_number(cola) end) max_negative,
min(case when to_number(cola) < 0 then to_number(cola) end) min_negative
from test1;
Here's the SQLFiddle for both cases.
N.B。我已经明确地将负值和正值分开(我将 0 与正数放在一起;您必须决定如何处理值为 0 的行!),以防万一没有负数或没有正数。
要获得正的最小值试试这个..
select min(cola) from test where cola>0;
要获得负最大值试试这个..
select max(cola) from test where cola<0;
1)Now i'm not sure how to get the other two values.
使用CASE表达式。
SQL> SELECT MAX(
2 CASE
3 WHEN cola >= 0
4 THEN cola
5 END) max_positive,
6 MIN(
7 CASE
8 WHEN cola >= 0
9 THEN cola
10 END) min_positive,
11 MAX(
12 CASE
13 WHEN cola < 0
14 THEN cola
15 END) max_negative,
16 MIN(
17 CASE
18 WHEN cola < 0
19 THEN cola
20 END) min_negative
21 FROM test;
MAX_POSITIVE MIN_POSITIVE MAX_NEGATIVE MIN_NEGATIVE
------------ ------------ ------------ ------------
50 1 -2 -65
SQL>
2)Meanwhile while trying this i got another doubt. When i have some column of type varchar2 and it has numbers as value. I'm performing the above operation in this column. Positive maximum is same as above. But Negative minimum is quiet weird.
您需要先将 STRING 转换为 NUMBER,然后使用相同的查询。为了减少每次输入 to_number,您可以使用 WITH 子句。
注意 此列中只能有数字,不能有字母数字。否则,一定要得到 ORA-01722: invalid number
错误。
SQL> WITH t AS
2 ( SELECT to_number(cola) cola FROM test1
3 )
4 SELECT MAX(
5 CASE
6 WHEN cola >= 0
7 THEN cola
8 END) max_positive,
9 MIN(
10 CASE
11 WHEN cola >= 0
12 THEN cola
13 END) min_positive,
14 MAX(
15 CASE
16 WHEN cola < 0
17 THEN cola
18 END) max_negative,
19 MIN(
20 CASE
21 WHEN cola < 0
22 THEN cola
23 END) min_negative
24 FROM t;
MAX_POSITIVE MIN_POSITIVE MAX_NEGATIVE MIN_NEGATIVE
------------ ------------ ------------ ------------
50 1 -2 -65
SQL>
我有一个 number
类型的列。它既有正值也有负值。我需要获得 4 个值:正最大值、正最小值、负最大值、负最小值:
a) 获得正最大值:我可以使用 Check out the Fiddle
select max(cola) from test;
b) 获得负最小值:我可以使用 Check out the Fiddle
select min(cola) from test;
我有两个问题:
1)现在我不确定如何获得其他两个值。指导我得到那个
2)与此同时,在尝试此操作时我又产生了疑问。当我有一些 varchar2
类型的列并且它有数字作为值时。我在本专栏中执行上述操作。正最大值同上。但是负最小值很奇怪。 Check Fiddle Here .为什么这里没有进行适当的隐式转换。有人请解释这背后的原因吗?
在聚合函数中使用案例表达式。
例如
max(当可乐 < 0 时可乐结束)max_neg
min(cola) min_neg -- 这里不需要大小写表达式
Oracle 11g R2 架构设置:
Create table test(COLA number);
Insert into test values(1);
Insert into test values(50);
Insert into test values(-65);
Insert into test values(25);
Insert into test values(-2);
Insert into test values(-8);
Insert into test values(5);
Insert into test values(-11);
Create table test1(COLA varchar2(10));
Insert into test1 values('1');
Insert into test1 values('50');
Insert into test1 values('-65');
Insert into test1 values('25');
Insert into test1 values('-2');
Insert into test1 values('-8');
Insert into test1 values('5');
Insert into test1 values('-11');
查询 1:
select
max(case when cola < 0 then cola end) max_neg_cola
, min(cola)
, min(case when cola > 0 then cola end) min_pos_cola
, max(cola)
from test
| MAX_NEG_COLA | MIN(COLA) | MIN_POS_COLA | MAX(COLA) |
|--------------|-----------|--------------|-----------|
| -2 | -65 | 1 | 50 |
对于问题 1,您可以轻松地使用 case 来确定您对哪些值执行 min/max。例如:
select max(case when cola >= 0 then cola end) max_positive,
min(case when cola >= 0 then cola end) min_positive,
max(case when cola < 0 then cola end) max_negative,
min(case when cola < 0 then cola end) min_negative
from test;
对于问题 2,当您对 varchar 对象执行 min/max 时,您将进行字符串比较,不是 数字比较。您必须将这些值显式转换为数字,因为 Oracle 不知道您希望进行隐式转换。而且你不应该真的依赖隐式转换。例如:
select max(case when to_number(cola) >= 0 then to_number(cola) end) max_positive,
min(case when to_number(cola) >= 0 then to_number(cola) end) min_positive,
max(case when to_number(cola) < 0 then to_number(cola) end) max_negative,
min(case when to_number(cola) < 0 then to_number(cola) end) min_negative
from test1;
Here's the SQLFiddle for both cases.
N.B。我已经明确地将负值和正值分开(我将 0 与正数放在一起;您必须决定如何处理值为 0 的行!),以防万一没有负数或没有正数。
要获得正的最小值试试这个..
select min(cola) from test where cola>0;
要获得负最大值试试这个..
select max(cola) from test where cola<0;
1)Now i'm not sure how to get the other two values.
使用CASE表达式。
SQL> SELECT MAX(
2 CASE
3 WHEN cola >= 0
4 THEN cola
5 END) max_positive,
6 MIN(
7 CASE
8 WHEN cola >= 0
9 THEN cola
10 END) min_positive,
11 MAX(
12 CASE
13 WHEN cola < 0
14 THEN cola
15 END) max_negative,
16 MIN(
17 CASE
18 WHEN cola < 0
19 THEN cola
20 END) min_negative
21 FROM test;
MAX_POSITIVE MIN_POSITIVE MAX_NEGATIVE MIN_NEGATIVE
------------ ------------ ------------ ------------
50 1 -2 -65
SQL>
2)Meanwhile while trying this i got another doubt. When i have some column of type varchar2 and it has numbers as value. I'm performing the above operation in this column. Positive maximum is same as above. But Negative minimum is quiet weird.
您需要先将 STRING 转换为 NUMBER,然后使用相同的查询。为了减少每次输入 to_number,您可以使用 WITH 子句。
注意 此列中只能有数字,不能有字母数字。否则,一定要得到 ORA-01722: invalid number
错误。
SQL> WITH t AS
2 ( SELECT to_number(cola) cola FROM test1
3 )
4 SELECT MAX(
5 CASE
6 WHEN cola >= 0
7 THEN cola
8 END) max_positive,
9 MIN(
10 CASE
11 WHEN cola >= 0
12 THEN cola
13 END) min_positive,
14 MAX(
15 CASE
16 WHEN cola < 0
17 THEN cola
18 END) max_negative,
19 MIN(
20 CASE
21 WHEN cola < 0
22 THEN cola
23 END) min_negative
24 FROM t;
MAX_POSITIVE MIN_POSITIVE MAX_NEGATIVE MIN_NEGATIVE
------------ ------------ ------------ ------------
50 1 -2 -65
SQL>