Python 当执行一个不返回任何内容的查询时,会创建什么类型的表?
我正在使用petl并尝试创建一个简单的表,其中包含一个查询中的值。我写了以下内容:Python 当执行一个不返回任何内容的查询时,会创建什么类型的表?,python,sql,etl,Python,Sql,Etl,我正在使用petl并尝试创建一个简单的表,其中包含一个查询中的值。我写了以下内容: @staticmethod 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
@staticmethod
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 [TimeSeriesValue]
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
我正在连接数据库,如果有该日期的值,那么我将能够创建一个如下所示的表:
+-----------------+
| TimeSeriesValue |
+=================+
| 100 |
+-----------------+
IF(TimeSeriesValue IS NOT NULL)
<your query>
ELSE
SET TimeSeriesValue = 100
但是,如果查询没有返回任何内容,那么表会是什么样子?
如果查询没有返回任何内容,我想将TimeSeriesValue设置为100。不知道该如何做。执行语句时应该传入参数,而不是咀嚼字符串。但这不是你问题的核心 可能最简单的解决方案是用SQL完成所有工作。如果您最多需要查询一行,则:
SELECT COALESCE(TimeSeriesValue, 100) as TimeSeriesValue
FROM [RAP].[dbo].[TimeSeriesPosition]
WHERE TimeSeriesTypeID = 12 AND
SecurityMasterID = 45889 AND
FundID = 7 AND
EffectiveDate = %s
这将始终返回一行,如果未找到任何内容,则会将其放入
100
值。如果查询未返回任何内容,则只会显示列名称,列名称下方不带任何内容。我想试试这样的东西:
+-----------------+
| TimeSeriesValue |
+=================+
| 100 |
+-----------------+
IF(TimeSeriesValue IS NOT NULL)
<your query>
ELSE
SET TimeSeriesValue = 100
IF(TimeSeriesValue不为NULL)
其他的
设置TimeSeriesValue=100