在Python Sqlite中创建表时出错

时间:2016-01-25 23:14:19

标签: python sql sqlite

我正在运行一些查询,我想将查询的输出存储为新表。我正在使用Jupyter笔记本,我已连接到sqlite数据库,该数据库具有我在查询中使用的表。

sql2="""

create table sales_fact_unique  as
(
 with min_leads as 
 ( 
  select LeadId, min(BookingCreateDate)as FirstBookingDate
  from 
   sales_fact
    where GrossRevenue <>0
  group by
   LeadId
) 

 select a.LeadId, a.FirstBookingDate, b.ArrivalDate,
  b.DepartureDate,b.DealWonSalesPersonId,
  b.GrossRevenue,b.EngineFromFinalURL,b.CampaignFromFinalUrl
 from 
  min_leads as a
inner join 
 (select * from sales_fact where grossrevenue <>0 ) as b
 on a.leadid=b.leadid 
 and a.FirstBookingDate=b.BookingCreateDate
)
;

"""


conn.execute(sql2)
conn.commit()

这是我得到的错误。

<ipython-input-42-bc72021206b2> in <module>()
     45 
     46 
---> 47 c.execute(sql2)
     48 conn.commit()
     49 

OperationalError: near "(": syntax error

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

CREATE TABLE ... AS ...需要查询,而不是子查询,因此您必须删除最外面的括号:

> CREATE TABLE t AS (SELECT 1);
Error: near "(": syntax error
> CREATE TABLE t AS SELECT 1;