我当前的表格如下:
+-------+------+
| Level | Team |
+-------+------+
| 1 | 1 |
| 2 | 1 |
| 2 | 1 |
| 3 | 2 |
| 3 | 2 |
| 3 | 2 |
+-------+------+
我想按级别分组,并且知道两个团队的级别数。通过使用以下命令,我可以轻松获得单个团队的人数:
SELECT Level, Count(Team)
FROM table
WHERE Team = 1
GROUP BY Level
SORT BY Level;
+-------+-------------+
| Level | Team1_Count |
+-------+-------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 0 |
+-------+-------------+
但是,我想要的最终结果如下:
+-------+-------------+-------------+
| Level | Team1_Count | Team2_Count |
+-------+-------------+-------------+
| 1 | 1 | 0 |
| 2 | 2 | 0 |
| 3 | 0 | 3 |
+-------+-------------+-------------+
删除WHERE子句可得出每个级别的总数,但不会将其分成团队。如何创建两个新列并显示每个级别的计数?
答案 0 :(得分:0)
使用combineLatest(...this.filtersList.map((f) => f.filtersChanges).filter(f => f !== null)).subscribe(
...
);
和case
表达式尝试以下操作。这是demo。
sum
输出
select
level,
sum(case when team = 1 then 1 else 0 end) as Team1_count,
sum(case when team = 2 then 1 else 0 end) as Team2_count
from table
group by
level
order by
level
如果您使用的是postgreSQL,则可以将| level | Team1_count | Team2_count |
| ----- | ----------- | ----------- |
| 1 | 1 | 0 |
| 2 | 2 | 0 |
| 3 | 0 | 3 |
与filter
一起使用,如下所示
count