SQL Project Query #7

Questions ArchiveCategory: DatabaseSQL Project Query #7
Colby Jensen asked 2 years ago

On question #7 I’m getting these errors:

  • Field of aggregated query neither grouped nor aggregated: line 1, column 8
  • Field of aggregated query neither grouped nor aggregated: line 1, column 20

My line is as follows

SELECT sfirstname, slastname, COUNT(encid) AS encounter_count
FROM creditrating
INNER JOIN customer ON creditrating.creditid = customer.creditid
INNER JOIN encounter ON customer.customerid = encounter.customerid
INNER JOIN salesperson ON encounter.salesid = salesperson.salesid
WHERE annualincome < 25000
OR (creditdescription = ‘Very Poor’ OR creditdescription = ‘Extremely Poor’)
ORDER BY slastname DESC

 
I am having trouble finding my error.  Is it what I am counting?  Is it in my keys?

1 Answers
Dallin Blanc answered 2 years ago

I ran into the exact same error and from what I could find out online, the error comes from the fact that (quoted from the answer I found) “as soon as you start to use Aggregate functions like SUM() [or COUNT()] you have to specify what happens to all of the columns in the query.” So as far as I can tell, what fixes this is that you have to add a GROUP BY line right above the ORDER BY and specify every field from the SELECT statement that is not being counted in the GROUP BY statement as well. Hope this helps.

Tyler Josse replied 2 years ago

I ran into the same error and this helped! Thank you!

Your Answer

7 + 4 =

css.php