|
-
Dec 13th, 2013, 04:44 AM
#10
Re: DataSet SQL GROUP By statement not working
Fedaykin, JM is doing you a favout by asking for it to be moved (and Si is doing you one by moving it). You're going to get much better answers to your question in the DB forum than you are in the VB.Net one.
The reason all columns in the select list must be part of an aggregate function or be included in the group by is because otherwise SQLServer would not be able to resolve a single value for them when more than one possible value was presented. Take the following set of Fruit data:-
Code:
FruitID FruitType Colour
1 Apple Green
2 Apple Yellow
3 Apple Red
4 Apple Green
5 Banana Yellow
If you were to write the following query:-
Code:
Select FruitType, Colour
From Fruit
Group by FruitType
What would the result be? For a banana the result is obvious, it's banana, yellow because that's the only possible answer from that data set. But what about apples? What value should be returned as the colour? You only want one row for Apples (because you specified that you wanted to group by Fruit Type) and you haven't given the database a mechanism for deciding which colour to return. For the query to make sense you would either have to group by both Fruit type and Colour (in which case you would get all three colours returned for Apples each returned as a separate row) or you would have to put colour in an aggregate function, e.g. Reddest (in which case you would get Red, because that's the reddest).
When you've got a load of rows where all the value are identical as you described the correct thing to do is either put them in an arbitrary aggregate function like Min or include them in the group by. An arbitrary aggregate fuction works because it will return the same value for all rows if they're all the same (e.g. the reddest of red, red and red is... well... red) so it can be resolved to a single value. Putting them all in the group by is probably better though because it's explicitely collapsing the rows.
As for why your query isn't eliminating the duplicates, it is. That's what Group By does. Which means what you're seeing aren't duplicates. I suggest you look very closely at two rows that appear to be duplicates because somewhere in tere there is a deffierence.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|