[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Subtracting columns in different tables

Posted on 2014-04-01
5
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 

Author Comment

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

Accepted Solution

by:
Sharath earned 2000 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 70

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

656 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