0

I am running some query and I want to store the output of the query as new table. I am using Jupyter notebook where I have connected to a sqlite database which has the table I am using in the query.

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()

Here is the error I am getting.

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

OperationalError: near "(": syntax error

Any idea?

5
  • You really should use a ORM such as sqlalchemy. It will make your life so much simpler. Commented Jan 25, 2016 at 23:21
  • Any suggestions how to use that with sqlite? Commented Jan 25, 2016 at 23:23
  • You have the correct amount of opening and closing () symbols, but I think you need an extra closing one and opening one somewhere. Commented Jan 25, 2016 at 23:40
  • @Manish its not really something I can sum up in a post. It works the same with all versions of sql. Read the docs, they are really good and will be well with your time Commented Jan 25, 2016 at 23:42
  • The error is only coming when I use create table syntax. Its not there without that. But I do need to create a new table. And am not sure what is wrong here. Everything matches. Commented Jan 26, 2016 at 5:22

1 Answer 1

1

CREATE TABLE ... AS ... requires a query, not a subquery, so you have to remove the outermost pair of parentheses:

> CREATE TABLE t AS (SELECT 1);
Error: near "(": syntax error
> CREATE TABLE t AS SELECT 1;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.