Oracle SQL语句排序依据

时间:2013-08-23 14:52:38

标签: sql oracle indexing oracle11g

我有以下查询:

    COLUMN INDEX_NAME FORMAT A15 HEADING "INDEX_NAME"
    COLUMN COLUMN_NAME FORMAT A15 HEADING "COLUMN_NAME"
    COLUMN COLUMN_POSITION FORMAT 999999 HEADING "COLUMN_POSITION"

    SELECT INDEX_NAME, COLUMN_NAME, COLUMN_POSITION
    FROM ALL_IND_COLUMNS
    WHERE TABLE_OWNER = 'ABC'

返回的结果如下:

    INDEX_NAME      COLUMN_NAME     COLUMN_POSITION
    --------------- --------------- ---------------
    SYS_C007963     C_UNAME                       1 
    ORDER_LINE_PKEY OL_ID                         1 
    ORDER_LINE_PKEY OL_O_ID                       2 
    ORDERS_PKEY     O_ID                          1 
    ITEM_PKEY       I_ID                          1 
    CUSTOMER_PKEY   C_ID                          1 
    COUNTRY_PKEY    CO_ID                         1 
    CC_XACTS_PKEY   CX_O_ID                       1 
    AUTHOR_PKEY     A_ID                          1 
    ADDRESS_PKEY    ADDR_ID                       1 

     10 rows selected 

如何更改sql语句,使其按重复INDEX_NAME的重复次数排序?

例如:

    INDEX_NAME      COLUMN_NAME     COLUMN_POSITION
    --------------- --------------- ---------------
    SYS_C007963     C_UNAME                       1  
    ORDERS_PKEY     O_ID                          1 
    ITEM_PKEY       I_ID                          1 
    CUSTOMER_PKEY   C_ID                          1 
    COUNTRY_PKEY    CO_ID                         1 
    CC_XACTS_PKEY   CX_O_ID                       1 
    AUTHOR_PKEY     A_ID                          1 
    ADDRESS_PKEY    ADDR_ID                       1 
    ORDER_LINE_PKEY OL_ID                         1 
    ORDER_LINE_PKEY OL_O_ID                       2

     10 rows selected 

1 个答案:

答案 0 :(得分:1)

您可以计算每个索引的数量,然后按顺序排序:

select INDEX_NAME, COLUMN_NAME, COLUMN_POSITION
FROM (SELECT INDEX_NAME, COLUMN_NAME, COLUMN_POSITION,
             count(*) over (partition by INDEX_NAME) as cnt
      FROM ALL_IND_COLUMNS
      WHERE TABLE_OWNER = 'ABC'
     ) t
order by cnt, INDEX_NAME