Solved

Group By & Sum MS SQL Query

Posted on 2012-04-05
6
585 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

19 Experts available now in Live!

Get 1:1 Help Now