我坚持查询。我正在他们的笔记中搜索一个客户评论,但我需要它在之前,包括和在此查询找到的评论之后返回笔记:
SELECT ct.whn, ct.tpe, ct.drctn, ct.smmry, ct.dscrptn, ct.zxcrtdby,
(SELECT nme FROM sdcrdtr WHERE sdcrdtr.id = ct.crdtr) as 'Creditor Name',
(SELECT accntrfrnce FROM cddbt WHERE cddbt.id = ct.dbt) as Reference
FROM cdcntct ct, cdcse c
WHERE c.id = ct.cse AND c.rfrnce = '123456789'
AND ct.smmry LIKE '%review%'
我在这里看到过类似的问题,但它是用于从提供特定ID或类似内容的表中查找所有内容。
我找不到从哪里开始!
提前致谢。 将
编辑:示例日期
ID WhN SMMRY DSCRPTN
12 01/05/2015 Sent Paperwork Info Received
13 01/05/2014 Reviewed Some Info Received
14 28/04/2014 Spoke to Client Said hello
15 01/05/2013 Note 15 Blank
16 01/05/2012 Note 16 Blank
17 01/05/2011 Review done Blank
18 30/12/2010 Another note Blank
所以如果我要运行我的查询,但是在之前,之后,之后的行请求,它将返回ID的12,13,14然后16,17,18。虽然它目前只返回ID 13和17,因为我不知道在这个条款中添加的位置。
答案 0 :(得分:0)
你可以这样做:
select t2.*
from MyTable t2
inner join (
select t.ID, max(tb.ID) as ID_Before, min(ta.ID) as ID_After
from MyTable t
left join MyTable tb on tb.ID < t.ID
left join MyTable ta on ta.ID > t.ID
where t.SMMRY like '%review%'
group by t.ID
) a on t2.ID in (a.ID, a.ID_Before, a.ID_After)
| ID | WhN | SMMRY | DSCRPTN |
|----|------------|-----------------|--------------------|
| 12 | 01/05/2015 | Sent Paperwork | Info Received |
| 13 | 01/05/2014 | Reviewed | Some Info Received |
| 14 | 28/04/2014 | Spoke to Client | Said hello |
| 16 | 01/05/2012 | Note 16 | Blank |
| 17 | 01/05/2011 | Review done | Blank |
| 18 | 30/12/2010 | Another note | Blank |