Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Order By 1

Status
Not open for further replies.

Naoise

Programmer
Dec 23, 2004
318
IE
tblFoo
ID | Foo
1 apple
2 orange
3 banana
4 pineapple
5 grapefruit

I want a statement to return the following, all the values alphabetically but 'pineapple' always the first result. So

pineapple
apple
banana
grapefruit
orange

 
That's deadly, 937 - the magic number (care to explain it? :) - this works for a query without distinct, I have

select distinct(Foo).... as there are many entries with similar values for Foo, so when I run the query with DISTINCT I get ORDER BY items must appear in the select list if SELECT DISTINCT is specified

Any ideas? Thanks
 
sorry, i don't understand your followup question, but i did notice that you wrote DISTINCT(Foo) and that's wrong, DISTINCT is not a function

r937.com | rudy.ca
 
Sorry, I was abbreviating

SELECT DISTINCT(Foo) .... should have been

SELECT DISTINCT(Foo) FROM tblFoo
order
by case when Foo='pineapple'
then 0 else 937 end
, Foo

So when I run this query with DISTINCT I get ORDER BY items must appear in the select list if SELECT DISTINCT is specified
 
why are you using the parentheses?

DISTINCT is not a function

besides, i don't think you need DISTINCT here at all

r937.com | rudy.ca
 
Ok, even with parenthese dropped the error is the same. I am simplifying a more complex query to ease the resolution.

tblFoo
ID | Foo
1 apple
2 orange
3 banana
4 pineapple
5 grapefruit
6 apple
7 banana

SELECT DISTINCT Foo, ID FROM tblFoo
order
by case when Foo='pineapple'
then 0 else 937 end
, Foo

I do need the distinct here, trust me, I have ammended the table above to show that there are multiples of each value for Foo. I just need a query that can give me

pineapple
apple
banana
grapefruit
orange

The order by clause worked a treat when used without DISTINCT, but I have to have it in. Any replies appreciated.
 
the problem is, if you want to include ID in the SELECT, you will have multiple different (i.e. DISTINCT) rows for each value of Foo

therefore, use GROUP BY instead of DISTINCT, and select only one of the ID values for each value of Foo, arbitrarily chosen here as the lowest ID --
Code:
select Foo, min(ID) as min_ID 
  from tblFoo
group
    by Foo
order
    by case when Foo='pineapple'
            then 0 else 937 end
     , Foo
of course, since you said "I just need a query that can give me..." and then listed the results without an ID, then what you really need is this --
Code:
select Foo
  from tblFoo
group
    by Foo
order
    by case when Foo='pineapple'
            then 0 else 937 end
     , Foo
:)



r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top