[英]Oracle SQL query with multiple conditions based on pandas dataframe
I have a dataframe in the following format:我有一个 dataframe 格式如下:
PID PID | Date1日期1 | Date2日期2 | Details细节 |
---|---|---|---|
17750A 17750A | 03/07/1960 1960 年 3 月 7 日 | 06/07/2009 2009 年 6 月 7 日 | A1B3 A1B3 |
17758X 17758X | 03/07/1960 1960 年 3 月 7 日 | 06/07/2009 2009 年 6 月 7 日 | A1B3 A1B3 |
06/09/1961 06/09/1961 | 11/05/2013 2013 年 11 月 5 日 | A2B2 A2B2 | |
28363D 28363D | 20/11/1964 1964 年 11 月 20 日 | 05/03/2019 2019 年 5 月 3 日 | A1A2 A1A2 |
30050A 30050A | 30/06/1961 1961 年 6 月 30 日 | 18/07/2017 2017 年 7 月 18 日 | A1B3 A1B3 |
04/11/1961 1961 年 4 月 11 日 | 16/10/2008 2008 年 10 月 16 日 | A2B2 A2B2 |
And an Oracle database with a table as follows:还有一个 Oracle 数据库,其表如下:
ID ID | DateA日期A | DateB日期B | Notes笔记 |
---|---|---|---|
17750A 17750A | 03/07/1960 1960 年 3 月 7 日 | 06/07/2009 2009 年 6 月 7 日 | A1B3 A1B3 |
03/07/1960 1960 年 3 月 7 日 | 06/07/2009 2009 年 6 月 7 日 | A1B3 A1B3 | |
20964Q 20964Q | 06/09/1961 06/09/1961 | 11/05/2013 2013 年 11 月 5 日 | A2B2 A2B2 |
28363D 28363D | 20/11/1964 1964 年 11 月 20 日 | 04/03/2019 2019 年 4 月 3 日 | A1A2 A1A2 |
30/06/1961 1961 年 6 月 30 日 | 19/07/2017 2017 年 7 月 19 日 | A1B3 A1B3 | |
10832Q 10832Q | 04/11/1961 1961 年 4 月 11 日 | 17/10/2008 2008 年 10 月 17 日 | A2B2 A2B2 |
I need to query the database to return another df containing any record where the ID matches a PID, or where (Date1, Date2) equals (DateA, DateB) - ie both dates in a df row match both dates in a table row.我需要查询数据库以返回另一个 df,其中包含 ID 与 PID 匹配或 (Date1, Date2) 等于 (DateA, DateB) 的任何记录 - 即 df 行中的两个日期都与表行中的两个日期匹配。
So far, I've managed to achieve the first, but not the second.到目前为止,我已经成功实现了第一个,但不是第二个。
pid_list = df['PID'].values
nvars = ','.join(f':{i}' for i in range(len(pid_list)))
sql_query = """
SELECT
gd.ID,
gd.DateA,
gd.DateB,
gd.Notes
FROM table1 as gd
WHERE gd.ID in (%s)
""" % nvars
df_result = pd_read_sql(sql_query, connection, params=pid_list)
How can I expand that to also match on the pair of dates?如何扩展它以匹配这对日期? Is there a way to do this by passing a list of tuples as a param, rather than needing to iterate through pairs of dates?有没有办法通过将元组列表作为参数传递来做到这一点,而不是需要遍历日期对? Something like:就像是:
sql_query = """
SELECT
gd.ID,
gd.DateA,
gd.DateB,
gd.Notes
FROM table1 as gd
WHERE gd.pid in (%s)
OR (Date1 = DateA AND Date2 = DateB)
""" % nvars, dates
df_result = pd.read_sql(sql_query, connection, params=(pid_list, (Date1, Date2)))
I think the params passed to pd.read_sql() may need to be a dict, but am not sure how to structure this or how to reference the different entries in the SQL query without iterating.我认为传递给 pd.read_sql() 的参数可能需要是一个字典,但我不确定如何构造它或如何在不迭代的情况下引用 SQL 查询中的不同条目。
Try selected all data for Query:尝试选择查询的所有数据:
SELECT
gd.ID,
gd.DateA,
gd.DateB,
gd.Notes
FROM table1 as gd
and filter from your DataFrame.并从您的 DataFrame 中过滤。 Don't execute query with tuple and IN conditions only 1000 values.不要使用元组和 IN 条件仅 1000 个值执行查询。
or two query and join two result或两个查询并连接两个结果
or create table in oracle and filter query oracle或在 oracle 中创建表并过滤查询 oracle
You will be always limited with the 1000 entry limit (which you may workaround with the split in sublists using OR
).您将始终受到 1000 个条目的限制(您可以使用OR
拆分子列表来解决此问题)。
For the date compare you may use multi-element IN
list compare as illustrated in the following query对于日期比较,您可以使用多元素IN
列表比较,如以下查询所示
select * from tab
where id in ('17750A','20964Q') or
(dateA,dateB) in ( ( date'1960-07-03', date'2009-07-06'),
(date'1961-06-30' , date'2017-07-19'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.