`

Order SQL data ORDER as in IN clause

阅读更多

example:


select * from t_cu where cno IN (
220,
120,
900,
350,
99,
1,
34
);
 



I would like to display the data in the same order as 

220,
120,
900,
350,
99,
1,
34

 

 

 

=========================A

select * from t_cu where cno IN (
220,
120,
900,
350,
99,
1,
34
)
order by decode(cno,
220, 1,
120, 2,
900, 3,
350, 4,
99,  5,
1,   6,
34,  7,
8);

=========================B

SQL> ed
Wrote file afiedt.buf
 
  1  with req as (select '7844,7698,7782,7499' as en from dual)
  2      ,ord as (select REGEXP_SUBSTR (en, '[^,]+', 1, level) en, rownum rn
  3               from req
  4               connect by level <= length(regexp_replace(en,'[^,]*'))+1)
  5  --
  6  select emp.*
  7  from emp join ord on (emp.empno = ord.en)
  8* order by ord.rn
SQL> / 
 
     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
 

=========================C

SQL>  with t as (select t.*, rownum r
             from table (sys.odcinumberlist (7788,
                                             7934,
                                             7782,
                                             7902,
                                             7369,
                                             7499,
                                             7566)) t)
  select empno, ename
    from emp e, t
   where e.empno = column_value
order by r
/ 
     EMPNO ENAME     
---------- ----------
      7788 SCOTT     
      7934 MILLER    
      7782 CLARK     
      7902 FORD      
      7369 SMITH     
      7499 ALLEN     
      7566 JONES     
 
7 rows selected.

=========================

分享到:
评论
1 楼 kavinhub 2013-02-04  
ref:
http://blog.csdn.net/ssqtjffcu1/article/details/8568065

1、table函数。用于构建虚拟表,并且可以套用同个from子句下其他表的值。为构建虚拟行的基础。
2、cast类型转换函数。用于将数据转换成行。
3、multiset函数。用于将数据转换成结果集的形式。
4、sys.odcinumberlist类型。告诉cast将子查询以什么类型返回结果。

----------
select column_value A from table(sys.odcinumberlist(1,2,3,4,5,6,7,8,9)) ;     
sys.odcinumberlist 函数把字符 纵向列出

也可以使用:
select count(*) from xmltable('1 to 9');

相关推荐

    微软内部资料-SQL性能优化5

    In a nonclustered index, the leaf level contains each index key, plus a bookmark that tells SQL Server where to find the data row corresponding to the key in the index. A bookmark can take one of two ...

    PLSqlsample

    When a customer creates a shopping cart and checks out the cart, the order data is stored in the ALL_ORDERS table. Assuming that the application needs to separate the newly created records and to be ...

    unidac64src 6.4.15

    Bug with QueryRecCount when ORDER clause contains CASE..END is fixed Bug with processing parameters on setting of an SQL query is fixed Oracle data provider Bug with using DATEADD function in ...

    SQLPrompt_7.3.0.651(包含注册机)

    Support ticket 76423: ambiguous columns are now always qualified with their table/alias inside an ORDER BY clause 7.3.0.642 - 27th October Support for user account licensing (More info) 7.3.0.639 - ...

    SQL示例大全.pdf

    Syntax for CAST: CAST ( expression AS data_type [ (length ) ]) Syntax for CONVERT: CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) 不带世纪数位 (yy) (1) 带世纪数位 (yyyy) 标准 输入/...

    微软内部资料-SQL性能优化3

    An intent lock indicates that SQL Server wants to acquire a shared (S) lock or exclusive (X) lock on some of the resources lower down in the hierarchy. For example, a shared intent lock placed at the ...

    SSD7 选择题。Multiple-Choice

    The foreign key in a table T1 _____ the same _____ as the corresponding primary key in table T2. must have, name need not have, name must have, domain (a) I, II, and III (b) I and II (c) ...

    Database Processing Fundamentals, Design, and Implementation (12th Edition).rar

    Combing the SQL WHERE Clause and the SQL ORDER BY Clause Performing Calculations in SQL Queries Using SQL Built-in Functions SQL Expressions in SQL SELECT Statements Grouping in SQL SELECT Statements ...

    UniDAC 7.1.4

    In order to continue using the products simultaneously, you should upgrade all of them at the same time. 7.1.4 29-Sep-17 InterBase data provider Bug with the "Input parameter mismatch" error for ...

    SQL Prompt_9.1.4.4532破解版

    SP-6847 : Added support for WITHIN GROUP order clause on STRING_AGG function. SP-6853 : Fix CTE/Cursor formatting error when automatically insert semicolons option is on. SP-6865 : 'Align data types ...

    SQLDBDiffFreeware.3.5.1数据库对比

    SQLDBDiff allow you : Ø Compare the structure of two SQL Server databases The following objects are supported : ...Add WHERE or ORDER BY clause. Ø Generate data synchronization scripts.

    Sql for mysql

    CHAPTER 4 SQL in a Nutshell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.2 Logging ...

    MySql存储过程编程.chm

    Stored Programs as an Alternative to Expensive SQL Section 22.5. Optimizing Loops Section 22.6. IF and CASE Statements Section 22.7. Recursion Section 22.8. Cursors Section 22.9. Trigger ...

    Delphi7.1 Update

    * In the Project Manager, units are incorrectly displayed in the order that they are listed in the USES clause, rather than alphabetically. * Using collections containing component references and ...

    RxLib控件包内含RxGIF,全部源码及DEMO

    save and restore columns order and display width in ini-files and system registry; display icons for BLOB, memo, OLE and picture fields; select multiple records; convert columns headings to buttons. ...

    Oracle事例

    sql&gt; temporary tablespace temp quota 10m on data password expire sql&gt; [account lock|unlock] [profile profilename|default]; &lt;1&gt;.查看当前用户的缺省表空间 SQL&gt;select username,default_tablespace ...

    easyload 9.0

    MapInfo空间数据库建立 MapInfo空间数据库方案: 数据库:Oracle9i 数据上传工具:easyload ...use, this column must exist in order for MapX to correctly identify and apply record-level styles. NULL

    Beginning Microsoft Visual CSharp 2008 Wiley Publishing(english)

    orderby Clause 897 Ordering Using Method Syntax 897 Querying a Large Data Set 899 Aggregate Operators 902 Querying Complex Objects 905 Projection: Creating New Objects in Queries 909...

    最全的oracle常用命令大全.txt

    SQL&gt;select sum(bytes)/(1024*1024) as "size(M)" from user_segments where segment_name=upper('&index_name'); 4、序列号 查看序列号,last_number是当前值 SQL&gt;select * from user_sequences; 5、视图 ...

    Hibernate Reference Documentation3.1

    10.4.4. Queries in native SQL 10.5. Modifying persistent objects 10.6. Modifying detached objects 10.7. Automatic state detection 10.8. Deleting persistent objects 10.9. Replicating object between two...

Global site tag (gtag.js) - Google Analytics