Author |
Topic Search Topic Options
|
snooper
Mod Builder Group
Joined: 23 April 2002
Location: Israel
Status: Offline
Points: 333
|
Topic: SQL DISTINCT stuff Posted: 12 February 2003 at 11:03am |
hey folks!
i have the followiong table example:
ID----Book-------Category ======================
1 e b 2 d b 3 w a 4 t a 5 e c
what i'd like to do is get the Distinct categories, but sort them as they are in the DB, ie: according to the ID, to get this:
b a c
but when i write :
"SELECT DISTINCT category FROM tbl ORDER BY ID ASC", i get an error, saying that the "order" clashes with the "Distinct" .
anyone got ideas how to do this>?
thanks! Snooper
|
 |
MorningZ
Senior Member
Joined: 06 May 2002
Location: United States
Status: Offline
Points: 1793
|
Posted: 12 February 2003 at 11:08am |
try:
SELECT DISTINCT(category), ID FROM tbl GROUP BY ID
|
Contribute to the working anarchy we fondly call the Internet
|
 |
snooper
Mod Builder Group
Joined: 23 April 2002
Location: Israel
Status: Offline
Points: 333
|
Posted: 12 February 2003 at 11:20am |
thanks for the reply.
i get this error:
You tried to execute a query that does not include the specified expression 'category' as part of an aggregate function.
|
 |
MorningZ
Senior Member
Joined: 06 May 2002
Location: United States
Status: Offline
Points: 1793
|
Posted: 12 February 2003 at 11:26am |
Oooops:
SELECT DISTINCT(category), ID FROM tbl GROUP BY ID, category
|
Contribute to the working anarchy we fondly call the Internet
|
 |
snooper
Mod Builder Group
Joined: 23 April 2002
Location: Israel
Status: Offline
Points: 333
|
Posted: 12 February 2003 at 11:54am |
ok, tried this, and at least i get no errors... but what i get now, is *all* the categories, sorted ASC - so the DISTINCT or ID havent done anything.
(i got this: a a b b c )
lets keep at this... we'll (you'll!) get it in the end :)
Edited by snooper
|
 |
michael
Moderator Group
Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4673
|
Posted: 12 February 2003 at 10:35pm |
What you are trying to do is impossible because you cannot sort by a column that is not in the aggregate or Group by clause. That means, which ID should SQL take as you are grouping by the category, the first, fifth .... ??? I hope you understand what I mean. You option is either to sort by the Category or put ID into an aggregate function like Select Sum(ID), Category from tbl Group By Category ORDER by ID
there is no real nead in using DISTINCT in what you are trying to do but basically with the table you have the only option would be
select DISTINCT(Category) from tbl ORDER BY CATEGORY
|
 |
snooper
Mod Builder Group
Joined: 23 April 2002
Location: Israel
Status: Offline
Points: 333
|
Posted: 13 February 2003 at 6:56am |
Thanks for the reply, michael. i kinda see what you're saying..
option 2 gives:
a b c (which is close, but no cigar)
option 1 gives the same error as before: You tried to execute a query that does not include the specified expression 'ID' as part of an aggregate function.
anyways, will keep at it.... THanks sn
|
 |
MorningZ
Senior Member
Joined: 06 May 2002
Location: United States
Status: Offline
Points: 1793
|
Posted: 13 February 2003 at 7:57am |
why dont you take a few minutes of your time and head over to SQL Team and/or W3 Schools and investigate this stuff yourself?
you're not advancing much as a programmer if you can't find these simple answers on your own
Edited by MorningZ
|
Contribute to the working anarchy we fondly call the Internet
|
 |