我有一列报告中的一行,我希望它基于SQL表中的同一列。
例如,在报告中应该是这样的:
ID | Reason | SubReason
1 | Did not like | Appearance
在SQL表中,它类似于:
ID | ReturnReason
1 | Did not like
1 | XX*SR*Appearance
1 | XX - TestData
1 | XX - TestData2
SubReason列是新添加的,当前的SQL查询类似于:
SELECT ID, ReturnReason AS 'Reason'
FROM table
WHERE LEFT(ReturnReason,2) NOT IN ('XX')
现在我想在SubReason的SELECT语句中添加一个列,如果*SR*
在值中,则应该是值。但是这不起作用,因为它在值中也有'XX'
,当前WHERE
子句将其省略。
答案 0 :(得分:2)
SELECT t.ID, t.ReturnReason AS 'Reason',
SUBSTRING(t1.ReturnReason,7,10000) as 'SubReason '
FROM t
LEFT JOIN t as t1 on t.id=t1.id and t1.ReturnReason LIKE 'XX*SR*%'
WHERE t.ReturnReason NOT LIKE 'XX%'