Results 1 to 8 of 8

Thread: Why does SQL server require this ???

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    113

    Why does SQL server require this ???

    I am SELECTing several fields and want to group by 3 of the fields. However it requires me to include all fields in the group by clause.

    Why is this ??? Why can't you select a couple fields to group by like in crystal reports ?

    Am I doing something wrong ?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Why does SQL server require this ???

    Are you doing an aggregates in your query?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    113

    Re: Why does SQL server require this ???

    Yes.... I am doing a SUM on a field called dozens. And want it to group on all styles/sizes/colors that are alike. But it is requiring me to group by other fields as well... Fields I don't want to group by.

  4. #4
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: Why does SQL server require this ???

    How would the query know what to do a SUM for if you don't tell it what the grouping qualifiers are? Otherwise it is just a line item report not a summary.

    Edit: Well if there are fields you don't want to GROUP By then they need some kind of Aggregate function Average/Max/Min etc.. everything in the GROUP BY statement is something not given an aggregate function. And this is true for I think all databases that use ANSI Standard SQL.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Why does SQL server require this ???

    When you do any type of aggregate functions, all fields in your SELECT statement NOT in an aggregate function, must be included in a GROUP BY clause.

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Why does SQL server require this ???

    Why can't you select a couple fields to group by like in crystal reports ?
    Crystal is either generating the SQL Statement with all fields in the Group By clause or it is processing the grouping internally.

    To see the sql statement that Crystal is executing use DataBase -> Show SQL Query menu item.

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Why does SQL server require this ???

    When you use a SUM function on a particular column, it is required that all other columns be part of the "grouping" determination or also be in an aggregate function.

    That's a basic concept that cannot be gotten around - not a requirement at all - a basic foundation of what GROUP BY is doing.

    Select FRUIT, COLOR, SUM(PRICE) From SomeTable Group By FRUIT

    Of course that cannot work - you have RED and YELLOW apples - what is the SUM(PRICE) going to refer to??

    There are ways to use SUB-QUERIES to get around issues like this, but in effect the SUB-QUERY can only return one row anyway - so it's basically an AGGREGATE function in and of itself.

    There are also ways to build temporary tables (or table variables if you are in MS SQL SERVER) that can help you build up your final resultset with your desired results.

    Give more details of the columns in your table and query and a more detailed explanation of what you desired result is...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Why does SQL server require this ???

    It's not really anything to do with aggregate functions. If you think about it, if you have a field in the select statement that isn't in the group by clause it might (and probably would) have several values in it.

    Using Szlamany's fruit example, if your SQL statement was select fruit, colour from myTable group by fruit and you have red, green and yellow apples, which value, of the three possible ones, should be returned in the result set for the apple row. The answer is inexact and can't be expressed.

    Aggregate functions don't need to be in the group by list because they express a value that applies across the whole of each group, so they can be expressed as a single value for each row.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width