Sql 动态获取数字列的最大和最小允许值?

Sql 动态获取数字列的最大和最小允许值?,sql,oracle,oracle10g,Sql,Oracle,Oracle10g,假设有一个名为money的列,该列不为NULL,其类型为NUMBERx,y。该列没有约束 我想按照这个顺序对这个money列进行排序,+ve>-ve>0,所以我的计划是将0值解码为money列可以在orderby子句中保存的最小允许值,如select*from tableXXX order by decodemoney,0,allowablenimumvalueformoneycolumn,money desc。我只是想知道是否有可能动态获取money列的最小允许值 如何获取列的最大和最小允许值

假设有一个名为money的列,该列不为NULL,其类型为NUMBERx,y。该列没有约束

我想按照这个顺序对这个money列进行排序,+ve>-ve>0,所以我的计划是将0值解码为money列可以在orderby子句中保存的最小允许值,如select*from tableXXX order by decodemoney,0,allowablenimumvalueformoneycolumn,money desc。我只是想知道是否有可能动态获取money列的最小允许值


如何获取列的最大和最小允许值?oracle是否有隐式变量来执行此操作?

您需要在列上创建检查约束:

CREATE TABLE TEST (MONEY NUMBER(14,2) NOT NULL)
/

ALTER TABLE TEST ADD
  CONSTRAINT MONEY_VALID CHECK (( MONEY > 100 AND MONEY < 5000))
/

-- This fails
INSERT INTO TEST VALUES (20);

-- This works
INSERT INTO TEST VALUES (110);

似乎您希望其货币价值=0的记录最后出现

如果是这种情况,您可以使用这样的订单条款:

order by 
case when money = 0 then 0
                    else 1 
end desc,
money desc
如果有一个有效的例子,那就是

create table tq84_order_by (
  txt   varchar2(10),
  money number not null
);

insert into tq84_order_by values ('aaa', 0);
insert into tq84_order_by values ('bbb', 2);
insert into tq84_order_by values ('ccc',-3);
insert into tq84_order_by values ('ddd', 4);
insert into tq84_order_by values ('eee', 1);

select * from tq84_order_by
order by 
case when money = 0 then 0
                    else 1 
                    end desc,
                    money desc;
导致

TXT             MONEY
---------- ----------
ddd                 4
bbb                 2
eee                 1
ccc                -3
aaa                 0    

你不必知道最小值。您可以将NULL视为最小值:

... ORDER BY decode(money, 0, NULL, money) NULLS LAST

愚蠢的防火墙不允许我发布SQL,因为它认为我在进行SQL注入攻击。也许有人能解决上述问题。你能提供更多关于你所追求的信息,比如样本数据吗?@OMG Ponies,我已经更新了我的问题。希望现在问题更清楚。