1

我需要过滤掉 SQL 表中的行,这些行在指定值列表中的一列中具有值。简化表如下所示:(抱歉格式错误,以前从未在此处发布过)

Error Codes     | Other Column 1 | Other Column 2 ...

--------------------------------------------------
F010,F123,F345, |  ......        | .....

F231,FC85,F904, |  ......        | .....

FC432,F0425,NA, |  ......        | .....

 ....

我首先拆分错误代码列以获取 3 个错误代码中的每一个,这是一个逗号分隔的值字符串。然后我需要过滤掉给定列表中包含所有三个错误代码的行,例如('F010'、'FC542'、'FB943')。我在 Teradata DB 上运行它,这是查询的一部分,但似乎没有过滤掉列表中的所有组合:

SELECT ... , 

CASE
WHEN ( substr(a.error_code, 1, position(',' in a.error_code)-1) in ('F010', 'FC542', 'FB012' 'FB943', 'NA')

AND substr(a.error_code,
              position(',' in a.error_code)+1, 
              position(',' in substr(a.error_code,  position(',' in a.error_code)+1, Characters(a.error_code)-position(',' in a.error_code)))-1) in ('F010', 'FC542', 'FB012' 'FB943', 'NA')

AND substr(a.error_code,
          position(',' in a.error_code) + position(',' in substr(a.error_code,    position(',' in a.error_code)+1, Characters(a.error_code)-position(',' in a.error_code)))+1,
          Characters(a.error_code)-(position(',' in a.error_code) + position(',' in substr(a.error_code,  position(',' in a.error_code)+1, Characters(a.error_code)-position(',' in a.error_code))))-1) in ('F010', 'FC542', 'FB012' 'FB943', 'NA') )

THEN 'No'

ELSE 'Yes'

end Error_Module,

...
FROM Error_code_table a

WHERE Error_Module = 'Yes'

另一方面,Characters() 函数与 Length() 函数相同。

谢谢,迈克

4

2 回答 2

1

Having multiple values in a comma-separated list contained in a single column makes this much more difficult. This task would be much easier if this could be reworked so that there's only a single error code per row.

I'm not familiar with Teradata, but looking at Google it appears they're big into analytics and business intelligence. As such, I'd hope they have some regular expression functionality available and I think that would be a good place for you to start looking. For example, in Oracle I'd do something like the following to extract the error codes from the string:

SELECT TRIM(REGEXP_REPLACE(ERROR_CODES, '(.*),.*,.*', '\1')),
       TRIM(REGEXP_REPLACE(ERROR_CODES, '.*,(.*),.*', '\1')),
       TRIM(REGEXP_REPLACE(ERROR_CODES, '.*,.*,(.*)', '\1'))
  INTO strErr1, strErr2, strErr3
  FROM DUAL;

Good luck.

于 2010-11-03T11:25:51.390 回答
0

如果错误代码没有出现在列表的开头,它将始终出现在表单中,<error code>,

SELECT
    CASE
        WHEN substr(a.error_code, 1, position(',' in a.error_code) - 1) in ('F010', 'FC542', 'FB012', 'FB943', 'NA')
            OR a.error_code LIKE ANY ('%,F010,%', '%,FC542,%', '%,FB012,%', '%,FB943,%', '%,NA,%')
        THEN 'No'
        ELSE 'Yes'
    End Error_Module
FROM Error_code_table a
WHERE Error_Module = 'Yes';
于 2010-11-04T23:09:04.743 回答