这样的SQL怎么写?

baishu 2003-08-22 09:28:20
一张表T1中有字段(name,value)
数据为 :
n1 1
n2 2
n3 0
n4 5
n6 8
n7 3
完成这样的功能,先按照(n1,n4,n3)顺序,然后其他按value的顺序;我的SQL如下:(不能完成我要的结果)
(select * from T1 where hxysmc in('n1','n4','n3'))
union select * from T1 where hxysmc not in ('n1','n4','n3') order by value desc;
帮帮忙,先谢谢了。
...全文
61 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
beckhambobo 2003-08-23
  • 打赏
  • 举报
回复
SQL> select id from aa;

ID
--
1
2
3
4
5
6
6

7 rows selected

以下是以id in (1,2,3)顺序首先排列,然再按not in (1,2,3)降序排序
SQL> select id from
2 (select id,max(id) over() max_id from aa)
3 order by decode(id,'3',max_id+1,'2',max_id+2,'1',max_id+3,id) desc;

ID
--
1
2
3
6
6
5
4

7 rows selected

改写楼主如何语句:
select name,value from
(select name,value,max(value) over() max_value from T1)
order by decode(name,'n1',max_value+3,'n4',max_id+2,'n3',max_value+1,value) desc;
pumawang 2003-08-23
  • 打赏
  • 举报
回复
SQL> desc test
名称 空? 类型
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(20)
VALUE NUMBER

SQL> select * from test ;

NAME VALUE
-------------------- ----------
n1 1
n2 2
n3 0
n4 5
n6 8
n7 3

已选择6行。

SQL> ed
已写入文件 afiedt.buf

1 select name ,value ,decode(name,'n1','a00','n4','a01','n3','a02','b'||value) ordV
2 from Test
3* order by ordV
SQL> /

NAME VALUE ORDV
-------------------- ---------- -----------------------------------------
n1 1 a00
n4 5 a01
n3 0 a02
n2 2 b2
n7 3 b3
n6 8 b8

已选择6行。

SQL>
// 结束

不好意思, 前面少写了个 "|" 。 我已经测试通过了。不用什么Union 都可以了。

这个方法最好理解了。


tiangou 2003-08-22
  • 打赏
  • 举报
回复
我刚才在8i中试了一下,
发现用了一个视图就行了,如下:

================================================
SQL> select * from test where id in (5,15,23)
2 union
3 (select * from test where id not in(5,15,23) order by name)
4 ;

select * from test where id in (5,15,23)
union
(select * from test where id not in(5,15,23) order by name)

ORA-00907: missing right parenthesis

SQL>
SQL> select * from test where id in (5,15,23)
2 union
3 (select * from (select * from test where id not in(5,15,23) order by name))
4 ;

ID NAME
---------- ------------------------------
1 test
2 test
3 test
4 test
5 test
15 test
23 test
25 test
33 test
35 test
45 test
60 test
70 test
80 test
90 test
100 test
101 test
200 test
202 test
1000 test

20 rows selected

SQL>
tiangou 2003-08-22
  • 打赏
  • 举报
回复
按照(n1,n4,n3)顺序?你的意思我很费解,你的SQL语句前半部没有体现出按(n1,n4,n3)来排序呀
后面加个括号试过么?如
(select * from T1 where name in('n1','n4','n3'))
union (select * from T1 where name not in ('n1','n4','n3') order by value desc);
baishu 2003-08-22
  • 打赏
  • 举报
回复
改一下,字段错了:
(select * from T1 where name in('n1','n4','n3'))
union select * from T1 where name not in ('n1','n4','n3') order by value desc;
baishu 2003-08-22
  • 打赏
  • 举报
回复
union 好像不改变顺序,如:
select * form t1 where name='n1'
union
select * form t1 where name='n2';

select * form t1 where name='n2'
union
select * form t1 where name='n1';
结果是一样的
baishu 2003-08-22
  • 打赏
  • 举报
回复
我试了一下,好像都不行,我的意思是对所有name数据,形成n1,n3,n4总在前,其他的在后面,按照value的大小降序排序,
使用union合并后还是按照原来的顺序,表中的顺序就是显示的顺序,好像不能改变,怎么办。
baishu 2003-08-22
  • 打赏
  • 举报
回复
To pumawang:
后面的'b'|value什么意思

pumawang 2003-08-22
  • 打赏
  • 举报
回复
一个不是很通用的方法:
select name ,value ,decode(name,'V1','a00','V4','a01','V3','a02','b'|value) ordV
from table_name
order by ordV

意思大概你看的明白吧。

17,082

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧