?
Solved

Select distinct values but order by custom sort

Posted on 2014-11-04
10
Medium Priority
?
269 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 2000 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

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…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Suggested Courses

765 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