Kamiran
Kamiran

Reputation: 155

Return SUM(column) from a table and SUM(column) from another table

I have two tables called tblSalary and tblDailyExpenses. In tblSalary, I have a column called PaydAmount that keeps the salary amount paid per employee. In tblDailyExpenses, I have a column called Amount that keeps the paid amount per invoice.

I want to return the total paid salary (PaydAmount) and the total amount of paid expenses (Amount) in a month.

I have tried this query:

SELECT
(
   (SELECT SUM(PaydAmount) FROM tblSalary WHERE tblSalary.Month=4) AS PaydSalary,
   (SELECT SUM(Amount) FROM tblDailyExpenses WHERE tblDailyExpenses.Month=4) AS PaydExpenses
)

I am using Sqlserver Compact edition where I want to return it in a data grid view as shown below:

#| Type           | Amount
-------------------------
1| Daily Expenses |  1200
-------------------------
2| Salary Paid    |  7800

Upvotes: 1

Views: 71

Answers (1)

Timothy G.
Timothy G.

Reputation: 9055

I actually think a UNION would be better to use here. You can do something like this:

SELECT 'Daily Expenses' AS Type, SUM(PaydAmount) AS Amount
FROM tblSalary 
WHERE tblSalary.Month=4

UNION

SELECT 'Salary Paid' AS Type, SUM(Amount) AS Amount
FROM tblDailyExpenses 
WHERE tblDailyExpenses.Month=4

This should yield what you are looking for in your data grid view below:

# Type Amount
1 Daily Expenses 1200
2 Salary Paid 7800

Upvotes: 1

Related Questions