views:

71

answers:

3

Hi, I get a missing expression error when I try,

SELECT 'label1:'||distinct col1 from tab1;

Is there a way to get around this error? Thanks in advance.

Edit: The entire query is:

SELECT 'label1:'||distinct col1 from tab1 order by col1;
+2  A: 

The first error is because distinct is in the wrong place.

SELECT distinct 'label1:'|| col1 as c 
from tab1
ORDER BY c;

The second one mentioned in the comments is because you were ordering by col1. You need to alias the new column and order by the alias as above. (Note you can use col1 as the alias if you want)

Martin Smith
Tried that too!.This gives ORA-01791-Not a SELECTED expression error.
cctk11
Just googled that and that seems to be related to the ORDER By clause. Can you post your *whole* query?
Martin Smith
@Martin Smith Ah yes!. Please see the update.
cctk11
+2  A: 

DISTINCT is a part of a SELECT clause, not the function of the columns:

SELECT  DISTINCT 'label1:' || col1
FROM    tab1

Update:

To make it work with ORDER BY, use

SELECT  'label1:' || col1
FROM    (
        SELECT  DISTINCT col1
        FROM    tab1
        )
ORDER BY
        col1
Quassnoi
Thanks I missed that part.
cctk11
+2  A: 

try this one

SELECT  DISTINCT 'label1:' || col1
FROM tab1 order by 1;
Bharat
Thanks. Could you explain how it works for '1' at the end?
cctk11
"1" specifies the Column Position i.e, on which column order by should be performed. If you specify order by 2,1 , then order by performed on 2nd column followed by 1st in the query
Bharat
@cctk11 It orders by column index. (i.e. the first and only column in your result set). I would avoid this practice and order by column alias instead as it is less fragile if you add another column in the select and forget to update the order by.
Martin Smith