我试图在python中运行SQL Query,但我收到错误not a valid month while passing dates using user input
。
该表的日期列位于" MM/DD/YYYY
"格式。我将日期作为" YYYY-MM-DD
"
以下是查询。
SELECT
substr(PLACEMENT_DESC, 1, INSTR(PLACEMENT_DESC, '.', 1) - 1) AS "'Placement#'",
SUM(VIEWS) AS "'Delivered_Impresion'",
SUM(CLICKS) AS "'Clicks'",
SUM(CONVERSIONS) AS "'Conversion'"
FROM TFR_REP.DAILY_SALES_MV
WHERE (IO_ID = {})
AND (DAY_DESC BETWEEN to_char(TO_DATE(TO_CHAR('{}'), 'MM/DD/YYYY'), 'YYYY-MM-DD')
AND to_char(TO_DATE(TO_CHAR('{}'), 'MM/DD/YYYY'), 'YYYY-MM-DD'))
GROUP BY PLACEMENT_ID,
PLACEMENT_DESC
ORDER BY PLACEMENT_ID".format(self.config.IO_ID, self.config.SDATE, self.config.EDATE)
这是python代码: 代码太大了,所以我只是简单地阅读SQL。
from __future__ import print_function
import pandas as pd
import numpy as np
from xlsxwriter.utility import xl_rowcol_to_cell
import config
import pandas.io.formats.excel
pandas.io.formats.excel.header_style = None
class Summary():
def __init__(self,config):
self.config=config
def connect_TFR_summary(self):
#sql_VDX_summary="select * from (select substr(PLACEMENT_DESC,1,INSTR(PLACEMENT_DESC, '.', 1)-1) as "'Placement#'", SDATE as "'Start_Date'", EDATE as "'End_Date'", initcap(CREATIVE_DESC) as "'Placement_Name'",COST_TYPE_DESC as "'Cost_type'",UNIT_COST as "'Unit_Cost'",BUDGET as "'Planned_Cost'",BOOKED_QTY as "'Booked_Imp#Booked_Eng'" from TFR_REP.SUMMARY_MV where (IO_ID = {}) AND (DATA_SOURCE = 'KM') AND SDATE >= TO_CHAR(TO_DATE("'{}'", 'YYYY-MM-DD'),'YYYY-MM-DD') AND EDATE <= TO_CHAR(TO_DATE("'{}'",'YYYY-MM-DD'),'YYYY-MM-DD') AND CREATIVE_DESC IN(SELECT DISTINCT CREATIVE_DESC FROM TFR_REP.SUMMARY_MV) ORDER BY PLACEMENT_ID) WHERE Placement_Name Not IN ('Pre-Roll - Desktop','Pre-Roll - Desktop + Mobile','Pre-Roll – Desktop + Mobile','Pre-Roll - In-Stream/Mobile Blend','Pre-Roll - Mobile','Pre-Roll -Desktop','Pre-Roll - In-Stream')".format(
# self.config.IO_ID, self.config.SDATE, self.config.EDATE)
sql_VDX_summary="select * from (select substr(PLACEMENT_DESC,1,INSTR(PLACEMENT_DESC, '.', 1)-1) as "'Placement#'", SDATE as "'Start_Date'", EDATE as "'End_Date'", initcap(CREATIVE_DESC) as "'Placement_Name'",COST_TYPE_DESC as "'Cost_type'",UNIT_COST as "'Unit_Cost'",BUDGET as "'Planned_Cost'",BOOKED_QTY as "'Booked_Imp#Booked_Eng'" from TFR_REP.SUMMARY_MV where (IO_ID = {}) AND (DATA_SOURCE = 'KM') AND SDATE >= TO_DATE('{}', 'YYYY-MM-DD') AND EDATE <= TO_DATE('{}','YYYY-MM-DD') AND CREATIVE_DESC IN(SELECT DISTINCT CREATIVE_DESC FROM TFR_REP.SUMMARY_MV) ORDER BY PLACEMENT_ID) WHERE Placement_Name Not IN ('Pre-Roll - Desktop','Pre-Roll - Desktop + Mobile','Pre-Roll – Desktop + Mobile','Pre-Roll - In-Stream/Mobile Blend','Pre-Roll - Mobile','Pre-Roll -Desktop','Pre-Roll - In-Stream')".format(
self.config.IO_ID, self.config.SDATE, self.config.EDATE)
sql_VDX_MV="select substr(PLACEMENT_DESC,1,INSTR(PLACEMENT_DESC, '.', 1)-1) as "'Placement#'", sum(IMPRESSIONS) as "'Impression'", sum(ENGAGEMENTS) as "'Eng'", sum(DPE_ENGAGEMENTS) as "'Deep'", sum(CPCV_COUNT) as "'Completions'" from TFR_REP.KEY_METRIC_MV WHERE (IO_ID = {}) AND (DAY_DESC BETWEEN to_char(TO_DATE(TO_CHAR('{}'), 'MM/DD/YYYY'),'YYYY-MM-DD') AND to_char(TO_DATE(TO_CHAR('{}'),'MM/DD/YYYY'),'YYYY-MM-DD')) GROUP BY PLACEMENT_ID, PLACEMENT_DESC ORDER BY PLACEMENT_ID".format(
self.config.IO_ID, self.config.SDATE,self.config.EDATE)
sql_Display_summary="select substr(PLACEMENT_DESC,1,INSTR(PLACEMENT_DESC, '.', 1)-1) as "'Placement#'", SDATE as "'Start_Date'", EDATE as "'End_Date'", CREATIVE_DESC as "'Placement_Name'", COST_TYPE_DESC as "'Cost_type'",UNIT_COST as "'Unit_Cost'",BUDGET as "'Planned_Cost'", BOOKED_QTY as "'Booked_Imp#Booked_Eng'" FROM TFR_REP.SUMMARY_MV where (IO_ID = {}) AND SDATE >= TO_DATE('{}', 'YYYY-MM-DD') AND EDATE <= TO_DATE('{}','YYYY-MM-DD') AND DATA_SOURCE = 'SalesFile' ORDER BY PLACEMENT_ID".format(
self.config.IO_ID, self.config.SDATE, self.config.EDATE)
#sql_Display_MV="select substr(PLACEMENT_DESC,1,INSTR(PLACEMENT_DESC, '.', 1)-1) as "'Placement#'", sum(VIEWS) as "'Delivered_Impresion'", sum(CLICKS) as "'Clicks'", sum(CONVERSIONS) as "'Conversion'" from TFR_REP.DAILY_SALES_MV WHERE (IO_ID = {}) AND (DAY_DESC BETWEEN to_char(TO_DATE(TO_CHAR('{}'), 'MM/DD/YYYY'),'YYYY-MM-DD') AND to_char(TO_DATE(TO_CHAR('{}'),'MM/DD/YYYY'),'YYYY-MM-DD')) GROUP BY PLACEMENT_ID, PLACEMENT_DESC ORDER BY PLACEMENT_ID".format(
#self.config.IO_ID, self.config.SDATE, self.config.EDATE)
sql_Display_MV = "select * from TFR_REP.DAILY_SALES_MV"
sql_preroll_summary="select * from (select substr(PLACEMENT_DESC,1,INSTR(PLACEMENT_DESC, '.', 1)-1) as "'Placement#'", SDATE as "'Start_Date'", EDATE as "'End_Date'", initcap(CREATIVE_DESC) as "'Placement_Name'",COST_TYPE_DESC as "'Cost_type'",UNIT_COST as "'Unit_Cost'",BUDGET as "'Planned_Cost'",BOOKED_QTY as "'Booked_Imp#Booked_Eng'" from TFR_REP.SUMMARY_MV where (IO_ID = {}) AND SDATE >= TO_DATE('{}', 'YYYY-MM-DD') AND EDATE <= TO_DATE('{}','YYYY-MM-DD') AND (DATA_SOURCE = 'KM') AND CREATIVE_DESC IN(SELECT DISTINCT CREATIVE_DESC FROM TFR_REP.SUMMARY_MV) ORDER BY PLACEMENT_ID) WHERE Placement_Name IN ('Pre-Roll - Desktop','Pre-Roll - Desktop + Mobile','Pre-Roll – Desktop + Mobile','Pre-Roll - In-Stream/Mobile Blend','Pre-Roll - Mobile','Pre-Roll -Desktop','Pre-Roll - In-Stream')".format(
self.config.IO_ID, self.config.SDATE, self.config.EDATE)
sql_preroll_mv="select substr(PLACEMENT_DESC,1,INSTR(PLACEMENT_DESC, '.', 1)-1) as "'Placement#'", sum(IMPRESSIONS) as "'Impression'", sum(CPCV_COUNT) as "'Completions'" from TFR_REP.KEY_METRIC_MV WHERE (IO_ID = {}) AND (DAY_DESC BETWEEN to_char(TO_DATE(TO_CHAR('{}'), 'MM/DD/YYYY'),'YYYY-MM-DD') AND to_char(TO_DATE(TO_CHAR('{}'),'YYYY-MM-DD')) GROUP BY PLACEMENT_ID, PLACEMENT_DESC ORDER BY PLACEMENT_ID".format(
self.config.IO_ID, self.config.SDATE, self.config.EDATE)
return sql_VDX_summary,sql_Display_summary,sql_preroll_summary,sql_Display_MV,sql_VDX_MV,sql_preroll_mv
def read_query_summary(self):
sql_VDX_summary,sql_Display_summary,sql_preroll_summary,sql_Display_MV,sql_VDX_MV,sql_preroll_mv=self.connect_TFR_summary()
read_sql_Display_mv=pd.read_sql(sql_Display_MV,self.config.conn)
答案 0 :(得分:1)
假设您以from selenium.webdriver import Ie
from selenium.webdriver.ie.options import Options
opts = Options()
opts.ignore_protected_mode_settings = True
driver = Ie(options=opts)
格式传递日期且"YYYY-MM-DD"
是日期类型,您可以使用日期文字,这是ANSI标准并避免DAY_DESC
TO_DATE
该表的日期栏位于&#34; MM / DD / YYYY&#34;格式
SELECT substr(PLACEMENT_DESC, 1, INSTR(PLACEMENT_DESC, '.', 1) - 1) AS "'Placement#'"
,SUM(VIEWS) AS "'Delivered_Impresion'"
,SUM(CLICKS) AS "'Clicks'"
,SUM(CONVERSIONS) AS "'Conversion'"
FROM TFR_REP.DAILY_SALES_MV
WHERE IO_ID = {}
AND DAY_DESC BETWEEN DATE '{}'
AND DATE '{}'
GROUP BY PLACEMENT_ID
,PLACEMENT_DESC
ORDER BY PLACEMENT_ID;
数据类型在数据库中没有任何格式。它只是根据您的会话参数而变化的显示。但是,如果您的意思是Date
是字符串(DAY_DESC
)类型,则可以在将其转换为以下字段后使用它: -
CHAR/VARCHAR2
请避免将日期存储为数据库中的字符串。