SQL: Loop through items in dictionary

Question:

I have created a dictionary as follows:

dict = { 'NASDAQ': {'AMZN%', 'AAPL%', 'ABNB%'}, 'TSX': {'SHOP%', 'L%', 'RY%', 'XIF%'}}

I want to write a for loop within a query to fetch data from a table TICKER_TABLE with a column called TICKER which contains the dict values.

The query I am writing is part of a broader Python loop that looks like this:

for key in dict.keys():
    query = """SELECT * FROM "TICKER_TABLE"
               WHERE "TICKER" LIKE (FOR ITEM IN dict.VALUES)""""

Is there a way to do this?

Asked By: MathMan 99

||

Answers:

If TICKER_TABLE and TICKER it’s varable use f before string and {} to add varable. I’m not very advanced with sql, so I will use an extra for loop in python, although it can probably be done better but I don’t know what u actually need:

 for key in dict:
   for value in dict[key]:
     query = f"SELECT * FROM {TICKER_TABLE}
               WHERE {TICKER} LIKE {value}"

Every iteretion of second for loop to AMZN%, AAPL%, SHOP% etc

Maby something for this will be helpful for u.

Answered By: Moonar

If you really wanted to do this using SQLAlchemy you could do it like this, combining the "likes" for each key into a single query:

import sqlalchemy as sa

engine = sa.create_engine(<connection_url>, echo=True, future=True)
# Reflect the database table into an object.
tbl = sa.Table('ticker_table', sa.MetaData(), autoload_with=engine)

with engine.connect() as conn:    
    for k, v in dict_.items():    
        q = sa.select(tbl).where(sa.or_(tbl.c.ticker.like(t) for t in v))    
        rows = conn.execute(q)    
        for row in rows:    
            print(row)    

Generates SQL like this:

SELECT ticker_table.id, ticker_table.ticker 
FROM ticker_table 
WHERE ticker_table.ticker LIKE ? 
  OR ticker_table.ticker LIKE ? 
  OR ticker_table.ticker LIKE ? 
  OR ticker_table.ticker LIKE ?

If you don’t need to process the results by key you can get them all in a single query:

import functools
import sqlalchemy as sa

# Merge the dictionary values into a single set.
vals = functools.reduce(set.union, dict_.values(), set())

with engine.connect() as conn:
    q = sa.select(tbl).where(sa.or_(tbl.c.ticker.like(t) for t in vals))
    rows = conn.execute(q)
    for row in rows:
        print(row)
    print()
Answered By: snakecharmerb