SQL查询以查找2个值之间的缺失数字

时间:2015-10-20 09:09:53

标签: teradata

需要您为以下场景编写SQL的专业知识

我在表“range_num”中有一行,如下所示。

start_num end_num

10 14

我们有什么方法可以编写一个查询来查找10和14之间的所有差距i ... 11,12,13以下格式

col1 col2 col3 col4 col5 10 11 12 13 14

我正在使用Teradata数据库,因此任何兼容的SQL查询都会有很大的帮助!!

1 个答案:

答案 0 :(得分:0)

这是您问题的解决方案......

    select 
max(case when   rownum=1 then gap end) as col1,max(case when   rownum=2 then gap end) as col2,
max(case when   rownum=3 then gap end) as col3,max(case when   rownum=4 then gap end) as col4,
max(case when   rownum=5 then gap end) as col5,max(case when   rownum=6 then gap end) as col6,
max(case when   rownum=7 then gap end) as col7,max(case when   rownum=8 then gap end) as col8,
max(case when   rownum=9 then gap end) as col9,max(case when   rownum=10 then gap end) as col10,
max(case when   rownum=11 then gap end) as col11,max(case when   rownum=12 then gap end) as col12,
max(case when   rownum=13 then gap end) as col13,max(case when   rownum=14 then gap end) as col14,
max(case when   rownum=15 then gap end) as col15,max(case when   rownum=16 then gap end) as col16,
max(case when   rownum=17 then gap end) as col17,max(case when   rownum=18 then gap end) as col18,
max(case when   rownum=19 then gap end) as col19,max(case when   rownum=20 then gap end) as col20,
max(case when   rownum=21 then gap end) as col21,max(case when   rownum=22 then gap end) as col22,
max(case when   rownum=23 then gap end) as col23
from
(
select a.id_start,
b.day_of_calendar as gap,
csum(1,1) as rownum
from db_sok.testt  a,
sys_calendar.calendar b
where b.day_of_calendar between a.id_start and a.id_end
)X

做一些调整,以防你需要别的......:)