sql - 在 postgres 查询中计算值的实例
问题描述
有点 SQL 新手问题..
如果我有一个表格,如下所示:
主机故障致命组名 主机 A 数据闻起来很可疑 主持人 B Flanklecrumpet 需要一个拥抱 y 制作 主持人 A RAM 喜欢 EWE n 研究 主持人 Z 踏板上的一根横梁歪斜 y 研究
..我想得到一些统计数据,我可以..
select count(distinct host) as hosts, count(host) as faults, group from tablename group by groupname
..这给了我每个组名的故障数和受影响的主机。
hosts faults groupname
2 3 research
1 1 production
我可以在同一个查询中显示致命条目的数量吗?
解决方案
使用条件聚合
select count(distinct host) as hosts,
count(host) as faults,sum(case when fatal='y' then 1 else 0 end) as numberofenty,
groupname from tablename group by groupname
推荐阅读
- ios - 在 performBlock 中多次保存上下文
- python - AttributeError:“numpy.ndarray”对象没有属性“值”
- javascript - 如何将隐藏/功能滚动条与视差元素结合起来?
- email - 如何在 Microsoft Outlook 中创建带有图像背景的时事通讯
- javascript - 如何将请求委托给 nodejs express 中的特定路由?
- c++ - 为什么在 x64 构建的 ARM 构建中缺少 CoCreateInstanceFromApp?
- java - 一个列表的多个 JComboboxes
- php - 在 Woocommerce 中更改交叉销售的订购参数
- java - 强制一个新的 HTTP 响应
- raku - 在 Perl 6 中是导出和绑定