Solved

Select distinct values but order by custom sort

Posted on 2014-11-04
10
254 Views
Last Modified: 2014-11-04
I wrote this query:

select distinct duration from jobs where duration<>'' and not(duration is null) and category='Teaching' order by case when duration='Full-Time' then 1 when duration='Part-Time' then 2 when duration='Long-Term Substitute' then 3 else duration end

But I get the usual:
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

I have read other posts on this exact topic, but complex SQL is over my head, and I can't understand how to fix this. Even looking at other examples, I am confused. I simply want to sort the results based on those three values.

Thank you.
0
Comment
Question by:bbdesign
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40422736
No idea, but you could avoid the issue and use a subquery
SELECT duration
FROM (
   select distinct duration 
   from jobs 
   where duration <> '' and not(duration is null) and category='Teaching') a
order by case 
   when duration='Full-Time' then 1 
   when duration='Part-Time' then 2 
   when duration='Long-Term Substitute' then 3 
   else duration end

Open in new window

btw using the return key and tabs to indent will make your T-SQL much more readable.
0
 
LVL 11

Accepted Solution

by:
Dany Balian earned 500 total points
ID: 40422739
try this
select duration from jobs where duration<>'' and not(duration is null) and category='Teaching' 
group by duration 
order by case when duration='Full-Time' then 1 when duration='Part-Time' then 2 when duration='Long-Term Substitute' then 3 else duration end

Open in new window

0
 

Author Comment

by:bbdesign
ID: 40422743
Works for me, thank you.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:bbdesign
ID: 40422749
Those both work, actually. "group by" still confuses me, but probably the cleaner way to go?
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40422759
yes for this case, both work.. group by could be easier to implement if you have joins with other tables as well, and also cleaner to read (for me)
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40422774
> "group by" still confuses me, but probably the cleaner way to go?
As an aside, eyeballeth thy super dooper article  SQL Server GROUP BY Solutions, which is an image and screenshot-heavy tutorial on GROUP BY, and should get you up to speed.

If you like it please hit the 'Yes' button next to 'Vote this article as helpful'?
0
 

Author Comment

by:bbdesign
ID: 40422788
Thanks for the suggestion, I may do just that!
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 40422789
SELECT duration FROM Jobs
WHERE duration<>'' and not(duration is null) and category='Teaching'
ORDER BY      
   case when duration='Full-Time' then '1'
   when duration='Part-Time' then '2'
   when duration='Long-Term Substitute' then '3'
    else duration  end ASC
0
 

Author Comment

by:bbdesign
ID: 40422803
All you did was add "asc" to the end?
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 40422817
Yeah sorry about that, I try to create a temp table on my local and test it out before i post it. So it took lot of time for me to post. Please ignore my post as solution provided by previous expert will work perfectly.
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question