Solved

SQL Group By Error

Posted on 2014-10-18
4
160 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
  • 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 22

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

776 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