Solved

Subtracting columns in different tables

Posted on 2014-04-01
5
211 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
Comment Utility
Notice the green arrow
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Awesome. THANK YOU!!!!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

15 Experts available now in Live!

Get 1:1 Help Now