[英]Return SUM(column) from a table and SUM(column) from another table
I have two tables called tblSalary
and tblDailyExpenses
.我有两个名为
tblSalary
和tblDailyExpenses
表。 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
)和支付费用Amount
( Amount
)。
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 ![]() |
Daily Expenses![]() |
1200 ![]() |
2 ![]() |
Salary Paid![]() |
7800 ![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.