Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select Distinct and Order By Sequence

Author  Topic 

eevans
Starting Member

48 Posts

Posted - 2009-02-13 : 10:36:45
Here is my ORDER BY statement...

ORDER BY
(CASE WHEN class_cde='sr' THEN 1
WHEN class_cde='jr' THEN 2
WHEN class_cde='so' THEN 3
WHEN class_cde='fr' THEN 4
END)

I am using it in conjunction with a SELECT DISTINCT statement.

When I execute the query, I receive the following error message...

"ORDER BY items must appear in the select list if SELECT DISTINCT is specified."

The class_code column is contained in the SELECT DISTINCT statement.

Any suggestions? Thanks.

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-02-13 : 10:39:49
Maybe this is the reason
clas_code <> class_cde
Can you please post the whole Select?
Go to Top of Page

eevans
Starting Member

48 Posts

Posted - 2009-02-13 : 10:59:36
"clas_code <> class_cde"

Not sure what you mean.

Also, I might mention that the when I change the SELECT DISTINCT statement to a plain SELECT statement, the ORDER BY statement works fine.

Thanks.
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-13 : 11:01:16
Post your whole select statement.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-13 : 11:05:19
quote:
Originally posted by eevans

Here is my ORDER BY statement...

ORDER BY
(CASE WHEN class_cde='sr' THEN 1
WHEN class_cde='jr' THEN 2
WHEN class_cde='so' THEN 3
WHEN class_cde='fr' THEN 4
END)

I am using it in conjunction with a SELECT DISTINCT statement.

When I execute the query, I receive the following error message...

"ORDER BY items must appear in the select list if SELECT DISTINCT is specified."

The class_code column is contained in the SELECT DISTINCT statement.

Any suggestions? Thanks.


you need to include the case..when...construct in select as well

ie. like

SELECT DISTINCT CASE WHEN class_cde='sr' THEN 1
WHEN class_cde='jr' THEN 2
WHEN class_cde='so' THEN 3
WHEN class_cde='fr' THEN 4
END,
....

ORDER BY
(CASE WHEN class_cde='sr' THEN 1
WHEN class_cde='jr' THEN 2
WHEN class_cde='so' THEN 3
WHEN class_cde='fr' THEN 4
END)

or you can use like this also

SELECT DISTINCT OrdField,..
FROM
(
SELECT CASE WHEN class_cde='sr' THEN 1
WHEN class_cde='jr' THEN 2
WHEN class_cde='so' THEN 3
WHEN class_cde='fr' THEN 4
END AS OrdField,..
...
)t
ORDER BY OrdField
Go to Top of Page

eevans
Starting Member

48 Posts

Posted - 2009-02-13 : 11:17:50
visakh16,
Your solution works. However, I end up with an unnecessary column containing the ordering values (1,2,3,4) in place of the actual values (SR,JR,SO,FR). Is there any way to eliminate this extra column? Thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-13 : 11:22:12
quote:
Originally posted by eevans

visakh16,
Your solution works. However, I end up with an unnecessary column containing the ordering values (1,2,3,4) in place of the actual values (SR,JR,SO,FR). Is there any way to eliminate this extra column? Thanks.


nope. you cant eliminate it in query if you need to use DISTINCT. b/w why are you using distinct here?
Go to Top of Page

eevans
Starting Member

48 Posts

Posted - 2009-02-13 : 11:26:35
Thanks. I appreciate you help.

The query I'm writing has a lot more variables that requires me to use SELECT DISTINCT
Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2009-02-13 : 12:21:24
Couldn't you just put the DISTINCT part into a subquery?


SELECT A.*
FROM (
SELECT DISTINCT class_cde, ...
FROM ...
) AS A
ORDER BY
(CASE WHEN class_cde='sr' THEN 1
WHEN class_cde='jr' THEN 2
WHEN class_cde='so' THEN 3
WHEN class_cde='fr' THEN 4
END)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-13 : 12:36:46
quote:
Originally posted by eevans

Thanks. I appreciate you help.

The query I'm writing has a lot more variables that requires me to use SELECT DISTINCT


lot more variables or values?
Go to Top of Page
   

- Advertisement -