【问题标题】:using the group clause and aggregate functions to operate on row data使用 group 子句和聚合函数对行数据进行操作
【发布时间】:2015-08-18 10:05:07
【问题描述】:

这是表tableA的sn-p:

station     code    persons    childLabour    DrinkingWater
staA         01     isaac      No              Yes
staB         05     Jane       Yes              Yes
staA         03     Jeff       No               No
staB         05     Saleh      No               Yes
staC         03     Henry      Yes              No
staA         01     Gera       No               No
staB         05     zoltan    Yes               Yes
staA         01     Allen      No               Yes
staC         04     Billy      No               Yes
staA         01     Jean       Yes              Yes

我正在尝试对该表制定一个查询,结果如下所示:
查询结果说明: 根据站点和代码,计算ChildLabour值=yes的人数并显示结果。还计算DrinkingWater 值为Yes 的人数并显示结果——每个站和相应的代码。

station       code     ChildLabour         DrinkingWater
staA           01       3                    4
staA           03       2                    1
staB           05       5                    10

所以,到目前为止,我的查询如下所示:

select distinct station,code, 
       (select count(persons) from `tableA` 
        where childLabour='Yes' 
        group by `station,code`) as `childLabour`
from `tableA` order by code `asc`;

但它会生成错误的结果,如下所示: 查询在整个列中显示相同的值(childLabour 值 = Yes 的所有人员的总数)。

station code ChildLabour  DrinkingWater
staA     01     3           4
staA     03     3           4
staB     05     3           4

我该如何解决这个问题,我相信这不是一个大问题!

【问题讨论】:

  • 我不明白结果集与数据集的关系。
  • 结果集:它是我想要得到的结果...

标签: mysql group-by aggregate-functions mysql-workbench


【解决方案1】:

可以尝试使用 SUM 并且只对有效值求和 - 等于是:

SELECT station, code, SUM(IF(childLabour = 'Yes', 1, 0)), SUM(IF(DrinkingWater = 'Yes', 1, 0))
FROM tableA
GROUP BY station, code

结果:

staA    01  1   3
staA    03  0   0
staB    05  2   3
staC    03  1   0
staC    04  0   1

解释:

station     code    persons    childLabour    DrinkingWater
staA         01     isaac      No               Yes
staA         01     Gera       No               No
staA         01     Allen      No               Yes
staA         01     Jean       Yes              Yes
                                1               3
staA         03     Jeff       No               No
                                0               0
staB         05     Saleh      No               Yes
staB         05     Jane       Yes              Yes
staB         05     zoltan     Yes              Yes
                                2               3
staC         03     Henry      Yes              Nn
                                1               0
staC         04     Billy      No               Yes
                                0               1

这些是结果以及我得到它们的原因。

SqlFiddle

【讨论】:

  • 它不起作用,查询在 ChildLabour 列中返回 0...但我需要 ChildLabour 值 = Yes 的人员的总和或计数!
  • 再看一下,根据您提供的数据、我得到的输出以及为什么 - 您在问题上提供的相同数据上没有得到相同的结果吗?
  • 查看查询:
  • 选择站,代码,SUM(IF(childLabour = 'Yes', 1, 0)) 从主要组按站,代码;如果 childLabour 为 Yes,则 if 返回 1。我需要 count/sum 函数来计算条件设置为 true 的人数....对于每个分组 [站和代码]。
  • --> 你的查询没有给我正确的结果! #ThanksAnyway
猜你喜欢
  • 1970-01-01
  • 2013-08-31
  • 2012-12-07
  • 2019-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
相关资源
最近更新 更多