Reputation: 1
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.
Upvotes: 0
Views: 93
Reputation: 1269443
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;
Upvotes: 1