日期:2014-05-17 浏览次数:21347 次
create table a(lszh int,hdh int)
insert into a(lszh,hdh)
select 1,null union all
select 2,1 union all
select 3,null union all
select 4,1 union all
select 5,2 union all
select 6,2 union all
select 7,null
--查询出如下结果
lszh hdh xh
1 null 1
2 1 2
3 null 3
4 1 2
5 2 4
6 2 4
7 null 5
也就是说hdh如果相同,xh就一样
SELECT *,DENSE_RANK() OVER (ORDER BY hdh) xh
FROM a
ORDER BY lszh
SELECT lszh,hdh,DENSE_RANK() OVER (ORDER BY CASE WHEN hdh IS NULL THEN 2000+lszh else hdh END) re
FROM a
ORDER BY lszh