Skip to content Skip to sidebar Skip to footer

Why Does My Query Involving Division And Count Always Result In 1?

I've simplified this down a bit since the literal data is pretty massive but a very simple example will suffice. I'm working on a query where because of the massive amount of data,

Solution 1:

You are trying to count distinct rows, but not using a count(distinct ...)

SELECTCOUNT(distinct c.id) as "Total Customers",
    COUNT(distinct p.id) as "Total Sales",
    COUNT(distinct c.id) *1.00/COUNT(distinct p.id) as "Sales per customer"
FROM test_customers c
    LEFTOUTERJOIN test_purchases p ON c.id = p.cid

Note, performance is not great

Post a Comment for "Why Does My Query Involving Division And Count Always Result In 1?"