Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Question

Status
Not open for further replies.

ivalum21

MIS
Jul 14, 2004
63
US
I need to run a query that will take the average balances of customers by city. So for example, I would need the average balance for all customers from Chicago. So I would display one row for Chicago with the average balance of all customers from Chicago.

I try using the DISTINCT function along with the AVG function, but I get a weird error.

Someone help!!
 
probably forgot the GROUP BY clause:
Code:
select city
     , avg(balance) as avgbal
  from yourtable
group
    by city
if you specify the city in the WHERE clause, then you can leave it out of the SELECT list, and therefore you can omit the GROUP BY:
Code:
select avg(balance) as avgbal
  from yourtable
 where city='Chicago'
in other words, you can omit the GROUP BY only if there are no non-aggregates (columns) in the SELECT list

stated another way, you can omit the GROUP BY only if the SELECT list contains only aggregates

rudy
SQL Consulting
 
if that doesn't help be sure to include your SQL and error so we can better help. You didn't give us much to go on.

Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
Here is my SQL right now:

Code:
SELECT CustCity, AVG(CustBal) AS AvgBal, left(CustZip, 5) AS ZipCode
FROM Customer
WHERE CustState = 'WA'
GROUP BY CustCity

It works fine when i take out the ZipCode part in my SELECT statement...how can i get it to work with the ZipCode part?
 
you get it to work with the zipcode part -- which is a non-aggregate -- by including it in the SELECT list
Code:
SELECT CustCity
     , AVG(CustBal) AS AvgBal
     , left(CustZip, 5) AS ZipCode
  FROM Customer
 WHERE CustState = 'WA'
GROUP 
    BY CustCity
     , left(CustZip, 5)

rudy
SQL Consulting
 
Add it to your GROUP BY clause.



Hope This Helps!

Ecobb

"My work is a game, a very serious game." - M.C. Escher
 
Okay...that worked, thanks a lot!

But I was trying this for the fun of it, what if i needed to add in that the AvgBal needed to be less than 100? I tried this:

Code:
SELECT CustCity, AVG(CustBal) AS AvgBal, left(CustZip, 5) AS ZipCode
FROM Customer
WHERE CustState = 'WA'
AND AvgBal < 100
GROUP BY CustCity, left(CustZip, 5)

But when i go to run it, it gives me a pop up box that wants me to input a value for AvgBal?? I don't want that...
 
you want HAVING, not WHERE
Code:
SELECT CustCity
     , AVG(CustBal) AS AvgBal
     , left(CustZip, 5) AS ZipCode
  FROM Customer
 WHERE CustState = 'WA'
GROUP 
    BY CustCity
     , left(CustZip, 5) 
having AVG(CustBal) < 100

rudy
SQL Consulting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top