BMW X3 Forum
BMW X3 Forum
Welcome to the ultimate G45 BMW X3 community.
BMW Garage BMW Meets Register Today's Posts
Post Reply
 
Thread Tools Search this Thread
      05-04-2011, 12:49 PM   #1
dasReaper
Enthusiast
United_States
58
Rep
20
Posts

Drives: '09 128i Black/Black
Join Date: Dec 2008
Location: Ohio

iTrader: (0)

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..
Appreciate 0
      05-04-2011, 12:57 PM   #2
SpacE82
Major
SpacE82's Avatar
United_States
116
Rep
1,311
Posts

Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago

iTrader: (0)

Garage List
2009 BMW 135i  [8.50]
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, 12:59 PM   #3
Freakazoid
Captain
Freakazoid's Avatar
188
Rep
962
Posts

Drives: Saturn Redline
Join Date: Dec 2009
Location: Pittsburgh

iTrader: (0)

why are you trying to do a sum on sums? I believe that's your issue.

Why can't you just subtract the 2 sums?
Appreciate 0
      05-04-2011, 01:00 PM   #4
Freakazoid
Captain
Freakazoid's Avatar
188
Rep
962
Posts

Drives: Saturn Redline
Join Date: Dec 2009
Location: Pittsburgh

iTrader: (0)

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
SpacE82
Major
SpacE82's Avatar
United_States
116
Rep
1,311
Posts

Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago

iTrader: (0)

Garage List
2009 BMW 135i  [8.50]
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
Freakazoid
Captain
Freakazoid's Avatar
188
Rep
962
Posts

Drives: Saturn Redline
Join Date: Dec 2009
Location: Pittsburgh

iTrader: (0)

Quote:
Originally Posted by SpacE82 View Post
but doesnt it still need a name, i think thats where the syntax error is coming in
I'm REALLY rusty on access, but in the 'real' database languages you don't need one (but you should have one).

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
SpacE82
Major
SpacE82's Avatar
United_States
116
Rep
1,311
Posts

Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago

iTrader: (0)

Garage List
2009 BMW 135i  [8.50]
Quote:
Originally Posted by Freakazoid View Post
I'm REALLY rusty on access, but in the 'real' database languages you don't need one (but you should have one).

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
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
Freakazoid
Captain
Freakazoid's Avatar
188
Rep
962
Posts

Drives: Saturn Redline
Join Date: Dec 2009
Location: Pittsburgh

iTrader: (0)

Quote:
Originally Posted by SpacE82 View Post
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
you sure about that? What version database are you using?

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
SpacE82
Major
SpacE82's Avatar
United_States
116
Rep
1,311
Posts

Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago

iTrader: (0)

Garage List
2009 BMW 135i  [8.50]
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
escobar929
Brigadier General
escobar929's Avatar
154
Rep
4,528
Posts

Drives: M2 CS
Join Date: Aug 2009
Location: Hollywood, FL

iTrader: (14)

Garage List
2020 M2 CS  [10.00]
2020 M240i  [0.00]
Quote:
Originally Posted by Freakazoid View Post
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
havent done SQL in forever but this looks right to me
Appreciate 0
      05-04-2011, 01:15 PM   #11
Freakazoid
Captain
Freakazoid's Avatar
188
Rep
962
Posts

Drives: Saturn Redline
Join Date: Dec 2009
Location: Pittsburgh

iTrader: (0)

Quote:
Originally Posted by SpacE82 View Post
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
very interesting, I'm running the same as well. Maybe it's a setting somewhere? I'm going to poke around., I've never had to give an alias to an aggregate function before...
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
SpacE82
Major
SpacE82's Avatar
United_States
116
Rep
1,311
Posts

Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago

iTrader: (0)

Garage List
2009 BMW 135i  [8.50]
Quote:
Originally Posted by Freakazoid View Post
very interesting, I'm running the same as well. Maybe it's a setting somewhere? I'm going to poke around., I've never had to give an alias to an aggregate function before...
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
Weird, because that works. Maybe it has something to do with joins?
Appreciate 0
      05-04-2011, 01:21 PM   #13
Freakazoid
Captain
Freakazoid's Avatar
188
Rep
962
Posts

Drives: Saturn Redline
Join Date: Dec 2009
Location: Pittsburgh

iTrader: (0)

Quote:
Originally Posted by SpacE82 View Post
Weird, because that works. Maybe it has something to do with joins?
I threw in a join for good measure and no problems, what's the query you're trying to run?
Appreciate 0
      05-04-2011, 01:23 PM   #14
SpacE82
Major
SpacE82's Avatar
United_States
116
Rep
1,311
Posts

Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago

iTrader: (0)

Garage List
2009 BMW 135i  [8.50]
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
Freakazoid
Captain
Freakazoid's Avatar
188
Rep
962
Posts

Drives: Saturn Redline
Join Date: Dec 2009
Location: Pittsburgh

iTrader: (0)

Quote:
Originally Posted by SpacE82 View Post
It was just a sum of two columns. Maybe its something to do with the tables in the databases here at work.
I'm extremely curious now haha. Gotta be something to do with the columns.

You're sure the columns are defined as ints right?
Appreciate 0
      05-04-2011, 01:34 PM   #16
SpacE82
Major
SpacE82's Avatar
United_States
116
Rep
1,311
Posts

Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago

iTrader: (0)

Garage List
2009 BMW 135i  [8.50]
Quote:
Originally Posted by Freakazoid View Post
I'm extremely curious now haha. Gotta be something to do with the columns.

You're sure the columns are defined as ints right?
Haha i know, its weird. I'm pretty sure they are ints, I havent changed their format.
Appreciate 0
      05-04-2011, 01:43 PM   #17
Freakazoid
Captain
Freakazoid's Avatar
188
Rep
962
Posts

Drives: Saturn Redline
Join Date: Dec 2009
Location: Pittsburgh

iTrader: (0)

Quote:
Originally Posted by SpacE82 View Post
Haha i know, its weird. I'm pretty sure they are ints, I havent changed their format.
looking online it looks like your error would be happening if you were creating a view, etc. which makes sense. but not for a simple select statement

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
SpacE82
Major
SpacE82's Avatar
United_States
116
Rep
1,311
Posts

Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago

iTrader: (0)

Garage List
2009 BMW 135i  [8.50]
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
Freakazoid
Captain
Freakazoid's Avatar
188
Rep
962
Posts

Drives: Saturn Redline
Join Date: Dec 2009
Location: Pittsburgh

iTrader: (0)

Quote:
Originally Posted by SpacE82 View Post
yeah, i would put names anyways cuz it makes it much cleaner and easier to work with
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
SpacE82
Major
SpacE82's Avatar
United_States
116
Rep
1,311
Posts

Drives: 2009 M-Sport 135i
Join Date: Mar 2009
Location: Chicago

iTrader: (0)

Garage List
2009 BMW 135i  [8.50]
Quote:
Originally Posted by Freakazoid View Post
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
Exactly. That's why i put names anyways. I hate having columns with no names.
Appreciate 0
      05-04-2011, 01:54 PM   #21
dasReaper
Enthusiast
United_States
58
Rep
20
Posts

Drives: '09 128i Black/Black
Join Date: Dec 2008
Location: Ohio

iTrader: (0)

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


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT -5. The time now is 07:20 PM.




x3:
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
1Addicts.com, BIMMERPOST.com, E90Post.com, F30Post.com, M3Post.com, ZPost.com, 5Post.com, 6Post.com, 7Post.com, XBimmers.com logo and trademark are properties of BIMMERPOST