Solved

SQL Group By Error

Posted on 2014-10-18
4
163 Views
Last Modified: 2014-10-18
I get the following error in the SQL Statement below

Incorrect syntax near the keyword 'Group'.

Select Phone, [First Name], [Machine operator],Sum([Drilled Total]) As [Total Drilled]
From Performance Inner Join People On [Machine operator] = [Operator COY]
Having Sum([Drilled Total]) > 100 And [Date] >= DATEADD(day,-30, getdate())
Group By Phone,[First Name],[Machine operator]
0
Comment
Question by:murbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
AielloJ earned 250 total points
ID: 40389000
murbro:

You're not allowed to have a non-aggregated expression ([Date] in this case) in a HAVING cluase.  Your [Date] expression must be specified in a WHERE clause.  Try the following:

SELECT
  [Phone],
  [First Name],
  [Machine operator],
  Sum([Drilled Total]) As [Total Drilled]
FROM
  Performance
 Inner Join
  People
 On [Machine operator] = [Operator COY]
WHERE
  {Date] >= DATEADD(day,-30, getdate())
HAVING
  Sum([Drilled Total]) > 100
GROUP BY
  [Phone],
  [First Name],
  [Machine operator]
 
I also suggest consistency in the use of the square brackets on column names as shown.

Best regards,

AielloJ
0
 

Author Comment

by:murbro
ID: 40389169
Thanks. I am still getting the following error
Incorrect syntax near the keyword 'GROUP'.
0
 
LVL 23

Assisted Solution

by:Snarf0001
Snarf0001 earned 250 total points
ID: 40389170
One modification from the above.  AielloJ is right, the non-grouped "date" column has to be in the where clause, not the having.
Only change, is "having" has to appear AFTER the group by:

Select Phone, [First Name], [Machine operator],Sum([Drilled Total]) As [Total Drilled]
From Performance Inner Join People On [Machine operator] = [Operator COY] 
Where [Date] >= DATEADD(day,-30, getdate())
Group By Phone,[First Name],[Machine operator] 
Having Sum([Drilled Total]) > 100

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 40389211
Thank you both
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS SQL Sever Import/export problem 7 47
WSUS Console loosed connectivity to the database. 13 39
Need help with a query 3 39
SQL Syntax 6 41
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question