SQL求和和平均查询

时间:2014-12-13 17:07:52

标签: sql sum distinct

我试图弄清楚如何做一些更高级的SQL功能(至少对我来说是先进的)。

我想采取以下数据:

  • 仅使用第1轮和第2轮的数据
  • 总结每个名字的分数
  • 显示每个不同名称的第1轮和第2轮的汇总数据

这是我制作的快速示例数据表:

Table: tblPoints

ID   NAME   ROUND  POINTS
---------------------------
1    Dummy    1      30
2    Yes      1      45
3    Hi       1      20
4    Dummy    2      45
5    Yes      2      15
6    Hi       2      65
7    Newbie   2      10
8    Dummy    3      150
9    Yes      3      120
10   Hi       3      145

这是我希望看到的(再次注意它仅适用于第1轮和第2轮):

NAME   SUM_POINTS_EVERYONE  SUM_POINTS   COUNT_NAMES
------------------------------------------------------
Dummy        230               75            4
Yes          230               60            4
Hi           230               85            4
Newbie       230               10            4

我从这开始只选择我想要的回合并得到2个新列:

SELECT name,
       (SELECT SUM(points) FROM tblPoints WHERE round IN (1, 2)) AS sum_points_everyone,
       (SELECT COUNT(DISTINCT name) FROM tblPoints WHERE ROUND IN (1, 2)) AS count_names
FROM tblPoints
WHERE round IN (1, 2);

自定义行的公式如下:

总分 =总结第1轮和第2轮(在这种情况下)玩家名称的分数

每个人总和 =总结第1轮和第2轮的每个点(在这种情况下)

计数名称 =所有唯一名称的COUNT()


正如您所看到的,我可以获取行SUM_POINTS_EVERYONE和COUNT_NAMES,但我从这里开始就如何获取其他行。如何仅使用上述字段输出不同名称的行?

注意:如果这超出了SQL的范围,我可以用PHP手动完成 - 但我试图看看我是否可以在一个查询中执行此操作,因此我不会这样做。必须将工作转移到PHP。


如果有人想快速制作表格,请点击这里:

CREATE DATABASE IF NOT EXISTS `testdb`;
USE `testdb`;
CREATE TABLE `tblPoints` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  `round` int(10) unsigned NOT NULL,
  `points` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
INSERT INTO `tblPoints` VALUES (1,'Dummy',1,30),(2,'Yes',1,45),(3,'Hi',1,20),(4,'Dummy',2,45),(5,'Yes',2,15),(6,'Hi',2,65),(7,'Newbie',2,10),(8,'Dummy',3,50),(9,'Yes',3,20),(10,'Hi',3,45);

1 个答案:

答案 0 :(得分:1)

SELECT NAME,
(SELECT SUM(POINTS) FROM tblPoints WHERE ROUND IN (1,2)) AS SUM_POINTS_EVERYONE,
SUM(POINTS) AS SUM_POINTS,
COUNT(1) AS COUNT_NAMES 
FROM tblPoints 
WHERE ROUND IN (1,2)
GROUP BY NAME 

<强>输出

|   NAME | SUM_POINTS_EVERYONE | SUM_POINTS | COUNT_NAMES |
|--------|---------------------|------------|-------------|
|  Dummy |                 230 |         75 |           2 |
|     Hi |                 230 |         85 |           2 |
| Newbie |                 230 |         10 |           1 |
|    Yes |                 230 |         60 |           2 |

<强> Fiddle

编辑名称计数

SELECT NAME,
(SELECT SUM(POINTS) FROM tblPoints WHERE ROUND IN (1,2)) AS SUM_POINTS_EVERYONE,
SUM(POINTS) AS SUM_POINTS,
(SELECT COUNT(DISTINCT NAME) FROM tblPoints WHERE ROUND IN (1,2)) AS COUNT_NAMES
FROM tblPoints
WHERE ROUND IN (1,2)
GROUP BY NAME 

<强>输出

|   NAME | SUM_POINTS_EVERYONE | SUM_POINTS | COUNT_NAMES |
|--------|---------------------|------------|-------------|
|  Dummy |                 230 |         75 |           4 |
|     Hi |                 230 |         85 |           4 |
| Newbie |                 230 |         10 |           4 |
|    Yes |                 230 |         60 |           4 |

<强> Fiddle