SQL DISTINCT stuff
Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Database Discussion
Forum Description: Discussion and chat on database related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=101
Printed Date: 28 March 2026 at 9:26am Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com
Topic: SQL DISTINCT stuff
Posted By: snooper
Subject: SQL DISTINCT stuff
Date 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
|
Replies:
Posted By: MorningZ
Date 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
|
Posted By: snooper
Date 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.
|
Posted By: MorningZ
Date 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
|
Posted By: snooper
Date 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 :)
|
Posted By: michael
Date 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
|
Posted By: snooper
Date 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
|
Posted By: MorningZ
Date Posted: 13 February 2003 at 7:57am
why dont you take a few minutes of your time and head over to http://www.sqlteam.com - SQL Team and/or http://www.w3schools.com/sql/default.asp - 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
------------- Contribute to the working anarchy we fondly call the Internet
|
Posted By: snooper
Date Posted: 13 February 2003 at 8:08am
|
well hello Morningz...
if its so simple, how come you didnt manage to help me with the correct answer in your first 2 messages??
and FYI, been there and done that. neither of these site addresses this issue.
|
Posted By: MorningZ
Date Posted: 13 February 2003 at 8:21am
ohh, i see now...
i "owe it to you" to do your work for you.. ha.... don't think so.. i was simply giving you some links to help yourself out and find the answer instead of having to have your hand held.....
take it or leave it, but your snotty reply like that, you just lost all future help and respect from me
------------- Contribute to the working anarchy we fondly call the Internet
|
Posted By: snooper
Date Posted: 13 February 2003 at 8:40am
|
"snotty" - i dont think so...
but if this is what you call "help" - i'll leave it for sure.
This forum is not meant to help people, as i have been helped, and have tried to help, many times before. this is not a "take a hike, you didnt do your homework" style forum, its meant to help people out.
if you feel you dont want to bother helping, or that you want to guise your inablility to help as something else - then please dont post answers. if you have a direct link to a helpful resource, then by all means, post it. but dont lecture people. with a reply like that - i would have prefered to get no replies at all....
there are many other fine folks here on the forum that actually WANT to help, so go find someone else to lecture to and be condescending to.
(..and if you still think this is snotty - go look it up..)
|
Posted By: jaz9090
Date Posted: 13 February 2003 at 9:08am
|
Instead of telling people that they are no-good programmers, and calling them snotty just try and help or just dont comment at all
|
Posted By: MorningZ
Date Posted: 13 February 2003 at 9:33am
jaz9090 wrote:
Instead of telling people that they are no-good programmers |
i'm at a loss where i called anyone a bad programmer? I simply stated (and even provided links) to where he could find the answers out at... is there something wrong with that? (i am guessing by the negavtive feedback that there is)
as for the "snotty" remark by me, it was in reference that this guy obviously just wants someone to do it for him, judging by the line:
"lets keep at this... we'll (you'll!) get it in the end :)"
and then i read whining because two people posted code to try to help and "waaah, it doesn't work"
maybe others see this thing differently, but i don't see any effort on his part to learn what the problem was and help himself out... we've all been down this road before of not knowing how to do something.. good programmers, as opposed to "bad", can do some research and find out how to do it.....
------------- Contribute to the working anarchy we fondly call the Internet
|
Posted By: Bunce
Date Posted: 13 February 2003 at 10:25am
|
No, you're right Morningz.
Snooper, those sights are excellent references to help you learn the basics of set theory (SQL Statements).
Once you understand this, you will understand that what you are trying to do, can't be done this way.
That said, IF the ID is an incrementing key and you want to use the minimum ID of a group of categories, to order the records, then you can use something like this:
SELECT Category, Min(ID) AS MinOfID FROM tbl GROUP BY Category ORDER BY Min(ID);
Cheers, Andrew
------------- There have been many, many posts made throughout the world...
This was one of them.
|
Posted By: michael
Date Posted: 13 February 2003 at 11:36am
|
The first option I gave, why is it not a cigar, it is the output you are looking for just not sorted by ID which is not possible in this type of query. The second option DOES work unless you pasted ID in there. The error message you wrote suggests that you have the field ID in there which you can't unless you put it into an aggregate function like Bunce said. Nevertheless it does not make sense to use Min function as this is a fake sorting, why would you sort by ID anyway?
|
Posted By: snooper
Date Posted: 13 February 2003 at 1:39pm
|
to michael: i want to sort by the ID, because i want the records displayed in the entry order, not by the ABC. ie i want: b>a>c and not a>b>c
i am still not sure WHY this is not do-able, but i ACCEPT it.
to Andrew: thanks! Your suggestion worked exactly as i wanted! :)
thanks to all the helpful folks who answered, and thats to Andrew for the winning answer!
Snooper
|
Posted By: Bunce
Date Posted: 13 February 2003 at 5:54pm
|
But you see its not necessarily in entry order.
What you are getting is the lowest ID for EACH group of categories, which in this case may be the earliest entry for each category.
You have to remember that once you GROUP by a value then you are summarising all records for that group into a SINGLE record. So by grouping by Category, you need to work out what you want to do with any other fields you wish to summarise.
In this case weve taken a MINIMUM of all the ID's for each group, and sorted by this.
Personally, I would be adding a 'Date Created' field and using this as it's more meaningful that using the ID.
Hope this makes it a little clearer.
Cheers, Andrew
------------- There have been many, many posts made throughout the world...
This was one of them.
|
Posted By: snooper
Date Posted: 13 February 2003 at 6:22pm
|
ok, i do get it now. thanks for the clear explanation, and again for the solution
:) sn
|
Posted By: jaz9090
Date Posted: 14 February 2003 at 6:19am
MorningZ wrote:
you're not advancing much as a programmer | You might as well have been calling him a bad programmer
|
Posted By: jaz9090
Date Posted: 14 February 2003 at 6:20am
|