结合两个计数查询以获得最常见的查询

时间:2018-04-05 12:05:29

标签: sql

我有一张足球运动员评分系统表 -

列是

玩家ID - 数字得分 - 小数得分

可用值

1-20 - 1-5 - 0-9

例如,玩家将被评为4.9或3.2等等。

我有疑问:

awk

AND

SELECT NS, COUNT(NS)
FROM playerrating
GROUP BY NS 
ORDER BY NS DESC;

如何将两者合并以获得所有玩家的平均得分?感谢

2 个答案:

答案 0 :(得分:0)

我假设NS和DS是单个浮点数。

我假设DS的长度为1

我还假设列名为player_idNSDS,但您的问题并不清楚

然后每个玩家的查询看起来像:

SELECT player_id, AVG(NS + ( DS / 10.0 )) AS rating
FROM playerrating
GROUP BY player_id

对于您可以使用的所有玩家的平均值:

SELECT AVG(NS + ( DS / 10.0 )) AS rating
FROM playerrating

答案 1 :(得分:0)

我使用了自己的表名和列名。请根据需要更改它们。 下面是SQL代码:

SELECT Sum(avg1) FROM  
(SELECT * FROM  
(SELECT Sum(ns*nscnt)/Sum(nscnt) AS avg1 FROM   
(SELECT number_score AS ns, COUNT(number_score) AS nscnt  
FROM soccer_score  
GROUP BY number_score   
ORDER BY number_score DESC))  
UNION ALL  
(SELECT Sum(ds*dscnt)/Sum(dscnt) AS avg1 FROM  
(SELECT decimal_score AS ds, COUNT(decimal_score) AS dscnt  
FROM soccer_score  
GROUP BY  decimal_score   
ORDER BY decimal_score DESC)));