Matt
Matt

Reputation: 4127

T-SQL aggregate function Logical error

Ok I have a data table containing duplicate Reciept numbers and a transaction value for each record, I need to simply list the total for each unique Reciept number, this is obviously a simple problem but I am missing something.

Any help is much appriciated

SELECT Gf_Receipt_number AS Reciept,
       SUM (Gf_Amount) AS Total
  FROM [TestP].[dbo].[Gf]
 WHERE Gf_Receipt_number IS NOT NULL
 GROUP BY Gf_Amount

Would probably help to mention the the error I am recieving is:

Column 'TestP.dbo.Gf.Gf_Receipt_number' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Upvotes: 2

Views: 235

Answers (3)

Kris.Mitchell
Kris.Mitchell

Reputation: 998

You would want put GF_Receipt_number in your group by to get the error to go away. Group By Refrence

Upvotes: 1

C.Evenhuis
C.Evenhuis

Reputation: 26446

You should probably GROUP BY Gf_Receipt_number instead.

Upvotes: 1

BlueMonkMN
BlueMonkMN

Reputation: 25601

You need to group by Gf_Receipt_number instead of Gf_Amount

The GROUP BY clause indicates what columns are being used to distinguish one group of records from another whereas the column used in aggregate functions (such as SUM(Gf_Amount)) determine how the other columns within those records are combined into a single value.

Upvotes: 7

Related Questions