Solved

SQL Group By Error

Posted on 2014-10-18
4
168 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 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: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 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:Murray Brown
ID: 40389211
Thank you both
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

626 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