Solved

Subtracting columns in different tables

Posted on 2014-04-01
5
213 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:Scott Pletcher
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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