我有两个名为tblSalary
和tblDailyExpenses
的表。在tblSalary
中,我有一个名为PaydAmount
的列,用于保留每位员工支付的工资金额。在tblDailyExpenses
中,我有一个名为Amount
的列,用于保留每张发票的付费金额。
我想在一个月内返回付费总薪资(PaydAmount
)和付费总额(Amount
)。
我试过这个问题:
SELECT
(
(SELECT SUM(PaydAmount) FROM tblSalary WHERE tblSalary.Month=4) AS PaydSalary,
(SELECT SUM(Amount) FROM tblDailyExpenses WHERE tblDailyExpenses.Month=4) AS PaydExpenses
)
我正在使用Sqlserver Compact版 我想在数据网格视图中返回它,如下所示:
#| Type | Amount
-------------------------
1| Daily Expenses | 1200
-------------------------
2| Salary Paid | 7800
答案 0 :(得分:1)
我实际上认为在这里使用UNION
会更好。你可以这样做:
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
这应该会在您的数据网格视图中产生您想要的内容:
#| Type | Amount
-------------------------
1| Daily Expenses | 1200
-------------------------
2| Salary Paid | 7800