0

我有以下查询:

    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 
4

1 回答 1

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
于 2013-08-23T14:59:22.560 回答