0

I have 2 tables, Greeter_Detail and ZipCode_Details

I'm trying to SUM the total of People and Families from and Greeter_Detail and join them on ZipCodes, but I get duplicates. ZipCode_Details has city and county info. Can anyone help, I've been on this for hours. Seems like it should be straight forward. TIA!

 SELECT SUM(Families), SUM(People), Zipcode, ZipCode_Detail.Zipcode_D, ZipCode_Detail.City, ZipCode_Detail.County
 FROM Greeter_Detail
 
 LEFT JOIN ZipCode_Detail ON Greeter_Detail.Zipcode = ZipCode_Detail.Zipcode_D
 WHERE $dateRange
 GROUP BY Zipcode
asked Jan 4, 2024 at 0:45
3
  • Please provide SHOW CREATE TABLE. Commented Jan 4, 2024 at 1:43
  • It is unclear what you mean by "total of People". Can you provide a short set of data and the desired result? Commented Jan 4, 2024 at 1:49
  • Did you try to use SELECT DISTINCT OR use DISTINCT in one of the aggregate functions? I feel using the keyword DISTINCT is the key for your query and this question altogether. Let me know Commented Jan 4, 2024 at 2:48

1 Answer 1

0
COUNT(*) counts rows
COUNT(DISTINCT name) counts how many different `name` there are
SUM(cost > 1000) counts how many are expensive

Beware of what happens with NULL values; some counts include them; some don't.

If you need to gather two different counts from different tables, you may need subqueries for each count.

SELECT
 ( SELECT COUNT(*) ... ) AS foo_count
 ( SELECT COUNT(*) ... ) AS bar_count
answered Jan 4, 2024 at 1:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.