在python中将for循环日期变量转换为sql

bttbmeg0  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(388)

我正在python脚本中编写一个oraclesql查询。查询如下:

query_dict={
    'df_fire':
    '''
    SELECT INSURED_ID AS CUST_ID, COUNT(*) AS CNT
    from POLICY
    WHERE POLICY_EXPDATE >= TO_DATE('2018/01/01', 'YYYY/MM/DD')
    AND POLICY_EFFDATE <= TO_DATE('2018/01/31', 'YYYY/MM/DD')
    GROUP BY INSURED_ID
    '''
}
"""

# Note: The duration for this kind of insurance policy is one-year.

# Note: It only shows each policy's effective date(POLICY_EFFDATE) and expire date(POLICY_EXPDAT) in the database.

然后我将其放入pickle文件并按如下方式打开:

df_fire ={}
account, pwd = 'E', 'I!'
for var, query in query_dict.items():
    df_fire[var] = get_SQL_raw_data(account, pwd, var, query)

pickle.dump(df_fire, open('./input/df_fire.pkl', 'wb'))

df_fire_dict = pickle.load(open('./input/df_fire.pkl', 'rb'))
df_fire = df_fire_dict['df_fire']

但是,此结果仅适用于201801,没有快照日期。我的目标是制作一个从201801到202004的yyyymmDataframe(如下所示)。也就是说,我想计算一个人每个月有多少份保险单。也许我需要使用for循环,但我不知道在哪里以及如何使用它。

My goal:

yyyymm icust_d cnt
-------------------
201801 A12345   1
201802 A12345   1
201803 A12345   2
....    ....   ....
202004 A12345   5

我是python的新手,已经花了好几个小时琢磨着怎么做了,但还是做不到。希望有人能帮忙。非常感谢你。

5us2dqdw

5us2dqdw1#

考虑一个要分组的扩展聚合查询 YYYYMM . 不需要循环:

SELECT TO_CHAR(POLICY_EFFDATE, 'YYYYMM') AS YYYYMM, 
       INSURED_ID AS CUST_ID, 
       COUNT(*) AS CNT
FROM POLICY
WHERE POLICY_EXPDATE >= TO_DATE('2018/01/01', 'YYYY/MM/DD')
  AND POLICY_EFFDATE <= TO_DATE('2020/04/30', 'YYYY/MM/DD')
GROUP BY TO_CHAR(POLICY_EFFDATE, 'YYYYMM'), 
         INSURED_ID
ORDER BY TO_CHAR(POLICY_EFFDATE, 'YYYYMM')

相关问题