mysql - SQL COUNT if a specific value is found and output in the same line
问题描述
I need some help with the query below.
I have for example the following data set:
and I need to get the following output:
I tried with a query similar to this one:
SELECT id, ValA, count(1) FROM dual GROUP BY id, ValA;
but it is not working as expected. It's basically duplicating the values in the output:
Would you be able to help me?
解决方案
count(*)
计算所有行。count(ValA
) 计算非空值。这意味着count(*) - count(ValA)
计算空值。
SELECT
id, count(*) - count(ValA), count(ValA)
FROM dual
GROUP BY id;
推荐阅读
- hibernate - 如何使可嵌入类的 id 一起唯一的 spring data jpa 以便不能输入重复项
- azure-devops - 通过 REST 创建包含所有元数据(如 buildNumber)的 Azure Devops/Pipeline 版本
- python-3.x - 如何从带有复选框的下拉框中保存选定的输入?我的下拉框没问题,但复选框不起作用
- excel - 根据日期删除行
- python - 如何从 Python 中的 URL 下载 pdf 并将其转换为全文(用于数据集传递的 HTML/字典)?
- python - Ruby 相当于后台调度程序?
- python - 将 curl 请求转换为 python 代码时出现错误请求
- rust - 是否可以在项目范围内启用内部属性?
- php - PHP 8方法覆盖来自同一BaseClass的不同类型
- reactjs - 单击按钮时Reactjs调用另一个组件