I have a Vote-scoring system, each user can score any product each day (maximum of 10 points a day, but they can go on the same product each day).
The schema for my vote table is like so:
Vote: ID, user_id, product_id, score, date.
What I’d like to do is not only fetch the total score and amount of individual votes, (so I can work out an average) but also get the unique amount of voters (DISTINCT user_id’s) in the current time frame (in this example, a month). The current query I have is:
SELECT
SUM(`Vote`.`score`) AS `score`,
COUNT(*) AS `votes`,
CONCAT(YEAR(`Vote`.`date`), '-', MONTH(Vote.date)) AS `month`
FROM
`votes` AS `Vote`
WHERE
`product_id` = 4
GROUP BY
month
ORDER BY `Vote`.`date` DESC
Thanks in advance.
Use
COUNT(DISTINCT user_id)in yourSELECTlist.You can also have
AVG(score)calculated.