Gossamer Forum
Home : General : Databases and SQL :

SQL: distinct stuff...

Quote Reply
SQL: distinct stuff...
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

Last edited by:

snooper: Feb 12, 2003, 8:04 AM
Quote Reply
Re: [snooper] SQL: distinct stuff... In reply to
That is correct, you'd need to replace the ID with category in the ORDER BY function or add ID into the SELECT segment of your SQL Statement.

Example:

SELECT DISTINCT(Category), ID
FROM TABLE
ORDER BY ID, Category

OR

SELECT DISTINCT(Category), ID
FROM TABLE
ORDER BY CATEGORY

BTW: You don't need to use ASC since that is the default sorting order. You'd only need to use DESC if you want to sort your results in descending order.
========================================
Buh Bye!

Cheers,
Me

Last edited by:

Stealth: Feb 12, 2003, 10:33 AM
Quote Reply
Re: [Stealth] SQL: distinct stuff... In reply to
thanks for the reply.

neither gave me errors, which is a good sign, but i still havent got to the goal yet.

for option 1 i got:


b
b
a
a
c


which means that it sorting it by the ID, but not giving me the distinct.

for option 2 i got :


a
a
b
b
c


which means that the distinct hasnt work, and the order by ID either.


so we're getting there... opt 1 is closer so far!
any tweaks that you can think of?

thanks!

sn



EDIT: PS: SELECT DISTINCT(category) FROM tblSefer_Parsha GROUP BY category gives:

a
b
c


which is obviously giving me the Distinct, but not the order i want ...

Last edited by:

snooper: Feb 12, 2003, 11:20 AM