select *
, checker = case
when charindex('SURPLUS CASH',concat(bank_text_1, bank_text_2, bank_text_3, bank_text_4, bank_text_5))>0
or charindex('CHQ IN AT',concat(bank_text_1, bank_text_2, bank_text_3, bank_text_4, bank_text_5))>0
or charindex('ELECTR CHARGE',concat(bank_text_1, bank_text_2, bank_text_3, bank_text_4, bank_text_5))>0
then 'Exclude'
else ''
end
from t
結果:http://rextester.com/VBOC27547
+--------------+---------------+-----------------------+------------------+-------------+----------------+--------+---------+
| statement_no | bank_text_1 | bank_text_2 | bank_text_3 | bank_text_4 | bank_text_5 | amount | checker |
+--------------+---------------+-----------------------+------------------+-------------+----------------+--------+---------+
| 0001 | SURPLUS CASH | | | | | 125,00 | Exclude |
| 0002 | M THOMAS | TRAINING FEE | CHQ IN AT 100217 | | | 470,00 | Exclude |
| 0003 | SWALEC | Training Centre One | Abergaveny | | Electr Charges | 700,00 | Exclude |
| 0004 | M Thomas | Training Centre Hire | | | | 850,00 | |
+--------------+---------------+-----------------------+------------------+-------------+----------------+--------+---------+
我測試的這個性能相比約翰·卡佩萊蒂的回答超過100多萬行,我不認爲他們是可比的。
我個人的偏好是約翰的回答,使用cross apply()
。
在下面的集合中,每個集合中的第一個查詢使用charindex(...,concat(..)
,第二個查詢是約翰的cross apply()
版本。
雖然這個實例的硬件規格相當薄弱,所以你的里程會有所不同。
-----------------------
(1000000 row(s) affected)
Table 't'. Scan count 1, logical reads 8153, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 3010 ms, elapsed time = 8750 ms.
(1000000 row(s) affected)
Table 't'. Scan count 1, logical reads 8153, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 5585 ms, elapsed time = 8725 ms.
-----------------------
(1000000 row(s) affected)
Table 't'. Scan count 1, logical reads 8153, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 3307 ms, elapsed time = 8685 ms.
(1000000 row(s) affected)
Table 't'. Scan count 1, logical reads 8153, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 5663 ms, elapsed time = 8913 ms.
-----------------------
(1000000 row(s) affected)
Table 't'. Scan count 1, logical reads 8153, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 3073 ms, elapsed time = 8414 ms.
(1000000 row(s) affected)
Table 't'. Scan count 1, logical reads 8153, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 5647 ms, elapsed time = 9049 ms.
-------------------
(1000000 row(s) affected)
Table 't'. Scan count 1, logical reads 8153, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 3011 ms, elapsed time = 8706 ms.
(1000000 row(s) affected)
Table 't'. Scan count 1, logical reads 8153, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 5491 ms, elapsed time = 8704 ms.
-----------------------
如果您使用某種數據訪問方式,如JDBC(Java)或ADO.NET(.NET),則可以使用參數。使用參數使您的查詢不再混亂。我更喜歡你已經擁有的方式,因爲它很容易維護。 – Loc