日期:2014-05-17 浏览次数:20584 次
with tb (id, name, [group], leader)as(
select 1, 'aa', '一组', 0 union all
select 2, 'bb', '一组', 1 union all
select 3, 'cc', '二组', 0 union all
select 4, 'dd', '二组', 1 union all
select 5, 'ee', '二组', 0 union all
select 6, 'ff', '三组', 0 union all
select 7, 'gg', '三组', 0),
tbb as(
select *,row_number() over(partition by [group] order by leader desc,id ) number from tb)
select id,name,[group],leader,
(select count([group]) from tbb where a.[group]=[group]) 统计 from tbb a where number=1
order by id