How to order by case insensitive ASC or DESC, with DISTINCT and UNION

ε祈祈猫儿з 提交于 2019-12-06 04:43:28

问题


How to order by case insensitive ASC or DESC for P/L sql 11g. this p/l sql basic question but i can't find good answer in Google please tell how to sort the select result case insensitive

this what i tried

   SELECT  DISTINCT
            asssss,
            saas_acc
      FROM DUAL
   UNION SELECT '--ALL--','ALL' FROM DUAL
   ORDER BY  upper(asssss) ASC ;

that gave to me ORA-01785: ORDER BY item must be the number of a SELECT-list expression


回答1:


DISTINCT actually filtered the UNIQUE content in the result set, with whatever expressions given in the SELECT clause.

We cannot order it using a Different expression or column name. Please see the example here.

SQL> l
  1  SELECT DISTINCT  (col1),(col2)
  2  FROM
  3    ( SELECT 'Hello' col1,'World' col2 FROM DUAL
  4    UNION ALL
  5    SELECT 'HELLO','WORLD' FROM DUAL
  6*   )
SQL> /

COL1  COL2
----- -----
HELLO WORLD
Hello World

You can see that DISTINCT is CASE SENSITIVE here.(2 rows displayed)


So, let me Do a UPPER() on both columns.

SQL> l
  1  SELECT DISTINCT UPPER (col1),UPPER(col2)
  2  FROM
  3    ( SELECT 'Hello' col1,'World' col2 FROM DUAL
  4    UNION ALL
  5    SELECT 'HELLO','WORLD' FROM DUAL
  6*   )
SQL> /

UPPER UPPER
----- -----
HELLO WORLD

Just 1 row is Displayed, ignoring the case.


Coming back to the actual problem. To order something on a DISTINCT Resultset, it has to be a part of DISTINCT clause's expression/column.

So, When you issue DISTINCT COL1,COl2, the order by may be by COL1 or COL2/.. it cannot be COL3 or even UPPER(COL1) because UPPER() makes a different expression conflicting the expression over DISTINCT.


Finally, Answer for your Question would be

if you want your ORDER to be case-insensitive, DISTINCT also has to the same way! As given below

SELECT  DISTINCT
            UPPER(asssss),
            saas_acc
      FROM DUAL
   ORDER BY  upper(asssss) ASC ;

OR if UNION has to be used, better do this, or same as above one.

SELECT * FROM    
(
  SELECT  DISTINCT asssss as asssss,
          saas_acc
  FROM DUAL
 UNION
 SELECT '--ALL--','ALL' FROM DUAL
)
ORDER BY  upper(asssss) ASC ;

Out of my own Experience, I had always felt, what ever expression/column is specified in the ORDER BY, it is implicitly taken to final SELECT as well. Ordering is just based on the column number(position) in the result actually . In this situation, DISTINCT COL1,COl2 is already there. When you give ORDER BY UPPER(COL1), it will be tried to append into the SELECT expression, which is NOT possible at all. So, Semantic check itself, would disqualify this query with an Error!




回答2:


To sort case insensitive you need to set the NLS_COMP to ANSI

   NLS_COMP=ANSI

Details: http://www.orafaq.com/node/999




回答3:


The simplest option would be to sort by the upper- (or lower-) case column data

ORDER BY UPPER( column_name )



回答4:


You can use upper or lower functions.

order by upper(columnName)

Update1

Try removing order-by clause from your query which will give you correct error, which is ORA-00904: "SAAS_ACC": invalid identifier. So you can search on google for this error or ask another question on SO.

Also have a look at how to use order by in union.



来源:https://stackoverflow.com/questions/21327286/how-to-order-by-case-insensitive-asc-or-desc-with-distinct-and-union

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!