`

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 ...

    SqlServer:使用IN()子句C#进行参数化查询

    文件名中的"SqlServer-Parameterized-Query-With-IN-Clause-Cshar.pdf"可能是详细解释这一主题的文档,而"LogOn.aspx?rp=%2FKB%2Fdatabase%2F1276827%2FInClauseParam.zip&download=true"则可能指向一个包含更多示例...

    SAS.9.2.SQL.Procedure.Users.Guide

    PROC SQL and the SAS DATA step are two different methods for processing data in SAS, each with its own strengths and use cases: - **PROC SQL**: - **Strengths**: - Easier syntax for complex queries....

    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 - ...

    Oracle SQL查考手册chm

    Oracle SQL查考手册是一部关于Oracle数据库查询语言的重要参考资料,它涵盖了运算符、表达式、条件、函数以及常见的SQL DDL(Data Definition Language)和Clause等内容。这篇总结将深入解析这些核心概念,帮助读者...

    Cody‘s_Data_Cleaning_Techniques_Using_SAS_(Second_Edtion)

    - **Checking That the Dates Are in the Proper Order**: Describes how to ensure that dates are in the correct order across multiple files. #### Double Entry and Verification (PROC COMPARE) This ...

    微软内部资料-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 ...

    SQL_ the Basics.pdf

    - subqueries in the "from" clause:在FROM子句中嵌套查询,用于复杂的数据检索。 - grouping, aggregation, and having clauses:通过GROUP BY进行分组,使用AGGREGATE函数(如SUM, AVG)进行聚合,HAVING子句则在...

    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 ...

    Oracle数据库通用的分页存储过程

    p_where_clause IN VARCHAR2, p_data OUT SYS_REFCURSOR ) AS BEGIN OPEN p_data FOR SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT /*+ FIRST_ROWS(p_page_size) */ * FROM p_table_name WHERE p_...

    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 ...

    数据库SQL*splus命令大全

    - `WHERE clause`: 用于过滤查询结果,只返回满足条件的记录。 11. **去除重复记录** - `DISTINCT`: 在查询结果中去除重复记录。 12. **函数和表达式** - `NVL(expression, default_value)`: 如果expression为...

    SQL Server 2012 Tutorials - Transact SQL DML Reference

    此外,书中还详细介绍了各种提示(Hints)、子句(Clauses)和其他语法结构,如JOIN Hints、Query Hints、Table Hints、OUTPUT Clause、SEARCH Condition、GROUP BY、HAVING、INTO Clause、ORDER BY、OVER Clause、...

    SQL Server 2012 Transact-SQL DML Reference

    - `SELECT column_name, SUM(column_name) OVER (ORDER BY column_name ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS rolling_sum FROM table_name;` #### Table Value Constructor 表值构造器用于创建临时表,...

    pivot_unpivot

    - `FOR quarter IN (q1_sales AS 'Q1', q2_sales AS 'Q2', q3_sales AS 'Q3', q4_sales AS 'Q4')`指定了原列中的值以及对应的季度名称。 ##### 3.4 注意事项 - `UNPIVOT`操作同样支持使用`WITH`子句来创建临时表或...

    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.

Global site tag (gtag.js) - Google Analytics