首页 > 解决方案 > 如何创建计算 Ws、Ls 和 Ds 的别名来创建记录

问题描述

我有一个带有标有“结果”的列的运动结果表,其中该列中的值是 W、L 或 D。我想创建一个别名列,该列将快速计算 Ws、Ls 和 Ds该列中的整个表格并将其显示为“Count W-Count L-Count D”。

我对 SQL 很陌生,我还没有弄清楚这个特定的请求,我也无法在 Google 中找到正确的搜索词来发现我正在寻找的情况的视频或论坛结果。

标签: sql

解决方案


如果您想要单独中的值,请使用条件聚合:

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;

推荐阅读