tags:

views:

426

answers:

8

Why does SQL require that I specify on which attributes to group. Why can't it just use all non-aggregates. If an attribute is not aggregated and is not in the GROUP BY clause then nondeterministic choice would be the only option assuming tuples are unordered (mysql kind of does this) and that is a huge gotcha. AFAIK Postgresql requires that all attributes not appearing in the GROUP BY must be aggregated. This reinforces that it is superfluous. Am I missing something or is this a language design flaw that promotes loose implementations and makes queries harder to write? If I am missing something, what is an example query where group attributes can not be inferred?   

+2  A: 

If you issue just regarding to easier way to write scripts. Here is one tip:

In MS SQL MGMS write you query in text something like select * from my_table after that select text right click and "Design Query in Editor.." Sql studio will open new editor with filed up all fields after that again right click and select "Add Gruop BY" Sql MGM studio will add code for you .

I fund this method extremely useful for insert statements. When I need to write script for insert a lot of fields in table, I just do select * from table_where_want_to_insert and after that change type of select statement to insert,

adopilot
+1  A: 

The "superflous" attributes determine the ordering of the result.

Consider

create table gb (
  a number,
  b varchar(3),
  c varchar(3)
);

insert into gb values (   3, 'foo', 'foo');
insert into gb values (   1, 'foo', 'foo');
insert into gb values (   0, 'foo', 'foo');

insert into gb values (  20, 'foo', 'bar');
insert into gb values (  11, 'foo', 'bar');
insert into gb values (  13, 'foo', 'bar');

insert into gb values ( 170, 'bar', 'foo');
insert into gb values ( 144, 'bar', 'foo');
insert into gb values ( 130, 'bar', 'foo');

insert into gb values (2002, 'bar', 'bar');
insert into gb values (1111, 'bar', 'bar');
insert into gb values (1331, 'bar', 'bar');

This statement

select sum(a), b, c
  from gb
group by b, c;

results in

    44 foo bar
   444 bar foo
     4 foo foo
  4444 bar bar

while this one

select sum(a), b, c
  from gb
group by c, b;

results in

   444 bar foo
    44 foo bar
     4 foo foo
  4444 bar bar
René Nyffenegger
The only thing that determines ordering is an ORDER BY clause. If you don't explicitly state the desired order the data should be returned, the database can order it however is most convenient to it. This will vary from database to database. Without an ORDER BY clause, any appearance of order is entirely coincidental.
Chris Latta
Yes, thats right. I should have written: influences
René Nyffenegger
+1  A: 

This thread provides some useful explanations.

http://social.msdn.microsoft.com/Forums/en/transactsql/thread/52482614-bfc8-47db-b1b6-deec7363bd1a

James Wiseman
A: 

I'd say it is more likely to be a language design choice that decisions be explicit, not implicit. For instance, what if I wish to group the data in a different order than that in which I output the columns? Or if I want to group by columns that aren't included in the columns selected? Or if I want to output grouped columns only and not use aggregate functions? Only by explicitly stating my preferences in the group by clause are my intentions clear.

You also have to remember that SQL is a very old language (1970). Look at how Linq flipped everything around in order to make Intellisense work - it looks obvious to us now, but SQL predates IDEs and so couldn't have taken into account such issues.

Chris Latta
+8  A: 
John Ormerod
Of course. All non-aggregate attributes in the SELECT must appear in the GROUP BY but not all attributes in the GROUP BY must appear in the SELECT. I feel dumb for not checking the converse. But there is still a case for making GROUP BY optional in the presence of a mix of aggregate and non-aggregate SELECT columns, a common use case. I really hate the mysql behavior of just randomly choosing a column when the SELECT attribute isn't accounted for in the GROUP BY
Samuel Danielson
Er, I mean when mysql just chooses an arbitrary tuple.
Samuel Danielson
A: 

I Agree

I quite agree with the question. I asked the same one here.

I honestly think it's a language flaw.

I realise that there are arguments against that, but I have yet to use a GROUP BY clause containing anything other than all the non-aggregated fields from the SELECT clause in the real world.

AJ
A: 

I would say if you have a large number of items in the group by clause then perhaps the core info should be pulled out into a tabular sub-query which you inner join into.

There is a probably a performance hit, but it makes for neater code.

select  id, count(a), b, c, d
from    table
group by
        id, b, c, d

becomes

select  id, myCount, b, c, d
from    table t
        inner join (
            select id, count(*) as myCount
            from table
            group by id
        ) as myCountTable on myCountTable.id = t.id

That said, I'm interested to hear counter-arguments for doing this as opposed to having a large group by clause.

Antony Koch
+2  A: 

I agree its verbose that the group by list shouldn't implicitly be the same as then non-aggregated select columns. In Sas there are data aggregation operations that are more succinct.

Also : it's hard to come up with an example where it would be useful to have a longer list of columns in the group list than the select list. The best I can come up with is ...

create table people
(  Nam char(10)
  ,Adr char(10)
)

insert into people values ('Peter', 'Tibet')
insert into people values ('Peter', 'OZ')
insert into people values ('Peter', 'OZ')

insert into people values ('Joe', 'NY')
insert into people values ('Joe', 'Texas')
insert into people values ('Joe', 'France')

-- Give me people where there is a duplicate address record

select * from people where nam in 
(
select nam              
from People        
group by nam, adr        -- group list different from select list
having count(*) > 1
)
cindi