1

I have the following query in python, which I use via pd.read_sql_query. It works fine. I have a list of dates, called dates, that I use for the query.

for date i in dates:
     query = ('SELECT * FROM table WHERE id in'+ str(tuple(ids)) + ' AND date 
     between '+"'"+str(i)+"'"+' and '+"'"+str(i)+"'")

Is there an easier way to query the dates, such as the ids? I would like to have something like this:

query = ('SELECT * FROM table WHERE id in'+ str(tuple(ids)) + ' AND date in' + str(tuple(dates)) + "'")

However, this does not work because dates are saved as timestamp in my python list as well as in the sql database.

my sql data looks the following:

id  date                 variable 
 A  20180101 00:00:00       1
 B  20180101 00:00:00       2
 B  20180501 00:00:00       3
 C  20180201 00:00:00       4

I have a list of ids formatted as strings:

["B", "C"]

And a list of dates, formated as datetime64[ns]:

[20180101 00:00:00, 20180201 00:00:00]

The desired output is:

id  date                 variable 
 B  20180101 00:00:00       2
 C  20180201 00:00:00       4
2
  • Could you provide an example of how 'dates' look like? In general, I am looking for sample input and output. Commented Sep 14, 2018 at 20:04
  • did an update. hope its more clear now Commented Sep 14, 2018 at 20:13

1 Answer 1

2

There is many things that you can do but in my opinion this would be the most readable:

def join(l):
    return ",".join([f"'{x}'" for x in l])
ids_list = join(["B","C"])
dates_list = join(["20180101 00:00:00", "20180201 00:00:00"])

# below works for python 3.6+
print(f"SELECT * FROM table WHERE id in ({ids_list}) and date in ({dates_list})")

# below works for python < 3.6 (you can also use .format())
print("SELECT * FROM table WHERE id in %(ids_list)s and date in %(dates_list)s" % locals())
Sign up to request clarification or add additional context in comments.

2 Comments

@FriedrichFranz could you specify which python version are you using? I will change my answer accordingly.
My bad, I didnt carry the f before the string because I was unaware of itsfunction. For future referece: it worked perfectly with python 3.6.6.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.