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
3
  • Please provide SHOW CREATE TABLE.
    – Rick James
    Commented Jan 4 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?
    – Rick James
    Commented Jan 4 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
    – Full Array
    Commented Jan 4 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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.