Home > Development > How to use Group By correctly

How to use Group By correctly


As you may not be aware I am not an SQL guru but I know enough to get by. A lot of the stuff I do with SQL is actually generated by an ORM most of the time. I know enough to make sure the SQL that is generated is not absolute rubbish and I can fine tune it in the ORM as required.

One thing I want to point out is that having multiple coulns in the group by clause like Name, Age, Address just because you get an error from SQL is not the correct way of doing things.

For a great read though grouping basics see the following great articles.

http://www.sqlteam.com/article/how-to-use-group-by-in-sql-server

http://www.sqlteam.com/article/how-to-use-group-by-with-distinct-aggregates-and-derived-tables

These are great articles everyone who is writing SQL should read.

Blair…

Advertisements
Categories: Development Tags: ,
  1. Daniel
    February 10, 2010 at 9:11 am

    We should do a quick profile of inner joins on subselects vs inner joins on temp tables when doing aggregation like this.

    I’ve got a feeling that the subselect will be called for each row of the outer table, query optimiser my recognise it correctly though.

    Either way has to be better than dealing with a massive amount of redundant data though

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: