日期:2014-05-17 浏览次数:20493 次
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (A02 nvarchar(2),A03 nvarchar(2))
insert into [TB]
select 'A','B' union all
select 'A','C' union all
select 'A','W' union all
select 'A','E' union all
select 'A','R' union all
select 'A','A' union all
select 'B','Q' union all
select 'A','Y' union all
select 'A','C1' union all
select 'A','W1' union all
select 'A','E1' union all
select 'A','R1' union all
select 'A','A1' union all
select 'B','Q1' union all
select 'B','Q2' union all
select 'B','Q3' union all
select 'B','Q4' union all
select 'B','Q5' union all
select 'B','Q6'
select DENSE_RANK() OVER(ORDER BY A02,rn) A01,t.A02,t.A03
from (
select *,(ROW_NUMBER() OVER(PARTITION BY A02 ORDER BY A03) -1 )/5 rn
from TB
) t
select ntile(6) over(order by A02)as A01,*
from [TB]