SQL Error

Hi I am getting an error in this SQL statement "Syntax error near 'air atic'"

SELECT  [Performance].[Shaft], Cast([Performance].[Date] As Date) As [Date], Sum([Performance].[Drilled Total]) AS Metres,[Shafts].[Target] As Target
FROM [Performance] Inner Join [Shafts] On [Performance].[Shaft] = [Shafts].[Shaft]
Where [Date] >= [Shafts].[From] And [Date] <= '05 Nov 2015'
GROUP BY [Performance].[Shaft],[Performance].[Date],[Shafts].[Target]
HAVING [Performance].[Shaft] = 'air atic'
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Jim HornMicrosoft SQL Server Data DudeCommented:
HAVING is for filitering on an aggregate, such as HAVING SUM(salary) > 1000000, and since the above query uses HAVING with a scalar value then it should be in the WHERE clause.
SELECT  [Performance].[Shaft], Cast([Performance].[Date] As Date) As [Date], Sum([Performance].[Drilled Total]) AS Metres,[Shafts].[Target] As Target 
FROM [Performance] Inner Join [Shafts] On [Performance].[Shaft] = [Shafts].[Shaft] 
Where [Date] >= [Shafts].[From] And [Date] <= '05 Nov 2015' 
   AND [Performance].[Shaft] = 'air atic'
GROUP BY [Performance].[Shaft],[Performance].[Date],[Shafts].[Target] 

Open in new window

Vitor MontalvãoMSSQL Senior EngineerCommented:
The HAVING clause expects an aggregate function and you don't have one. Just move that criteria to the WHERE clause:
SELECT  [Performance].[Shaft], Cast([Performance].[Date] As Date) As [Date], Sum([Performance].[Drilled Total]) AS Metres,[Shafts].[Target] As Target 
 FROM [Performance] Inner Join [Shafts] On [Performance].[Shaft] = [Shafts].[Shaft] 
 WHERE [Date] >= [Shafts].[From] And [Date] <= '05 Nov 2015' 
    AND [Performance].[Shaft] = 'air atic' 
 GROUP BY [Performance].[Shaft],[Performance].[Date],[Shafts].[Target] 

Open in new window

Jim HornMicrosoft SQL Server Data DudeCommented:
btw I cleaned up your code below, making it much easier to read.
Used table aliases p and s instead of the full table names.
Removed all square brackets except for [Drilled Total] where the space requires it, and [Date] as that is also a column data type name.  Highly recommend not using spaces and data type names as column names
Changed the date WHERE value to 20151105 to prevent any date settings from throwing an error.
SELECT p.Shaft, Cast(p.[Date] As Date) As Date, Sum(p.[Drilled Total]) AS Metres, s.Target As Target 
FROM Performance p
   JOIN Shafts s On p.Shaft = s.Shaft 
WHERE p.[Date] >= s.From And p.[Date] <= '20151105' 
   AND p.Shaft = 'air atic'
GROUP BY p.Shaft, p.Date, s.Target

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
I had already tried that. I get the same error with the followingL

SELECT  [Performance].[Shaft], Cast([Performance].[Date] As Date) As [Date], Sum([Performance].[Drilled Total]) AS Metres,[Shafts].[Target] As Target
FROM [Performance] Inner Join [Shafts] On [Performance].[Shaft] = [Shafts].[Shaft]
Where [Date] >= [Shafts].[From] And [Date] <= '05 Nov 2015' And [Performance].[Shaft] = 'air atic' GROUP BY [Performance].[Shaft],[Performance].[Date],[Shafts].[Target
Jim HornMicrosoft SQL Server Data DudeCommented:
What is the data type of performance.date?
Vitor MontalvãoMSSQL Senior EngineerCommented:
The query runs without the [Performance].[Shaft] = 'air atic' filter?
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks Jim. I get the error incorrect syntax near 'air_atic'.Incorrect syntax near the keyword 'From'.
Jim HornMicrosoft SQL Server Data DudeCommented:
Again, what is the data type of performance.date, and while we're at it s.From, as they have to be the same data type for the WHERE clause to compare the two.

Also if p.Date is not a date to begin with, doing the p.[Date] <= {a date} will likely either fail or not work as it comapres as a varchar and not a date value.

The CAST of p.Date looks suspect, try this..

SELECT p.Shaft, Cast(p.[Date] As date) as the_date, Sum(p.[Drilled Total]) AS Metres, s.Target As Target
FROM Performance p
   JOIN Shafts s On p.Shaft = s.Shaft
WHERE CAST(p.[Date] as date) >= s.From And CAST(p.[Date] as date) <= '20151105'
   AND p.Shaft = 'air atic'
GROUP BY p.Shaft, p.Date, s.Target

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
Vitor MontalvãoMSSQL Senior EngineerCommented:
Looks like your GROUP BY isn't well formed:
SELECT  [Performance].[Shaft], 
        Cast([Performance].[Date] As Date) As [Date], 
        Sum([Performance].[Drilled Total]) AS Metres,
        [Shafts].[Target] As Target 
FROM [Performance] 
    Inner Join [Shafts] On [Performance].[Shaft] = [Shafts].[Shaft] 
WHERE [Date] >= [Shafts].[From] And [Date] <= '05 Nov 2015' And [Performance].[Shaft] = 'air atic' GROUP BY [Performance].[Shaft],Cast([Performance].[Date] As Date) As [Date], [Shafts].[Target 

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi Vitor. For some reason I still get the same error even without that there
Strange
Vitor MontalvãoMSSQL Senior EngineerCommented:
Really strange.
Can you post a print screen of your query window with the error message?
Don't forget to protect sensitive data as server and user name.
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thank you both very much
Vitor MontalvãoMSSQL Senior EngineerCommented:
So, what was the issue?
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

From novice to tech pro — start learning today.