I am trying to find a way to insert a calculated row into SQL query that is based off of a subtracted sum from a separate total
For example:
Name | Title | eID | Spend_Category |Jan|Feb|...|Nov|Dec|Sum of Months|
Jay | CEO | 1 | Lodging |10 |20 |...| 0 |30 | 60 |
Jay | CEO | 1 | Airlines |20 |40 |...| 0 |60 | 120 |
Jay | CEO | 1 | Auto |10 |20 |...| 0 |30 | 60 |
Paul | VP | 2 | Lodging |10 |20 |...| 0 |30 | 60 |
On a separate table, I have each employee ID's total spend. What I am trying to do is create a new row and for each user(eID) with a new Spend_Category of "Other" that sums up all of the different Spend for that user and subtracts it from the total. So if Jay had a total spend of $500
For example:
Name | Title | eID | Spend_Category |Jan|Feb|...|Nov|Dec|Sum of Months|
Jay | CEO | 1 | Lodging |10 |20 |...| 0 |30 | 60 |
Jay | CEO | 1 | Airlines |20 |40 |...| 0 |60 | 120 |
Jay | CEO | 1 | Auto |10 |20 |...| 0 |30 | 60 |
Jay | CEO | 1 | Other |...................|$500-240=$260|
Paul | VP | 2 | Lodging |10 |20 |...| 0 |30 | 60 |
...etc...
I am certainly open to ideas and I am very new to SQL. Here is where I am at thus far and would appreciate the group's expertise.
SELECT
SC.Full_Name,
TS.Job_Title,
TS.Card_Type,
SC.Employee_ID,
SC.Genesis_Industry,
SC.May,
SC.June,
SC.July,
SC.August,
SC.September,
SC.October,
SC.November,
SC.December,
SC.January,
SC.February,
SC.March,
SC.April,
(NZ(SC.May,0)+NZ(SC.June,0)+ NZ(SC.July,0)+ NZ(SC.August,0)+NZ(SC.September,0)+NZ(SC.October,0)+NZ(SC.November,0)+NZ(SC.December,0)+NZ(SC.January,0)+NZ(SC.February,0)+NZ(SC.March,0)+NZ(SC.April,0)) AS Category_Spend,
TS.Total_Spend
FROM Spend_Category SC
left join Top_Spender TS
on SC.Employee_ID=TS.Employee_ID