写一条SQL语句,在线等待
假如有两个字段A,B,如果B为空的话,我想SELECT A FROM TABLE WHRE A > X,如果B不为空的话,我想SELECT B FROM TABLE WHRE B > X,就不关心A了。一条语句如何实现? 问题点数:50、回复次数:14Top
1 楼CodeMagic(ErrorDetector)回复于 2004-12-03 15:50:45 得分 5
select a from tb where a>x and b is null
union all
select b from tb where b>x and b is not nullTop
2 楼zmgowin(hermit)回复于 2004-12-03 15:51:30 得分 10
select a from table where a>x and b is null
union all
select b from table where b>x and b is not null;
or
select nvl(b,a) from table where decode(b,null,a,b)>x;Top
3 楼afantict(非洲土狼)回复于 2004-12-03 15:53:16 得分 0
decode什么意思?Top
4 楼xhy818(zh1860)回复于 2004-12-03 15:54:51 得分 5
是這個意思嗎?
SELECT A FROM TABLE WHRE A > Xand B is null
union
SELECT B FROM TABLE WHRE B > X and B is not nullTop
5 楼afantict(非洲土狼)回复于 2004-12-03 15:56:34 得分 0
select nvl(b,a) from table where b>x;
这样可以吗?
Top
6 楼LGQDUCKY(飘)回复于 2004-12-03 15:57:31 得分 10
decode 是ORACLE的函数,相当于 如果 。。。 就 。。。。Top
7 楼afantict(非洲土狼)回复于 2004-12-03 16:08:24 得分 0
nvl(b,a)
的意思是如果b字段为空,就取a字段吗?Top
8 楼vipyami()回复于 2004-12-03 16:10:36 得分 5
select NVL(B,A) where NVL(B,A)>xTop
9 楼Nicholas_zhao()回复于 2004-12-03 16:14:31 得分 5
SELECT NVL(B,A) FROM TABLE WHERE NVL(B,A) > XTop
10 楼sosolong(solong)回复于 2004-12-03 16:14:54 得分 5
这个效率高些:
select nvl(b,a) from table where decode(b,null,a,b)>x;Top
11 楼afantict(非洲土狼)回复于 2004-12-03 16:16:27 得分 0
select nvl(b,a) from table where decode(b,null,a,b)>x;
这条语句里面的
nvl(b,a)
的意思是如果b字段为空,就取a字段吗?
我可以select nvl(b,a) as abcd from table where decode(b,null,a,b)>x order by abcd;吗?
Top
12 楼afantict(非洲土狼)回复于 2004-12-03 16:39:35 得分 0
假如有两个字段A,B,如果B为空的话,我想SELECT A FROM TABLE WHRE A > X,如果B不为空的话,我想SELECT B FROM TABLE WHRE B > X,就不关心A了。一条语句如何实现?
如果我还想在这条语句里面统计字段C的和,放在一条语句里面
select nvl(b,a),SUM(C) from table where decode(b,null,a,b)>x
这样写可以吗?
应该怎么写?
Top
13 楼zmgowin(hermit)回复于 2004-12-03 16:44:26 得分 5
后面带上group by nvl(b,a)Top
14 楼afantict(非洲土狼)回复于 2004-12-03 16:51:25 得分 0
我不想group
我想每条都取Top




