sql - 如何计算来自相同 ID 的不同值?
问题描述
如果每个 ID 有一些重复项,我如何计算列中的不同值?
我尝试了 COUNT、DISTINCT、CASE 和所有方法,但我无法计算每个 ID 的不同值。我有一张这样的桌子。
ID Symptom
1 Headache
1 Headache
1 Hematuria
1 Leg pain
1 Headache
2 Cough
2 Headache
2 Cough
3 Cough
3 Cough
3 Cough
我想得到这样的东西。
ID Symptom
1 Headache
1 Hematuria
1 Leg pain
2 Cough
2 Headache
3 Cough
或者我如何获得总数?就像有 5 种不同的症状,但没有 11 种症状(如果我使用 DISTINCT,我会得到 4 种)。
解决方案
你需要distinct
withselect
声明:
select distinct id, Symptom
from table t;
如果您需要唯一计数,请在内部使用它count()
:
select id, count(distinct Symptom)
from table t
group by id;
推荐阅读
- monit - 监控 SSL/TLS 版本未针对端口 2812 更新?
- azure - 使用 Blob 触发器调用 Azure 函数
- swift - SwiftUI 按钮动态 SF 符号
- sorting - 以表达式为值对 Tablix 进行排序
- r - as.vector(x) 中的错误:在单细胞分析中没有将此 S4 类强制为向量的方法
- sql - 优化 SQLite 的插入速度(时间序列数据)
- github - Github Actions:自动评论拉取请求的控制台输出
- windows - 为什么要清除 Windows 事件日志来修复 Docker?
- reactjs - 未定义的方法`use_unimodules!安装 react-native-unimodules 时
- sql - Laravel Eloquent limit() 和 count() 返回完整计数