Referencing other columns in queries using group by

  • Good morning,

    SQL standards state that the SELECT must contain items that are in the GROUP BY list.

    I need to add an additional column (and ID field) to the SELECT without utilizing an aggregate function or adding the column to the GROUP BY.

    Does anyone have an idea on how to accomplish this?

    Thanks!

  • Hi,

    Given that it is an ID field there shouldn't be any reason why you cannot (or shouldn't) have it in the GROUP BY clause.

    Essentially though, it's not possible.  If you are aggregating data then you must either aggregate it, group it, or not include it in your result list.  It doesn't make sense to aggregate without a grouping.

  • Why do you want both groupings and an ID field?  Perhaps there is a way to do a join or a sub-query if we knew why you wanted both groups (inherently multiple rows summed together based upon a field) and an ID (normally only one per one row which nullifies grouping).

    Steve

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply