Solved

Subtracting columns in different tables

Posted on 2014-04-01
5
212 Views
Last Modified: 2014-04-02
We are building a benefit time tracking app. I simply want to sum a column in the TimeUsed table and  subtract it from the sum of a column in the TimeEarned table based on StaffMemeberID.

The following querie works when executed in SQL Management Studio (2008), but when it is pasted into Visual Studio 2012's Data Source wizard it only returns the results from the first select statement:

Select (Select SUM(VacationEarned)
from TimeEarned
Where StaffMemberID=3)
- (Select SUM(VacationUsed)
from TimeUsed where StaffMemberID=3)As [Vacation Left]

Select(Select SUM(PersonalEarned)
from TimeEarned
Where StaffMemberID=3)
- (Select SUM(PersonalUsed)
from TimeUsed where StaffMemberID=3)As [Personal Left]

Select(Select SUM(SickEarned)
from TimeEarned
Where StaffMemberID=3)
- (Select SUM(SickUsed)
from TimeUsed where StaffMemberID=3)As [Sick Left]

Any help will be appreciated
0
Comment
Question by:ICantSee
  • 3
5 Comments
 

Author Comment

by:ICantSee
ID: 39970176
Notice the green arrow
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 39970388
Not sure why is it nor returning the result of next 2 SQL statements. But you can actually combine all 3 queries into one query.
SELECT VacationEarned-VacationUsed [Vacation Left],
       PersonalEarned-PersonalUsed [Personal Left],
       SickEarned-SickUsed [Sick Left]
FROM
  (SELECT StaffMemberID,
          SUM(VacationEarned) VacationEarned ,
          SUM(PersonalEarned) PersonalEarned,
          SUM(SickEarned) SickEarned
   FROM TimeEarned
   WHERE StaffMemberID=3
   GROUP BY StaffMemberID ) TE
JOIN
  (SELECT StaffMemberID,
          SUM(VacationUsed) VacationUsed ,
          SUM(PersonalUsed) PersonalUsed,
          SUM(SickUsed) SickUsed
   FROM TimeUsed
   WHERE StaffMemberID=3
   GROUP BY StaffMemberID ) TU ON TE.StaffMemberID = TU.StaffMemberID

Open in new window

0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39970697
This is all you need:

SELECT
    SUM(VacationEarned) - SUM(VacationUsed) AS [Vacation Left],
    SUM(PersonalEarned) - SUM(PersonalUsed) AS [Personal Left],
    SUM(SickEarned) - SUM(SickUsed) AS [SickLeft]
FROM TimeEarned
WHERE
    StaffMemberID=3
0
 

Author Comment

by:ICantSee
ID: 39971950
ScottPletcher

thank you for your answer. It doesn't work because VacationUsed, SickUsed, PersonalUsed does not come from TimeEarned. The code throws the error accordingly.

Error
0
 

Author Closing Comment

by:ICantSee
ID: 39971956
Awesome. THANK YOU!!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

896 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

12 Experts available now in Live!

Get 1:1 Help Now