I have a SQL query that I want want to execute for a list of input parameters.
SELECT Field1, Field2
FROM Table
WHERE Field3 = ?
AND Field4 = ?
I have ~10,000 pairs of values that I want to run this query for. At the moment I'm iterating over the list, and appending each result to a data frame. I feel like there is probably a more Pythonic way to do this. I just don't know what it is.
Is there a cleaner way to do this?
CodeNewbie
2,11118 silver badges29 bronze badges
asked Aug 21, 2014 at 15:02
Batman
9,0177 gold badges48 silver badges87 bronze badges
1 Answer 1
I think you need to create a temp table that stores the 10,000 pairs of values.
Then you can use an Inner Join on that temp table.
Example:
Select f1,f2
From
table t
Inner Join temptable m
On m.c1 = t.f3 and m.c2(column 2) = t.f4
Phillip
4571 gold badge4 silver badges13 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Batman
That had also occurred to me, but still felt a little bit like brute force and ignorance.
Batman
It looks like this is going to be the best solution for my problem, because (with a left join) it's also the easiest way to handle the case where there is not a record in the table that matches my input parameters.
default
If (field3 = x1 and field4 =y1) or (field3 = x2 and field y2)... etc? How is this list stored? Is it just a list in Excel, or is it something you can put into a table as mentioned in one of the answers?