我试图以每种形式显示冰岛连续海拔的间隔:
ELEVATION
0 27
29 33
35
37 40
42 46
48
51 63
目前,我只是设法跟踪差距,我有一些想法:
如果(columnB中的值的计数直到columnC =' GAP')等于 columnB的值,其中columnC =' GAP'然后我们有一个连续的间隔 在columnA和column B的值之间
有人能给我一些提示吗?
with x as (
SELECT distinct elevation
FROM CITIES
WHERE iso = 'IS' AND iso IS NOT NULL
),y as (
SELECT a.ELEVATION as "A",B.ELEVATION as "B",C.ELEVATION as "C"
FROM x a
JOIN x b ON b.ELEVATION > a.ELEVATION
LEFT JOIN x c ON c.ELEVATION > b.ELEVATION AND c.ELEVATION < b.ELEVATION + 2
)select y.A,y.B,y.C,case when y.C is null then 'GAP' else ' ' end GAPZ from y
order by 1,2
A B C GAP
------------------
0 1 2
0 2 3
0 3 4
...
...
...
0 25 26
0 26 27
0 27 GAP
0 29 30
0 30 31
0 31 32
0 32 33
0 33 GAP
0 35 GAP
0 37 38
0 38 39
0 39 40
0 40 GAP
0 42 43
0 43 44
0 44 45
0 45 46
0 46 GAP
0 48 GAP
0 51 52
0 52 53
...
...
...
0 61 62
0 62 63
0 63 GAP
0 65 66
0 66 67
0 67 68
0 68 69
0 69 GAP
0 71 72
...
...
...
答案 0 :(得分:1)
你已经到了一半!您需要标记间隙的任一端,然后仅选择那些行。
Select x,
Case lead(x,x) over(order by x)
When x+1 then null
Else x
End as endpoint,
Case lag(x,x) over(order by x)
When x-1 then null
Else x
End as startpoint
From table
这将显示行是开始还是端点。我们称之为Q1。现在我们只需从中选择我们需要的东西。
Select Q1.startpoint,
(Select min(endpoint)
From Q1 as endp
Where endp.endpoint >= Q1.x) as endpoint
From Q1
Where Q1.startpoint is not null