设为首页 加入收藏

TOP

一些比较难的sql问题:8
2014-11-24 01:24:21 来源: 作者: 【 】 浏览:4
Tags:一些 比较 sql 问题
一些比较难的sql问题:8
最近,在 论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了。
所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。
1、求一查询语句 

CREATE #temp (cName CHAR(1),re int)
INSERT #temp
SELECT 'A',1 UNION ALL
SELECT 'A',2 UNION ALL
SELECT 'B',3 UNION ALL
SELECT 'A',4 UNION ALL
SELECT 'A',5
如何查询得到如下的结果:
cName,re,xh
-------------------
'A',1,1
'A',2,1
'B',3,2
'A',4,3
'A',5,3

下面是我的解答:
[sql] 
select *,  
       dense_rank() over(order by case when exists(select  t2.re from #temp t2   
                                                   where t1.cname = t2.cname   
                                                   and t1.re= t2.re + 1)  
                                            then (select  t2.re from #temp t2   
                                                   where t1.cname = t2.cname   
                                                   and t1.re= t2.re + 1)  
                                       else t1.re  
                                   end  
                         ) as xh  
from #temp t1  
/*  
cName re xh  
A     1  1  
A     2  1  
B     3  2  
A     4  3  
A     5  3  
*/  

但是这个解答是有问题的,因为当连续的记录超过3条时,就会有问题,所以修改了一下,这个是正确的解法:
[sql] 
create table #temp (cName CHAR(1),re int)  
  
insert into #temp  
SELECT 'A',1 UNION ALL  
SELECT 'A',2 UNION ALL  
SELECT 'B',3 UNION ALL  
SELECT 'A',4 UNION ALL  
SELECT 'A',5 union all  
SELECT 'A',6 union all  
SELECT 'A',7 union all  
SELECT 'D',8 union all  
SELECT 'D',9 union all  
SELECT 'D',10 union all  
select 'B',11 union all  
select 'A',12  
  
;with t  
as  
(  
select *,  
       row_number() over(partition by cname order by re) as rownum  
from #temp   
)  
  
select cname,  
       re,  
       dense_rank() over(order by case when exists(select min(t2.re) from t t2   
                                                   where t1.cname = t2.cname   
                                                   and t1.re-t1.rownum= t2.re-t2.rownum)  
                                            then (select  min(t2.re) from t t2   
                                                   where t1.cname = t2.cname   
                                                   and t1.re-t1.rownum= t2.re-t2.rownum)  
                                       else t1.re  
                                   end  
                         ) as xh  
from t t1  
/*cname re  xh  
A   1   1  
A   2   1  
B   3   2  
A   4   3  
A   5   3  
A   6   3  
A   7   3  
D   8   4  
D   9   4  
D   10  4  
B   11  5  
A   12  6  
*/  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇数据库表修改删除及查询操作 下一篇分布式事务笔记

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: