Results 1 to 3 of 3

Thread: I have problems in SQL server 2012 a query GROUP BY

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    134

    I have problems in SQL server 2012 a query GROUP BY

    I use this query to load som data from database:

    Code:
    SELECT Code, Article, Price, Sum(Quantity) AS Tquan, Sum(Total) AS Ttotal, TaxID, Tax, IND, INDX, Emp, Table, Salla, Prodh, Desc FROM RAM1 Where Emp=@Emp AND Table=@Table Group By Code
    "So i want to Group only by Code.I have used this query in many databases like Acces,Sqllite but this hapens only in SQL server.
    and i get this error:
    "Msg 8120, Level 16, State 1, Line 3
    Column 'RAM1.Article' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."

  2. #2
    Hyperactive Member
    Join Date
    Dec 2000
    Location
    Sydney
    Posts
    311

    Re: I have problems in SQL server 2012 a query GROUP BY

    You can't Group By one column when you have SUM() of other columns.

    Rather than me explain it here, there's a simple example given in the first answer here: http://stackoverflow.com/questions/1...contained-in-e

    You'll need to GROUP BY all none SUMMED columns, or MAX, MIN etc. Depends on your purpose and data.
    Why do today what you can tomorrow...

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,953

    Re: I have problems in SQL server 2012 a query GROUP BY

    Help got it mostly right. When you perform a group by all your columns must either be part of an aggregation, or part of the group by. If you need fields that are neither grouped nor part of an aggregation, then you have to add that back in after the fact. How you do it varies depending on your needs.

    -tg\
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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