sql - 如何创建计算 Ws、Ls 和 Ds 的别名来创建记录
问题描述
我有一个带有标有“结果”的列的运动结果表,其中该列中的值是 W、L 或 D。我想创建一个别名列,该列将快速计算 Ws、Ls 和 Ds该列中的整个表格并将其显示为“Count W-Count L-Count D”。
我对 SQL 很陌生,我还没有弄清楚这个特定的请求,我也无法在 Google 中找到正确的搜索词来发现我正在寻找的情况的视频或论坛结果。
解决方案
如果您想要单独列中的值,请使用条件聚合:
select sum(case when result = 'W' then 1 else 0 end) as w_cnt,
sum(case when result = 'L' then 1 else 0 end) as l_cnt,
sum(case when result = 'T' then 1 else 0 end) as t_cnt
from t;
推荐阅读
- c# - 当管理员用户使用 Rdp 登录服务器时代码运行
- c - 通过从堆中删除值并将值插入堆中来同步文件和堆中的数据
- mysql - 最佳实践 - 在本地 mysql 服务器/RDS 上开发?
- c# - 如何去除多个 if-else 或重复的三元运算?
- java - 如何使用 lambda 在构造函数参数中实现> 1个接口方法?
- android - 将视频缩放为圆形 - Android
- javascript - Mongoose 事务 - MongoError: 承诺中止
- xml - 有没有一种简单的方法可以通过 bash 命令验证 xsd 中的 schematron
- javascript - Tabulator.js 的 id 可访问性不起作用
- html - 无法在 Firefox 中测量 Html-Image 的 Clientwidth