Click here to Skip to main content
16,016,643 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In table records as follows(Table Name Tb_Presea_Faculty_Feedback)

Feedback Facid FacQ1 FacQ2 FacQ3 FacQ4 FacQ5

1 1 5 4 3 2 2
1 2 2 3 4 5 2
1 3 3 4 5 2 3

Query as follows

select Feedbackid,sum(b.FacQ1) /3.0 as Q1,sum(b.FacQ2)/3.0 as Q2,sum(b.FacQ3)/3.0 as Q3,sum(b.FacQ4) /3.0 as Q4,sum(b.FacQ5) /3.0 as Q5 from Tb_Presea_Faculty_Feedback as b where b.Feedbackid = '1' group by Feedbackid

When i run above query output as follows

Feedbackid Q1 Q2 Q3 Q4 Q5

1 3.33 3.66 4.00 3.00 2.33

in Table records as follows(Table Name Tb_Presea_Faculty_Master)

Facid Facname
1 Gopi
2 Ram
3 Suresh


i want to display the Facname from the database. in the above query i want output as follows.

Feedbackid Facname Q1 Q2 Q3 Q4 Q5

1 Gopi 3.33 3.66 4.00 3.00 2.33


For getting a above output. i written a query as follows,

select a.Facname,b.Feedbackid,sum(b.FacQ1) /3.0 as Q1,sum(b.FacQ2)/3.0 as Q2,sum(b.FacQ3)/3.0 as Q3,sum(b.FacQ4) /3.0 as Q4,sum(b.FacQ5) /3.0 as Q5 from Tb_Presea_Faculty_Feedback as b,Tb_Presea_Faculty_Master as a where b.Feedbackid = '1' and a.Facid=b.Facid group by Feedbackid


But i execute a above query shows error as follows,

Column 'Tb_Presea_Faculty_Master.FacName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.


please help me. what is the problem in my above code.
Posted

1 solution

Whenever you are doing an aggregate query in sql all columns included in the select statement must either be grouped by, or aggregated, i.e. sum() or count() etc.

So to solve the issue either remove a.Facname from your select list, include it in your group by clause, or aggregate on it (e.g. count(a.facName)

Upon closer inspection of your issue, it appears that you need to remove feedbackId from your select list and group by clause, and add facname to your group by clause, this would give your sums for each facname value.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900