0

I have a Python dictionary that has list of all products we sell. Is is possible to have these products entered in a WHERE clause of a SQL statement as shown below and get the corresponding output.

Dictionary

Given below is the list of products we sell:

prod_list : [prod_a, prod_b, prod_c, prod_d]

I am looking to have a separate SQL generated for each of these products wherein the above mentioned prod_names would be used in the where clause in the SQL listed below:

Sample SQL:

select prod_id,prod_name,count (*)
from daily_sales
where prod_name = '' <-- this value to be pulled from the dictionary above
group by prod_id,prod_name

I am using Amazon Redshift DB..

Could anyone guide me on how to do this. Thanks.

dark horse
  • 3,211
  • 8
  • 19
  • 35
  • 1
    What exactly needs to go in the WHERE clause? – glibdud Sep 12 '18 at 19:25
  • 1
    Please post a valid python data structure as input. I see a list of dictionaries if anything. – user2390182 Sep 12 '18 at 19:25
  • @glibdud, trying to pass in prod_names in the where clause namely prod_a, prod_b, prod_c – dark horse Sep 12 '18 at 19:26
  • @schwobaseggl, I basically have a list that has prod_name and prod_id as a dictionary. I could probably convert it into a dataframe. But would like to know how could I dynamically pass the prod_names into the where clause – dark horse Sep 12 '18 at 19:27
  • I think a more complete example would be helpful. Are you trying to generate a query for one specific product? A list of products? – glibdud Sep 12 '18 at 19:27
  • See: https://stackoverflow.com/questions/35061730/python-list-of-ints-in-prepared-sql-statement – Patrick Haugh Sep 12 '18 at 19:28
  • Also, what DB are you using? – glibdud Sep 12 '18 at 19:28
  • @glibdud, I am trying to generate a query that would execute for each products in a loop. – dark horse Sep 12 '18 at 19:28
  • Sorry forgot to mention it in my inital post. I am using Redshift DB.. – dark horse Sep 12 '18 at 19:30
  • @darkhorse It is not clear what clause you want to generate. `where prod_name = 'prod_a','prod_b','prod_c`' is not valid SQL. `where prod_name = 'prod_a,prod_b,prod_c`' will probably try to access an unexisting column – Stop harming Monica Sep 12 '18 at 19:33
  • @Goyo, I basically have list of all product names stored seperately. I am trying to pass in these values into the SQL query in the where clause and extract the output.. – dark horse Sep 12 '18 at 19:34
  • @darkhorse I think you already said that, but it doesn't tell me what where clause you want. – Stop harming Monica Sep 12 '18 at 19:36
  • if it was to it once, I could have added where prod_name = prod_a. I have a list of products and would like to do it in a loop for a list of products. I am not sure how to go about this. – dark horse Sep 12 '18 at 19:37
  • @ Oh, if by chance you want to generate and execute a separate SQL command for each product I think that is relevant and should be explained **in the question**. – Stop harming Monica Sep 12 '18 at 19:37
  • My bad, if I did not convey my message correctly. Let me edit my question and add in these points so that it would help readers understand what I was looking for.. – dark horse Sep 12 '18 at 19:39
  • @Goyo, add few more points.. Hope this would make better sense of what I am looking for.. – dark horse Sep 12 '18 at 19:41

0 Answers0