How to use Group By statement

Iam trying to use group by statement but it gives me this error :- Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

But I need to group them accordind to their Feeid.

select st.ID,af.Fee_Head_Name,SUM(Amount) as tamt from admission af join 
student st on af.ID= FeeID group by FeeID

Without SUM(Amount)(means simply Amount) and Group by statement iam getting output like this:

Fee_Head_Name | Amount
Admission     | 5000
Tution        | 2500
Games         | 1500
Tution        | 2500
Games         | 1500

But my desired must look Like this :-

Fee_Head_Name | Amount
Admission     | 5000
Tution        | 5000
Games         | 3000

Please help me on this iam stuck here for so long and used almost everything but nothing work

Answers


Try like this

SELECT Fee_Head_Name , SUM(Amount)

FROM TABLE1

GROUP BY Fee_Head_Name 

GROUP BY

EDIT

IF you want to select ID also then use this

 SELECT MIN(ID),Fee_Head_Name , SUM(Amount)

 FROM TABLE1

 GROUP BY Fee_Head_Name 

select af.Fee_Head_Name,SUM(Amount) as tamt from admission af join 
student st on af.ID= FeeID group by af.Fee_Head_Name

group by with column in your select statement


select af.Fee_Head_Name,SUM(Amount) as tamt from admission af join 
student st on af.ID= FeeID GROUP BY Fee_Head_Name

You should use the above query . Group By is used in conjunction with the aggregate functions to group the result-set


Try this

SELECT Fee_Head_Name,
       SUM(Amount) 
FROM admission 
GROUP BY Fee_Head_Name

See this link for more details http://www.w3schools.com/sql/sql_groupby.asp


Need Your Help

How to get the privacy values with FQL query from stream table entries using a Facebook application?

facebook facebook-graph-api facebook-fql privacy

I would like to know the privacy values (CUSTOM, ALL_FRIENDS, etc.) of the stream entries from a Facebook application, but the result does not contain privacy field values for all of the returned e...

Ampersand in JSON/PHP in POST

php jquery json post escaping

I'm having a text field which is send via JSON and jQuery (wrapped with .toJSON function) to my server via AJAX and POST request. On PHP side I'm doing json_decode .