简体   繁体   中英

Group By not aggregating the group by column in SQL

I'm trying to get a count of users who have paid for a service and signed up in a certain year from a mysql db. A member can pay for multiple services so can be counted twice:

select 
    count(payment_order.memberId) as members,
    from_unixtime(account_login.memberFrom, '%Y') as memberFrom
from 
    payment_order
join 
    account_login 
on 
    payment_order.memberId = account_login.memberId
Where 
    account_login.memberFrom != '0'
    and payment_order.`status` = 'paid'
    and payment_order.dateCompleted >= '2010-01-01 00:00:00'
    and payment_order.dateCompleted <= '2011-12-31 23:59:59'
group by memberFrom

Rather than grouping by years, it seems to be grouping by the individual member count. I think I'm doing something and can;t see the wood for the trees:

1, 2005
4, 2005
1, 2005
1, 2006
5, 2006
5, 2006

What I'm looking for is

 6, 2005
11, 2006

Grateful for any pointers that may explain my befuddlement

You're grouping by account_login.memberFrom , which creates each individual row.

Then you're selecting from_unixtime(account_login.memberFrom, '%Y') on each of those rows, creating the duplicate values.

Instead, use GROUP BY from_unixtime(account_login.memberFrom, '%Y')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM