Solved

Group By & Sum MS SQL Query

Posted on 2012-04-05
6
590 Views
Last Modified: 2012-06-27
I am not sure if I am doing this right.. but I want to write a query that can do a couple things..

I have a table with Employee ID, Date/Time and Charge amount for an employee Cafeteria.

I need to be able to query the database between two dates, say 3/1/12 and 3/15/12 and show the charges for Employee ID 1 and sum the total of purchases..

The Query to get the total is where I am running into issues.. to me this should work but it does not.

SELECT     PAYROLL.dbo.EMPLOYEE.ID, PAYROLL.dbo.EMPLOYEE.LAST_NAME, PAYROLL.dbo.EMPLOYEE.FIRST_NAME, 
                      SUM(EMP_DEDUCTIONS.CHARGE_AMOUNT) AS Total
FROM         PAYROLL.dbo.EMPLOYEE RIGHT OUTER JOIN
                      EMP_DEDUCTIONS ON PAYROLL.dbo.EMPLOYEE.ID = EMP_DEDUCTIONS.EMP_ID
GROUP BY PAYROLL.dbo.EMPLOYEE.ID, PAYROLL.dbo.EMPLOYEE.LAST_NAME, PAYROLL.dbo.EMPLOYEE.FIRST_NAME, EMP_DEDUCTIONS.VOID
HAVING      (EMP_DEDUCTIONS.VOID IS NULL) AND (PAYROLL.dbo.EMPLOYEE.ID = 'BEA01') AND (CONVERT(varchar, EMP_DEDUCTIONS.CHARGE_DATETIME, 
                      101) BETWEEN '3/1/2012' AND '3/15/2012')
ORDER BY PAYROLL.dbo.EMPLOYEE.ID

Open in new window


I get this error

Msg 8121, Level 16, State 1, Line 6
Column 'EMP_DEDUCTIONS.CHARGE_DATETIME' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8121, Level 16, State 1, Line 6
Column 'EMP_DEDUCTIONS.CHARGE_DATETIME' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.

This will ultimately be written for a web page so the employee can run a report on their purchases and get the total.

Thanks for any help.
0
Comment
Question by:DMTechGrooup
6 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37813981
add CHARGE_DATETIME to the group by clause:

SELECT     PAYROLL.dbo.EMPLOYEE.ID, PAYROLL.dbo.EMPLOYEE.LAST_NAME, PAYROLL.dbo.EMPLOYEE.FIRST_NAME, 
                      SUM(EMP_DEDUCTIONS.CHARGE_AMOUNT) AS Total
FROM         PAYROLL.dbo.EMPLOYEE RIGHT OUTER JOIN
                      EMP_DEDUCTIONS ON PAYROLL.dbo.EMPLOYEE.ID = EMP_DEDUCTIONS.EMP_ID
GROUP BY PAYROLL.dbo.EMPLOYEE.ID, PAYROLL.dbo.EMPLOYEE.LAST_NAME, PAYROLL.dbo.EMPLOYEE.FIRST_NAME, EMP_DEDUCTIONS.VOID, EMP_DEDUCTIONS.CHARGE_DATETIME 
HAVING      (EMP_DEDUCTIONS.VOID IS NULL) AND (PAYROLL.dbo.EMPLOYEE.ID = 'BEA01') AND (CONVERT(varchar, EMP_DEDUCTIONS.CHARGE_DATETIME, 
                      101) BETWEEN '3/1/2012' AND '3/15/2012')
ORDER BY PAYROLL.dbo.EMPLOYEE.ID

Open in new window

0
 
LVL 26

Expert Comment

by:tigin44
ID: 37814095
try this

SELECT     PAYROLL.dbo.EMPLOYEE.ID, PAYROLL.dbo.EMPLOYEE.LAST_NAME, PAYROLL.dbo.EMPLOYEE.FIRST_NAME,
                      SUM(EMP_DEDUCTIONS.CHARGE_AMOUNT) AS Total
FROM         PAYROLL.dbo.EMPLOYEE RIGHT OUTER JOIN
                      EMP_DEDUCTIONS ON PAYROLL.dbo.EMPLOYEE.ID = EMP_DEDUCTIONS.EMP_ID
WHERE      (EMP_DEDUCTIONS.VOID IS NULL)
  AND (PAYROLL.dbo.EMPLOYEE.ID = 'BEA01')
  AND (CONVERT(varchar, EMP_DEDUCTIONS.CHARGE_DATETIME, 101) BETWEEN '3/1/2012' AND '3/15/2012')
GROUP BY PAYROLL.dbo.EMPLOYEE.ID, PAYROLL.dbo.EMPLOYEE.LAST_NAME, PAYROLL.dbo.EMPLOYEE.FIRST_NAME, EMP_DEDUCTIONS.VOID
ORDER BY PAYROLL.dbo.EMPLOYEE.ID
0
 
LVL 24

Author Comment

by:DMTechGrooup
ID: 37814124
Trying this

SELECT     PAYROLL.dbo.EMPLOYEE.ID, PAYROLL.dbo.EMPLOYEE.LAST_NAME, PAYROLL.dbo.EMPLOYEE.FIRST_NAME, 
                      SUM(EMP_DEDUCTIONS.CHARGE_AMOUNT) AS Total
FROM         PAYROLL.dbo.EMPLOYEE RIGHT OUTER JOIN
                      EMP_DEDUCTIONS ON PAYROLL.dbo.EMPLOYEE.ID = EMP_DEDUCTIONS.EMP_ID
WHERE      (EMP_DEDUCTIONS.VOID IS NULL) 
  AND (PAYROLL.dbo.EMPLOYEE.ID = 'BEA01') 
  AND (CONVERT(varchar, EMP_DEDUCTIONS.CHARGE_DATETIME, 101) BETWEEN '3/1/2012' AND '3/15/2012')
GROUP BY PAYROLL.dbo.EMPLOYEE.ID, PAYROLL.dbo.EMPLOYEE.LAST_NAME, PAYROLL.dbo.EMPLOYEE.FIRST_NAME, EMP_DEDUCTIONS.VOID
ORDER BY PAYROLL.dbo.EMPLOYEE.ID

Open in new window


I don't get an error but I also get no results.  Same with just adding the other to the group by, still no results.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 9

Accepted Solution

by:
OCDan earned 250 total points
ID: 37814153
Try this mate:
SELECT     PAYROLL.dbo.EMPLOYEE.ID,
               PAYROLL.dbo.EMPLOYEE.LAST_NAME,
               PAYROLL.dbo.EMPLOYEE.FIRST_NAME,
               SUM(EMP_DEDUCTIONS.CHARGE_AMOUNT) AS Total
FROM  PAYROLL.dbo.EMPLOYEE
RIGHT OUTER JOIN EMP_DEDUCTIONS ON PAYROLL.dbo.EMPLOYEE.ID = EMP_DEDUCTIONS.EMP_ID
WHERE  EMP_DEDUCTIONS.VOID IS NULL
  AND PAYROLL.dbo.EMPLOYEE.ID = 'BEA01'
  AND EMP_DEDUCTIONS.CHARGE_DATETIME BETWEEN '2012-03-01 00:00:00' AND '2012-03-15 23:59:59')
 
GROUP BY PAYROLL.dbo.EMPLOYEE.ID, PAYROLL.dbo.EMPLOYEE.LAST_NAME, PAYROLL.dbo.EMPLOYEE.FIRST_NAME, EMP_DEDUCTIONS.VOID
ORDER BY PAYROLL.dbo.EMPLOYEE.ID
0
 
LVL 26

Assisted Solution

by:tigin44
tigin44 earned 250 total points
ID: 37814179
so try this

SELECT     PAYROLL.dbo.EMPLOYEE.ID, PAYROLL.dbo.EMPLOYEE.LAST_NAME, PAYROLL.dbo.EMPLOYEE.FIRST_NAME,
                      SUM(EMP_DEDUCTIONS.CHARGE_AMOUNT) AS Total
FROM         PAYROLL.dbo.EMPLOYEE RIGHT OUTER JOIN
                      EMP_DEDUCTIONS ON PAYROLL.dbo.EMPLOYEE.ID = EMP_DEDUCTIONS.EMP_ID
WHERE      (EMP_DEDUCTIONS.VOID IS NULL)
  AND (PAYROLL.dbo.EMPLOYEE.ID = 'BEA01')
  AND (CONVERT(date, EMP_DEDUCTIONS.CHARGE_DATETIME, 101) BETWEEN '3/1/2012' AND '3/15/2012')
GROUP BY PAYROLL.dbo.EMPLOYEE.ID, PAYROLL.dbo.EMPLOYEE.LAST_NAME, PAYROLL.dbo.EMPLOYEE.FIRST_NAME, EMP_DEDUCTIONS.VOID
ORDER BY PAYROLL.dbo.EMPLOYEE.ID
0
 
LVL 24

Author Closing Comment

by:DMTechGrooup
ID: 37814356
Last two worked like a charm.. now I will look it over to see where I need to adjust.. thanks for the help.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now