Solved

Group By & Sum MS SQL Query

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

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 9

Accepted Solution

by:
OCDan earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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 …
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.​
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

9 Experts available now in Live!

Get 1:1 Help Now