Solved

Select distinct values but order by custom sort

Posted on 2014-11-04
10
243 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 65

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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

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 65

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

730 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