views:

55

answers:

3

Consider this query:

SELECT F1,F2 FROM TABLE GROUP BY F1

Selecting F1 is valid, but F2 seems to be incorrect (after all it can change from row to row). However SQL Server does not check any logic involved here -- for example F2 could be dependent of F1 (because of the JOIN clause, for example).

I know the workarounds, but my question here is:

How to RELAX this "group by" restriction (directly)?

Something like:

RELAX_GROUPBY
SELECT F1,F2 ....

begin of edit 1

So it would be something similar to MySQL ability to get data without any workarounds from groupped dataset.

Example of data:

F1 | F2
1  | 2
1  | 2

Output (after executing the query given above):

F1 | F2
1  | 2

end of edit 1

Remark: yes, I do know the workarounds -- aggregate functions, creating view, table on-fly, and others (depending on scenario). I am not interested in another workaround. If you know the solution to the question please answer, thank you very much.

A: 

if you use group by you should use a Aggregate function then only it will work

e.g:

SELECT F1,count(F2) FROM TABLE GROUP BY F1
anishmarokey
I EXPLICITLY wrote that I know workarounds (there are many of them). Please stick to the question, thank you.
macias
+1  A: 

Assuming F2 is the same for every F1 (which is where your query is relevant), the easiest way is to do something like

SELECT F1, MAX(F2) AS F2
FROM TABLE
GROUP BY F1

assuming F2 is a field that can have aggregate functions applied to it, of course.

There's no way to relax the GROUP BY in the way you describe, short of rewriting the whole thing. I know MySQL does something a bit different (you can group by one field and SELECT all the others), but it's inconsistent with other implementations.

CodeByMoonlight
Thank you for the second part -- I am looking for something like this. Or, some function like FIRST (or LAST) which would indicate that for that column __I__ know that the data are the same (after grouping) so it is ok, to get any element.
macias
A: 

if you are so sure that F2 is dependent on F1, just add it to the group by (how difficult is that?):

SELECT F1,F2 FROM TABLE GROUP BY F1, F2

The "do what I mean and not what I code" portion of SQL Server will never be good enough to read your mind, tell it how to group the columns and it will do it. There is no facility within SQL Server to "relax" the group by restrictions, and I'm glad.

KM
I am not saying MS-SQL should read my mind, simply get any element **it** wants in case of relaxed grouping. MySQL can do this, so it is technically possible and it is not a challenge in terms of implementing this.
macias
@macias, I'm familiar with MySql's GROUP BY, and I've always thought that it works in a strange way. I'm glad TSQL is more "strict". However, I like to actually declare variables types and sizes where many people like the dynamic typing. I think that being strict and clear results in less problems down the road.
KM
I also opt for being precise (static typing and alike) -- in MySQL you can turn on the strict grouping and I do it all the time. It is not my fault that MS-SQL is so lousy it cannot figure out that inner join clause does not add new values (not mentioning it lacks USING). I have to deal it, like it or not, and using aggregate workaround is actually the worst thing to do, because it hides your intention. Better would be using table on fly (I use it) or relaxed grouping, because it is even less typing and your intentions are on platter.
macias
cry me a river, if I had a nickle for every `database X is lousy because it lacks feature Y` I'd be rich. Ya, I have to agree, SQL Server is lousy because you have to type a little more than you want to. If TSQL only it had a USING, it would be perfect!
KM
It's not just typing -- expressing the logic directly means more reliable software (less bugs), but I agree on USING and "db X is lousy..." issue :-)
macias
your selected answer certainly doesn't express the logic very clearly. how does wrapping MAX(F2) make it more clear than adding F2 to the GROUP BY??
KM