简体   繁体   中英

SQL: How to check a string for multiple values from another table

I am trying to parse through a string to see if values from another table appear in it in any position.

select ROUTE,
case when ROUTE like '%' || b.AIRPORTCODE || '%' then 1 
else 0 end as CLASS_B,
case when ROUTE like '%' || c.AIRPORTCODE || '%' then 1 
else 0 end as CLASS_C
from FLIGHT_MESSAGE, 
(select * from CLASS_B_C_AIRPORTS where CLASS_B_C = 'B') b,
(select * from CLASS_B_C_AIRPORTS where CLASS_B_C = 'C') c

The CLASS_B_C_AIRPORTS table will have an airport code (KDCA) and whether or not it is 'B' or 'C'.

In my example below, ROUTE field will contain a string of text like:

KDCA..FLUKY.DCA246.PAUKI..MOL.FLCON6.KRIC/0127 

For this string, I'd like to return the following, because KDCA is a Class B airport and KRIC is a Class C airport:

| ROUTE                                          | CLASS_B | CLASS_C |
----------------------------------------------------------------------
| KDCA..FLUKY.DCA246.PAUKI..MOL.FLCON6.KRIC/0127 | 1       | 1       |

This query currently returns 0's for Class B and Class C against this string.

I think this is what you want:

SELECT ROUTE,
       MAX(CASE WHEN CLASS_B_C = 'B' THEN 1 ELSE 0 END) as CLASS_B,
       MAX(CASE WHEN CLASS_B_C = 'C' THEN 1 ELSE 0 END) as CLASS_C
FROM FLIGHT_MESSAGE fm JOIN 
     CLASS_B_C_AIRPORTS a
     ON fm.ROUTE LIKE '%' || a.AIRPORTCODE || '%'
GROUP BY ROUTE;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM