当前位置: 代码迷 >> Sql Server >> 复杂的 分组比较求和 的SQL
  详细解决方案

复杂的 分组比较求和 的SQL

热度:47   发布时间:2016-04-27 17:02:54.0
求一个复杂的 分组比较求和 的SQL
假设有字段A   B   C,都是数值。
需要按A字段分组,然后求出每组中B> C的个数


------解决方案--------------------

create table T(A int, B int, C int)
insert T select 1, 2, 3
insert T select 1, 4, 2

insert T select 2, 5, 3
insert T select 2, 6, 3

insert T select 3, 2, 3


select A, [B> C的个数]=sum(case when B> C then 1 else 0 end) from T group by A

--result
A B> C的个数
----------- -----------
1 1
2 2
3 0

(3 row(s) affected)
  相关解决方案