sql - 数据字段中每个不同值的计数
问题描述
我正在寻找一个查询,它将返回表中的每个地区值以及给定时间段内每个不同值的计数
我目前正在使用以下查询
Select count(distinct account_type)
From Table_1
Where date between '2019-08-01' and '2019-08-31' and
account_type = '0' and
account_type = '1' and
account_type = '2' and
account_type = '3' and
account_type = '4'
我正在寻找的结果集如下
account_type Count
0 123
1 456
2 789
3 101112
4 131415
我得到的结果集是
account_type
0
解决方案
您的 WHERE 子句排除了所有元素,因为它们不能同时属于 0 和 1 类型(等)。
此外,通过count(distinct account_type)
您获得不同帐户类型的数量;不是每种帐户类型的元素数量。
尝试这个:
SELECT account_type,
COUNT(*)
FROM table_1
WHERE date BETWEEN '2019-08-01' AND '2019-08-31'
AND account_type IN ('0', '1', '2', '3', '4')
GROUP BY account_type
ORDER BY account_type;
如果account_type
始终是单个字符(例如 '06' 不存在),您还可以使用:
AND account_type BETWEEN '0' AND '4'
推荐阅读
- azure - TeamCity 按计划备份并复制到 Azure,REST 脚本改进
- javascript - 如何从 Service Worker 发送“离线”消息?
- reactjs - ModuleNotFoundError:当我尝试启动时没有名为“django”的模块
- glsl - 使用特定的 WebGL / GLSL 版本运行 Web 浏览器
- c# - 如何在 blazor 组件中显示来自后端的自定义引导警报
- python - 人脸识别信心下降,同时注册更多人物和图像
- compiler-construction - 如何使用 llvmpy 在编译器中包含汇编文件中的库函数?
- google-cloud-platform - 为谷歌云存储上托管的谷歌云功能启用 CORS
- spring-boot - 带有嵌入式 Keycloak 实例的 Spring Boot GraphQL Server 无法启动
- graphql - 在服务之间发送参数 graphql apollo federation