05-04-2011, 12:49 PM | #1 |
Enthusiast
58
Rep 20
Posts |
Need help with SQL query. Any IS people please help.
I'm making a database in Access for a ficticious company for my final project in my Business Database class. The query needs to sum the PayableAmount in the Claims table for each employee then subtract it from the MaxAnnBenAmt in the Plans table thats related to employee. The Employee has a status which corisponds with ClassElidgability for each plan.
The query i have written so far that gives a syntax error is: SELECT Claims.EmployeeId, SUM((SUM(Claims.PayableAmount)) - (SUM(Plans.MaxAnnBenAmt)) FROM Employees INNER JOIN Claims on Employees.EmployeeId = Claims.EmployeeId INNER JOIN Plans on Claims.PlanName = Plans.PlanName GROUP BY Claims.EmployeeId Here is the ERD for the database: Just saw the ERD is not up to date. Claims also has EmployeeID as a FK, DateofService, and PlanName as a FK.
__________________
"Life is risky business" ~Schirmnov, Oleg
Last edited by dasReaper; 06-27-2012 at 02:58 AM.. |
05-04-2011, 12:57 PM | #2 |
Major
116
Rep 1,311
Posts
Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago
|
I think you need to give the sum function/column a name.
So try something like: SELECT Claims.EmployeeId, (SUM((SUM(Claims.PayableAmount)) - (SUM(Plans.MaxAnnBenAmt))) net_payable and you might have to add the "net_payable" column to the group by |
Appreciate
0
|
05-04-2011, 01:00 PM | #4 |
Captain
188
Rep 962
Posts |
so specifically:
SELECT Claims.EmployeeId, SUM(Claims.PayableAmount) - SUM(Plans.MaxAnnBenAmt) FROM Employees INNER JOIN Claims on Employees.EmployeeId = Claims.EmployeeId INNER JOIN Plans on Claims.PlanName = Plans.PlanName GROUP BY Claims.EmployeeId |
Appreciate
0
|
05-04-2011, 01:03 PM | #5 |
Major
116
Rep 1,311
Posts
Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago
|
but doesnt it still need a name, i think thats where the syntax error is coming in
|
Appreciate
0
|
05-04-2011, 01:04 PM | #6 | |
Captain
188
Rep 962
Posts |
Quote:
I also have no idea how the Access handles bad errors, but you can't do sums on sums, so that could be getting read out as a 'syntax' error |
|
Appreciate
0
|
05-04-2011, 01:07 PM | #7 |
Major
116
Rep 1,311
Posts
Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago
|
Cuz when I write query in SQL Server, it will cause a syntax error if there is no name. maybe its different when writing SQL in access
|
Appreciate
0
|
05-04-2011, 01:09 PM | #8 | |
Captain
188
Rep 962
Posts |
Quote:
I just ran this: select sum (fe_charmax)+ SUM(fe_data_type) from tblfields group by fe_id With no problems |
|
Appreciate
0
|
05-04-2011, 01:13 PM | #9 |
Major
116
Rep 1,311
Posts
Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago
|
I'm using SQL Server 2008. I just tried removing the names of a sum function and it didnt run saying "No column name was specified" and when I added the name it ran
|
Appreciate
0
|
05-04-2011, 01:13 PM | #10 |
Brigadier General
154
Rep 4,528
Posts
Drives: M2 CS
Join Date: Aug 2009
Location: Hollywood, FL
|
havent done SQL in forever but this looks right to me
|
Appreciate
0
|
05-04-2011, 01:15 PM | #11 | |
Captain
188
Rep 962
Posts |
Quote:
You're saying you get an error if you do something as stupid as: select SUM(1+1) from sys.all_views ? Well OP, your initial issue is definitely the sums of sums. secondary might be the naming convention. between the 2 you should be golden |
|
Appreciate
0
|
05-04-2011, 01:18 PM | #12 | |
Major
116
Rep 1,311
Posts
Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago
|
Quote:
|
|
Appreciate
0
|
05-04-2011, 01:21 PM | #13 |
Captain
188
Rep 962
Posts |
|
Appreciate
0
|
05-04-2011, 01:23 PM | #14 |
Major
116
Rep 1,311
Posts
Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago
|
It was just a sum of two columns. Maybe its something to do with the tables in the databases here at work.
|
Appreciate
0
|
05-04-2011, 01:24 PM | #15 |
Captain
188
Rep 962
Posts |
|
Appreciate
0
|
05-04-2011, 01:34 PM | #16 |
Major
116
Rep 1,311
Posts
Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago
|
|
Appreciate
0
|
05-04-2011, 01:43 PM | #17 | |
Captain
188
Rep 962
Posts |
Quote:
It has to be a setting somewhere. regardless it's bad practice to not have an alias |
|
Appreciate
0
|
05-04-2011, 01:45 PM | #18 |
Major
116
Rep 1,311
Posts
Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago
|
yeah, i would put names anyways cuz it makes it much cleaner and easier to work with
|
Appreciate
0
|
05-04-2011, 01:47 PM | #19 |
Captain
188
Rep 962
Posts |
hell in it's current state I don't think it'd even be useable by access (or anything). when it DOES run in sql for me, it has no column name. Thus there's no way to reference it.... I'm hoping the OP already thought about that
|
Appreciate
0
|
05-04-2011, 01:53 PM | #20 |
Major
116
Rep 1,311
Posts
Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago
|
Exactly. That's why i put names anyways. I hate having columns with no names.
|
Appreciate
0
|
05-04-2011, 01:54 PM | #21 |
Enthusiast
58
Rep 20
Posts |
Thanks for the help guys. I finally got it to work.
SQL code was: SELECT Claims.EmployeeID, Plans.ClassEligibility, SUM((Plans.MaxAnnBenAmt) - (Claims.PayableAmount)) AS Remaining_Benefit FROM (Employers INNER JOIN (Employees INNER JOIN Claims ON Employees.EmployeeID = Claims.EmployeeID) ON Employers.EmployerID = Employees.EmployerID) INNER JOIN Plans ON (Plans.PlanName = Claims.PlanName) AND (Employers.EmployerID = Plans.EmployerID) GROUP BY Claims.EmployeeID, Plans.ClassEligibility;
__________________
"Life is risky business" ~Schirmnov, Oleg
|
Appreciate
0
|
Post Reply |
Bookmarks |
|
|