Solved

Group By & Sum MS SQL Query

Posted on 2012-04-05
6
601 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
[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
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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

688 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