简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM