I am doing some string parsing on SQL queries and have come to a bit of a crossroads on identification and extraction of nested queries. In the case of:
select name, wins
from mlb_team
where wins > (select avg(wins) from mlb_team) and league = 'NL'
order by wins desc
I would like to extract (select avg(wins) from mlb_team)
. A more global issue arises in the case of a nested query which contains the keyword that follows the keyword from the parent query of the nested query. i.e.
select columns
from table
where column_val > (select avg(column) from table where conditional order by column asc) and league = 'NL'
order by wins desc
The nested query contains the order by clause as does the parent, so searching for that keyword alone does not work to make this code more universal. I would like to be able to put in any query and have it extract a nested query.
Tips or suggestions would help. I'm currently using a lot of regex to pull clauses out and single table queries process fine (i.e. I'm getting each individual clause by itself) but with nested queries I am hitting a wall.
Thanks guys!