Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Group By Error

Posted on 2014-10-18
4
Medium Priority
?
170 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:Murray Brown
[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 1000 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:Murray Brown
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 1000 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:Murray Brown
ID: 40389211
Thank you both
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

722 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