Sql 如何在分组时将标志设置为true并获取总数

Sql 如何在分组时将标志设置为true并获取总数,sql,sql-server,Sql,Sql Server,桌子 输出为 |C1 |C2 |C3 | |------|----|-------| |A |M |4000 | |A |S |588998 | |B |S |2 | | | | | 提前感谢使用条件聚合: |C1 |Total of C3|Flag set to true if C1 contains only S as Status| |-----|-------- |-------

桌子

输出为

|C1    |C2  |C3     |
|------|----|-------|
|A     |M   |4000   |
|A     |S   |588998 |
|B     |S   |2      |
|      |    |       | 

提前感谢

使用条件聚合:

|C1   |Total of C3|Flag set to true if C1 contains only S as Status|
|-----|--------   |-------                                         |
|A    |592998     |0                                               |
|B    |2          |1                                               | 
                        

                                                
您还可以将条件表示为:

select c1, sum(c3),
       min(case when c2 = 'S' then 1 else 0 end) as flag
from t
group by c1;

当最小值(标志)=“S”和最大值(标志)=“S”时,则1否则0结束
C2列是否包含M和S以外的值?是C2列是否包含M和S以外的值。
select c1, sum(c3),
      (case when min(c2) = max(c2) and min(c2) = 'S' then 1 else 0 end) as flag
from t
group by c1;