我遇到一个表,它的结构与下面的变量@t相似。
我想为每个开始条目找到结束条目,但这很困难,因为条目可能包含子条目。
你知道我怎么用它的位置为它的共同发起人startentrytext找到endentrytext吗?
条目的视觉表示:
-- Start 1
-- Start 2
-- Stop 2
-- Start 3
-- Start 4
-- Start 5
-- Stop 5
-- Stop 4
-- Stop 3
-- Stop 1
期望输出:
StartEntryText EndEntryText
Start 1 Stop 1
Start 2 Stop 2
Start 3 Stop 3
Start 4 Stop 4
Start 5 Stop 5
sql语句:
-- Simplified Table Structure
DECLARE @t TABLE (EntryText varchar(16) NULL
, EntryType varchar(16)
, EntryIndex int NULL
)
-- Test Data
INSERT INTO @t(EntryText, EntryType, EntryIndex)
SELECT d.EntryText
, d.EntryType
, d.EntryIndex
FROM
(
SELECT 'Start 1 ' AS EntryText, 1 AS EntryIndex, 'Open' AS EntryType
UNION ALL SELECT 'Start 2' AS EntryText, 2 AS EntryIndex, 'Open' AS EntryType
UNION ALL SELECT 'Stop 2' AS EntryText, 3 AS EntryIndex, 'Close' AS EntryType
UNION ALL SELECT 'Start 3' AS EntryText, 4 AS EntryIndex, 'Open' AS EntryType
UNION ALL SELECT 'Start 4' AS EntryText, 5 AS EntryIndex, 'Open' AS EntryType
UNION ALL SELECT 'Start 5' AS EntryText, 6 AS EntryIndex, 'Open' AS EntryType
UNION ALL SELECT 'Stop 5' AS EntryText, 7 AS EntryIndex, 'Close' AS EntryType
UNION ALL SELECT 'Stop 4' AS EntryText, 8 AS EntryIndex, 'Close' AS EntryType
UNION ALL SELECT 'Stop 3' AS EntryText, 9 AS EntryIndex, 'Close' AS EntryType
UNION ALL SELECT 'Stop 1' AS EntryText, 10 AS EntryIndex, 'Close' AS EntryType
) d
-- TODO: Find EndEntryText
SELECT t.EntryText AS StartEntryText
, NULL AS EndEntryText
FROM @t t
WHERE t.EntryType = 'Open'
1条答案
按热度按时间weylhg0b1#
不是一个完美的解决方案,但这应该做我需要的。
这基本上是通过将每个条目的祖先加在一起+1来计算每个条目的级别
OPEN
和-1表示CLOSE
. 然后得到第一个CLOSE
每次之后OPEN
在同一层面上。