简体   繁体   English

sql 查询计数不同

[英]sql query to count distinct

candidate候选人 position position
john约翰 referendum公投
mark标记 referendum公投
sofia苏菲亚 premier总理
john约翰 referendum公投
john约翰 referendum公投
sofia苏菲亚 premier总理
mark标记 referendum公投
sofia苏菲亚 premier总理
anna安娜 premier总理
john约翰 referendum公投

hi guys, T need help with this query to count the results, the output that I what will be:嗨,伙计们,T 需要此查询的帮助来计算结果,我将是什么 output:

john, for the referdum, has 4 votes
mark, for the referdum has 2 votes
sofia, for the premier has 3 votes
anna, for the premier has 1 votes
SELECT DISTINCT 
    candidate, 
    position, 
    count(DISTINCT candidate) over (order by position) AS votes_received 
from votes; 

this was my query, but says:这是我的查询,但说:

This version of MariaDB does not yet support 'COUNT (DISTINCT) aggregate as window function此版本的 MariaDB 尚不支持 'COUNT (DISTINCT) 聚合为 window function

thanks for help感谢帮助

I think you need to look at GROUP BY , something like:我认为您需要查看GROUP BY ,例如:

SELECT COUNT(*) AS `total`, `candidate`, `position`
FROM `votes`
GROUP BY `candidate`, `position`

(back ticks, just in case...) (反勾号,以防万一……)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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