Sql,如何得到这些结果?

Sql,how to get these results?

这是我需要的 table 结构和输出,请提供一些建议。

| requestid | requeststatus |         note             | lasted updated date |
|     2123  |     open      | copy from requestid 1234 | 2018/8/19           |
|     2124  |    follow up  | copy from requestid 3456 | 2018/8/20           |

如何编写一个函数来获取注释中requestid的结果。 例如,我需要获取 1234(这是从那里复制用户信息的请求 ID)作为输出。

如果我理解正确你可以尝试使用exists和子查询like

SELECT * 
FROM T t1
WHERE exists (
     SELECT 1 
     FROM T tt
     WHERE tt.note like '%' + t1.requestid  + '%'
)

请求总是最后四个字符吗?

如果是这样,这将适用于大多数数据库:

select t.*, right(note, 4)
from t;
with cte as (
select distinct  requestid, reverse(LEFT(REVERSE(Note),CHARINDEX(' ',REVERSE(Note)))) as Cloneformid ,note,CreatedDate,ROW_NUMBER()  OVER(PARTITION BY RequestID ORDER by CreatedDate desc) as rn 
from table 
where   Note like 'cloned from request Id %' 
)
select RequestID, cast(Cloneformid  as int) as Cloneformid
from cte
where   rn =1 and  isnumeric(Cloneformid) = 1 

如果您使用的是 Oracle,请使用:

select substr(note, -4) from table where 1;

如果您使用的是 MS SQL,

select right(note, 4) from table where 1;

如果您正在使用 MySQL,

select substr(note, char_length(note) - 3, 4) from table where 1;