如何从SQL查询中为表设置值?

时间:2018-06-08 18:03:54

标签: python sql petl

我有以下方法:

   def get_base_price(date):
        # open connection to db
        # run SQL query to check if price exists for that date
        # set base price to that value if it exists
        # set it to 100 if it doesn't
        sql = '''SELECT COALESCE(TimeSeriesValue, 100) as TimeSeriesValue, EffectiveDate
            FROM [RAP].[dbo].[TimeSeriesPosition]
            WHERE TimeSeriesTypeID = 12 AND
                SecurityMasterID = 45889 AND
                FundID = 7 AND
                EffectiveDate = %s''' % date
        with self.job.rap.connect() as conn:
            data = etl.fromdb(conn, sql).cache()        
        return data

我基本上检查在设定的日期是否有找到的价格。如果它发现两者都会返回如下表:

+------------+---------------+
| Last Price | EffectiveDate |
+============+===============+
| '103.24'   | '1990-03-31'  |
+------------+---------------+

但是,如果不是,Last Price将设置为100,但EffectiveDate将为空。我可以在COALESCE表达式中使用相同的字符串格式吗?

理想情况下,我想要一个像:

这样的词典

{'Last Price': 100, 'EffectiveDate': '1990-03-31'}

0 个答案:

没有答案