【转】面试之BI-SQL--table转换

guguda2008 2013-10-14 02:30:15
刚在别的地方看到的,还挺好玩,无聊的同学可以试试。

Num

1

2

4

6

7

8

10

11

13



写条SQL语句转成下表:

Column1 Column2

1 2

4 4

6 8

10 11

13 13
...全文
613 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞啊子 2013-10-21
  • 打赏
  • 举报
回复


--->这个是 缺失范围查询

--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go 
create table [tb]([Num] int)
insert [tb]
select 1 union all
select 2 union all
select 4 union all
select 6 union all
select 7 union all
select 8 union all
select 10 union all
select 11 union all
select 13



select cur+1 as startNum,nxt-1 as endNum
from(
	 select Num as cur,(select MIN(Num) from tb lt1 where t.Num<lt1.Num) as nxt
	 from tb t
) p
where nxt-cur>1;

詩和遠方 2013-10-20
  • 打赏
  • 举报
回复
引用 9 楼 fredrickhu 的回复:
好像是断号查询。恩恩
我们还没反应过来,大神已经知道问题的类别了,我会告诉你“断号查询”这个名词我从没听过吗,大神果然够经验!!!
guguda2008 2013-10-18
  • 打赏
  • 举报
回复
就是断号查询,原文解答SQL如下 selectMIN(Num),MAX(Num)from (selectROW_NUMBER()over (orderbyNum)rk,Num FromNumbers)A Groupby (A.rk-A.Num) orderbyMin(Num)
chuanzhang5687 2013-10-17
  • 打赏
  • 举报
回复
数字的连续性
--小F-- 2013-10-17
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author  :fredrickhu(小F,向高手学习)
-- Date    :2013-10-17 11:32:39
-- Verstion:
--      Microsoft SQL Server 2012 - 11.0.2100.60 (X64) 
--	Feb 10 2012 19:39:15 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go 
create table [tb]([Num] int)
insert [tb]
select 1 union all
select 2 union all
select 4 union all
select 6 union all
select 7 union all
select 8 union all
select 10 union all
select 11 union all
select 13
--------------开始查询--------------------------
select
    rtrim(a.num) AS columus1,(case when min(b.num)!=a.num then rtrim(min(b.num)) else a.num end) AS columus2
from
    (select t.num from tb t where not exists(select 1 from tb where num=t.num-1)) a,
    (select t.num from tb t where not exists(select 1 from tb where num=t.num+1)) b
where
    a.num<=b.num
group by
    a.num
----------------结果----------------------------
/* columus1     columus2
------------ -----------
1            2
4            4
6            8
10           11
13           13

(5 行受影响)
*/
奔四在望 2013-10-17
  • 打赏
  • 举报
回复
引用 3 楼 wufeng4552 的回复:
select min(t.Column1)Column1, max(t.Column1)Column2 from( select Column1,cnt=Column1-row_number()over(order by getdate()) from tb )t group by cnt --没测试
牛逼
--小F-- 2013-10-17
  • 打赏
  • 举报
回复
好像是断号查询。恩恩
火拼阿三 2013-10-17
  • 打赏
  • 举报
回复
真的太厉害了。。。还没看出规律。。。
詩和遠方 2013-10-15
  • 打赏
  • 举报
回复
引用 3 楼 wufeng4552 的回复:
select min(t.Column1)Column1, max(t.Column1)Column2 from( select Column1,cnt=Column1-row_number()over(order by getdate()) from tb )t group by cnt --没测试
额,是这个很牛逼!!!
詩和遠方 2013-10-15
  • 打赏
  • 举报
回复
引用 2 楼 ap0405140 的回复:
唐诗不才,弱弱的问一下,这是什么规律.
牛逼呀!!!
guguda2008 2013-10-14
  • 打赏
  • 举报
回复
引用 2 楼 ap0405140 的回复:
唐诗不才,弱弱的问一下,这是什么规律.
这题考的就是找规律啊嘎嘎嘎嘎,实现其实很简单
昵称被占用了 2013-10-14
  • 打赏
  • 举报
回复
select a.Num as Column1 (select top 1 b.num from test b where b.num >= a.Num and not exists ( select 1 from test where Num = b.Num + 1 ) order by b.num asc ) as Column2 from test a where not exists ( select 1 from test where Num = a.Num - 1 ) order by 1 asc
水族杰纶 2013-10-14
  • 打赏
  • 举报
回复
select min(t.Column1)Column1, max(t.Column1)Column2 from( select Column1,cnt=Column1-row_number()over(order by getdate()) from tb )t group by cnt --没测试
唐诗三百首 2013-10-14
  • 打赏
  • 举报
回复
唐诗不才,弱弱的问一下,这是什么规律.
發糞塗牆 2013-10-14
  • 打赏
  • 举报
回复
占个位置看大牛表演

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧