improper syntax near ORDER

I have an error in my query improper syntax near 'ORDER' expecting ')', UNION or EXCEPT and same for AS

 USE firetalk_test
 IF OBJECT_ID('Temp_x', 'U') IS NOT NULL
 DROP Table Temp_x; 
 CREATE Table Temp_x AS( <-----------------------------------------------HERE
 SELECT Job.Jobid,         
 Job.company,         
 Job.rig,         
 Job.rignumber,         
 Job.wellname,         
 Job.onlocationdatetime,         
 Job.offlocationdatetime,         
 sfrentaldays * sfrentalrate + wfrentaldays * wfrentalrate + rigupcharge +          
 rigdowncharge + numberoftools * toolredresscharge +          
 sfinsurancedays * sfinsurancerate + wfinsurancedays * wfinsurancerate +          
 servicemandays * servicemanrate - creditdays * creditrate + othercharges + 
 SFStandByDays * SFStandByRate + WFStandByDays * WFStandByRate  AS   Total   
 FROM   Job          
 INNER JOIN JobRentalTicket ON Job.Jobid = JobRentalTicket.Jobid   
 WHERE  ( ( ( Job.Jobcomplete ) = 1 ) AND ( ( JobRentalTicket.invictussentinvoicetoagent ) = 1 ) )          
 OR  ( ( ( Job.Jobcomplete ) = 1 ) AND ( ( JobRentalTicket.InvoicePaid ) = 0 ) )            
 AND Job.offlocationdatetime BETWEEN  '2015-02-01 00:00:00' AND '2015-02-28 23:59:59' 
  ORDER BY Job.company - <-----------------------------------------------------------------------------------------here
  );         
 Select * From Temp_x ORDER BY company ASC 

Open in new window

LVL 6
r3nderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:
One too many right parenthesis.  :)

Remove the last one (right before the terminating colon).


Good Luck!
Kent
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
For starters...

1.

What's the purpose of DROP/CREATE, when you can TRUNCATE TABLE to remove all rows, then avoid having to re-define the schema of the table?

2.

Your code would be much more readable if it were indented.  

3.

For the columns that have both * and +, be VERY careful that the order of precedence is exactly what you want, meaning use parenteses marks ( ) to state the order, such as (2 * 3) + 4 = 10, but 2 + (3 * 4) = 24.

4.

Way to many unnecessary parenteses marks in the WHERE clause.  Was this query designed by a wizard?  

5.

AND Job.offlocationdatetime BETWEEN  '2015-02-01 00:00:00' AND '2015-02-28 23:59:59'

   Try this (See Beware of Between)
   Job.offlocationdatetime >='2015-02-01' AND Job.offlocationdatetime < '2015-03-01'

6.

It's really not necessary to add an ORDER BY to the statement that populates a table, when your return set has the same ORDER BY.  I'd get rid of the ORDER BY in the first statement.
0
ste5anSenior DeveloperCommented:
The CREATE TABLE syntax with the sub-query approach is incorrect. For this case exists

SELECT columnlist INTO tableToCreate FROM table;

Open in new window


E.g.

 USE firetalk_test;
 GO
 
 IF OBJECT_ID('Temp_x', 'U') IS NOT NULL
    DROP TABLE Temp_x; 
 
 SELECT J.Jobid ,
        J.company ,
        J.rig ,
        J.rignumber ,
        J.wellname ,
        J.onlocationdatetime ,
        J.offlocationdatetime ,
        sfrentaldays * sfrentalrate + wfrentaldays * wfrentalrate + rigupcharge + rigdowncharge + numberoftools * toolredresscharge + sfinsurancedays
        * sfinsurancerate + wfinsurancedays * wfinsurancerate + servicemandays * servicemanrate - creditdays * creditrate + othercharges + SFStandByDays
        * SFStandByRate + WFStandByDays * WFStandByRate AS Total
 INTO   Temp_x
 FROM   Job J
        INNER JOIN JobRentalTicket JRT ON J.Jobid = JRT.Jobid
 WHERE  ( J.Jobcomplete = 1
          AND JRT.invictussentinvoicetoagent = 1
        )
        OR ( J.Jobcomplete = 1
             AND JRT.InvoicePaid = 0
           )
        AND J.offlocationdatetime BETWEEN '2015-02-01 00:00:00' AND '2015-02-28 23:59:59'
 ORDER BY J.company; 
 
 SELECT *
 FROM   Temp_x
 ORDER BY company ASC;

Open in new window


Please qualify all columns with the table name or table alias. I'd prefer also using always an alias.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Vitor MontalvãoMSSQL Senior EngineerCommented:
You can't use the ORDER BY in the CREATE TABLE statement. Also as Jim said, you won't need it since the SELECT has an ORDER BY clause.
Even if you want to use the ORDER BY clause then you need to get rid of the CREATE TABLE statement and change the SELECT to a SELECT INTO statement:
 USE firetalk_test

SELECT Job.Jobid,         
 Job.company,         
 Job.rig,         
 Job.rignumber,         
 Job.wellname,         
 Job.onlocationdatetime,         
 Job.offlocationdatetime,         
 sfrentaldays * sfrentalrate + wfrentaldays * wfrentalrate + rigupcharge +          
 rigdowncharge + numberoftools * toolredresscharge +          
 sfinsurancedays * sfinsurancerate + wfinsurancedays * wfinsurancerate +          
 servicemandays * servicemanrate - creditdays * creditrate + othercharges + 
 SFStandByDays * SFStandByRate + WFStandByDays * WFStandByRate  AS   Total   
INTO Temp_x ----------------------------------------------------------------------------------------> Creates a new table
 FROM   Job          
 INNER JOIN JobRentalTicket ON Job.Jobid = JobRentalTicket.Jobid   
 WHERE  ( ( ( Job.Jobcomplete ) = 1 ) AND ( ( JobRentalTicket.invictussentinvoicetoagent ) = 1 ) )          
 OR  ( ( ( Job.Jobcomplete ) = 1 ) AND ( ( JobRentalTicket.InvoicePaid ) = 0 ) )            
 AND Job.offlocationdatetime BETWEEN  '2015-02-01 00:00:00' AND '2015-02-28 23:59:59' 
ORDER BY Job.company 

 Select * From Temp_x ORDER BY company ASC 

Open in new window

0
ste5anSenior DeveloperCommented:
okay, in which SQL dialect is this correct syntax:

CREATE TABLE tablename AS ( SELECT columnlist FROM tableexpression );

Open in new window


???
0
r3nderAuthor Commented:
thank you ste5an
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
okay, in which SQL dialect is this correct syntax:
Oracle :)
0
ste5anSenior DeveloperCommented:
OMG =8)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.