SQL Server:提取包含特定文本的行之后的行

时间:2017-02-21 06:01:04

标签: sql sql-server

有没有办法只提取包含type ='电子邮件'?的行后面的第一行?

样本表:

id     type      details
1   Email   admin@sqlfiddle.com
2   1234    1234
3   Email   orange@hotmail.com
4   12345   12345
5   123456  123456
6   Email   cake@hotmail.com
7   1234567     1234567
8   12345678    12345678
9   123456789   123456789
10  Email   lala@gmail.com
11  01      01
12  Email   apple@yahoo.ca
13  012     012
14  Email   red@gmail.com
15  0123    0123

使用Python和Pandas,我会写这样的东西......

indexes = table[table['type']=='Email'].index + 1
table = table.ix[indexes]

输出将是......

2   1234    1234
4   12345   12345
7   1234567     1234567
11  01  01
13  012     012
15  0123    0123

4 个答案:

答案 0 :(得分:3)

select  *

from   (select  *
               ,lag (type) over (order by id) as prev_type

        from    t
        ) t

where   prev_type = 'Email'

答案 1 :(得分:1)

对于像这样的SQL Server查询

select * from table where id in (Select id+1 from Table where type='Email')

答案 2 :(得分:1)

如果您只是想要在电子邮件行之后的第一条记录,那么您可能会更加有选择性:

SELECT * FROM Table WHERE ID IN (SELECT ID+1 FROM Table where type='Email') 

答案 3 :(得分:0)

这是一个即使id有差距也应该有效的解决方案。它使用窗口函数。

---Sample data
WITH data([id], [type] , [details]) AS
(
    SELECT 1,'Email','admin@sqlfiddle.com' UNION ALL
    SELECT 2,'1234', '1234' UNION ALL
    SELECT 3,'Email','orange@hotmail.com' UNION ALL
    SELECT 4,'12345','12345' UNION ALL
    SELECT 5,'123456',  '123456' UNION ALL
    SELECT 6,'Email','cake@hotmail.com' UNION ALL
    SELECT 7,'1234567',  '1234567' UNION ALL
    SELECT 8,'12345678', '12345678' UNION ALL
    SELECT 9,'123456789','123456789' UNION ALL
    SELECT 10,  'Email','lala@gmail.com' UNION ALL
    SELECT 11,  '01','01' UNION ALL
    SELECT 12,  'Email','apple@yahoo.ca' UNION ALL
    SELECT 13 , '012',  '012' UNION ALL
    SELECT 14  ,'Email','red@gmail.com' UNION ALL
    SELECT 15  ,'0123', '0123'
),
---temporary table to hold row numbers
tbl([Row_Num], [id], [type]) AS
(
    SELECT (ROW_NUMBER() OVER (ORDER BY [id])) AS [Row_Num] ,[id],[type] FROM data 
)

---actual query using both tables
SELECT 
    d.[id], 
    d.[type], 
    d.[details] 
FROM [data] d 

INNER JOIN

[tbl] t

ON d.[id] = t.[id] 

WHERE t.[Row_Num] IN (SELECT Row_Num + 1 FROM tbl WHERE [type] = 'Email')