2016-07-04 52 views
1

我有查詢:查詢 - 按返回重複行一列此查詢

SELECT DISTINCT 
"id", 
        "fn" || ' ' || "ln" || '/' || "bi" || '('|| TRUNC((months_between(sysdate, "bi") /12)) || ')/' || "pol" || '/ ' || "pol2" AS "INFO", 
    "cn" , 
    "ci", 
    TO_CHAR("di" , 'DD-MON-YYYY') AS "DIST", 
    "cif" AS "Cand. Indp." AS "CIF FLAG", 
    TO_CHAR("ecd", 'DD-MON-YYYY') AS "ECD DATE", 
    trim(both '/' from ("DSD")) AS "DSD DATE", 
    trim(both '/' from ("TSD" )) AS "TSD DATE", 
    "com" AS "COMMENTS", 
    "SMF" AS "SM Flag" 
FROM "MY_TABLE" 
ORDER BY "ln"; 

我得到ORA-01791:不是一個選擇的表達。

我GOOGLE了錯誤和問題似乎是獨特的和順序通過不一起工作。我試圖通過分組而不是明確的並使用max(「ln」),但是對於連接的列,我無法弄清楚如何對它們進行分組。我正在尋找如何對ln上的結果進行排序?

+0

'distinct'更改訂單和'order by'一起工作。您只需要在選擇列表中包含要排序的列。 'MY_TABLE'的主鍵是什麼? –

+0

@a_horse_with_no_name沒有主鍵。這也是一個臨時表。 – user2924127

回答

1

您不受ORDER BY 「LN」 選擇

只能通過選擇列,然後選擇或添加選擇

SELECT DISTINCT 
    "id", 
    "fn" || ' ' || "ln" || '/' || "bi" || '('|| TRUNC((months_between(sysdate, "bi") /12)) || ')/' || "pol" || '/ ' || "pol2" AS "INFO", 
    "ln", 
    "cn" , 
    "ci", 
    TO_CHAR("di" , 'DD-MON-YYYY') AS "DIST", 
    "cif" AS "Cand. Indp." AS "CIF FLAG", 
    TO_CHAR("ecd", 'DD-MON-YYYY') AS "ECD DATE", 
    trim(both '/' from ("DSD")) AS "DSD DATE", 
    trim(both '/' from ("TSD" )) AS "TSD DATE", 
    "com" AS "COMMENTS", 
    "SMF" AS "SM Flag" 
FROM "MY_TABLE" 
ORDER BY "ln"; 

SELECT DISTINCT 
    "id", 
    "fn" || ' ' || "ln" || '/' || "bi" || '('|| TRUNC((months_between(sysdate, "bi") /12)) || ')/' || "pol" || '/ ' || "pol2" AS "INFO", 
    "cn" , 
    "ci", 
    TO_CHAR("di" , 'DD-MON-YYYY') AS "DIST", 
    "cif" AS "Cand. Indp." AS "CIF FLAG", 
    TO_CHAR("ecd", 'DD-MON-YYYY') AS "ECD DATE", 
    trim(both '/' from ("DSD")) AS "DSD DATE", 
    trim(both '/' from ("TSD" )) AS "TSD DATE", 
    "com" AS "COMMENTS", 
    "SMF" AS "SM Flag" 
FROM "MY_TABLE" 
ORDER BY 2; 
+0

啊,我沒有意識到它必須是select語句中它自己的獨立列。有用! – user2924127

+0

好..如果我的回答是正確的,請將其標爲已接受.. – scaisEdge

+0

SO讓我等到我可以將其標記爲已回答 – user2924127