1

我有一个报告的一行的两列,我希望基于 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')

现在我想在 SELECT 语句中为 SubReason 添加一列,如果*SR*在值中,它应该是值。但是,这不起作用,因为它也包含在当前子句'XX'中省略的值中。WHERE

4

1 回答 1

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%'

SQLFiddle 演示

于 2013-10-16T13:45:53.067 回答