简体   繁体   English

从一个表中返回 SUM(column) 和从另一个表中返回 SUM(column)

[英]Return SUM(column) from a table and SUM(column) from another table

I have two tables called tblSalary and tblDailyExpenses .我有两个名为tblSalarytblDailyExpenses表。 In tblSalary , I have a column called PaydAmount that keeps the salary amount paid per employee.tblSalary ,我有一个名为PaydAmount的列,用于保存每位员工支付的工资金额。 In tblDailyExpenses , I have a column called Amount that keeps the paid amount per invoice.tblDailyExpenses ,我有一个名为Amount的列,用于保存每张发票的支付金额。

I want to return the total paid salary ( PaydAmount ) and the total amount of paid expenses ( Amount ) in a month.我想返回PaydAmount支付的工资总额( PaydAmount )和支付费用AmountAmount )。

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:我正在使用 Sqlserver Compact 版本,我想在数据网格视图中返回它,如下所示:

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

I actually think a UNION would be better to use here.我实际上认为在这里使用UNION会更好。 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 1 Daily Expenses日常开支 1200 1200
2 2 Salary Paid工资支付 7800 7800

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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