简体   繁体   English

SQL/MS Access:在 SQL 表中添加一个自动更新的字段,其中包含另一个表中的值的总和

[英]SQL/MS Access: Adding a automatically updating field in SQL table with sum of values from another table

I am building a database with MS Access, and I am quite new to Access and SQL language.我正在用 MS Access 构建一个数据库,我对 Access 和 SQL 语言还很陌生。
I would like to add a field in a table which will automatically update its value based on the sum of values listed from another table.我想在表中添加一个字段,该字段将根据另一个表中列出的值的总和自动更新其值。



Example:例子:

There is a staff and a payroll table, and I would like to add a total field to the staff table, which is equal to the total amount of salary listed in the payroll table for each staff.有一个员工和一个工资表,我想在员工表中添加一个总计字段,它等于每个员工在工资表中列出的工资总额
With a step-by-step approach, I have managed to build a query which do the summation of salary for each staff.通过循序渐进的方法,我设法构建了一个查询,该查询对每个员工的工资进行汇总。


staff职员

| id |   name   |
|----|----------|
| A1 | staff A1 |
| A2 | staff A2 |
| B1 | staff B1 |

payroll工资单

| id | month | salary |
|----|-------|--------|
| A1 |  JAN  |  10000 |
| A2 |  JAN  |  15000 |
| A1 |  FEB  |  10000 |

summation求和

SELECT staff.id, SUM([payroll]![salary]) AS total
FROM payroll 
INNER JOIN staff ON staff.id = payroll.id
GROUP BY staff.id;

| id | total |
|----|-------|
| A1 | 20000 |
| A2 | 15000 |

Expected result: staff预期结果:员工

| id |   name   | total |
|----|----------| ----- |
| A1 | staff A1 | 20000 |
| A2 | staff A2 | 15000 |
| B1 | staff B1 |     0 |

Questions:问题:

On MS Access, I tried to add a lookup query to the total field in staff to automatic update the value, but if only gives me a combo box options for selection.在 MS Access 上,我尝试向人员中字段添加查找查询以自动更新值,但如果只给我一个组合框选项以供选择。
Any suggestions and solutions for this problem will be appreciated, thanks.对此问题的任何建议和解决方案将不胜感激,谢谢。

SELECT total
FROM summation
WHERE staff.id = summation.id

using your main table from STAFF table might work, like ;使用 STAFF 表中的主表可能会起作用,例如;

SELECT staff.id, SUM([payroll]![salary]) AS total
FROM staff 
INNER JOIN payroll ON staff.id = payroll.id
GROUP BY staff.id;

INNER JOIN means, you're summing two tables with common values which means If B1 doesn't exist into the payroll tables, it also will not appear in the relationship table. INNER JOIN意味着,您将两个具有共同值的表相加,这意味着如果B1不存在于payroll表中,它也不会出现在关系表中。

FULL OUTER JOIN will help you to query all the data from two tables. FULL OUTER JOIN将帮助您查询两个表中的所有数据。

Maybe this chart will explain what I mean;也许这张图表可以解释我的意思;

Maybe you need to use FULL OUTER JOIN .也许您需要使用FULL OUTER JOIN So, It has to go like that;所以,它必须这样;

SELECT staff.id, SUM([payroll]![salary]) AS total
FROM payroll 
FULL OUTER JOIN staff ON staff.id = payroll.id
GROUP BY staff.id;

OUTPUT输出

| id |   name   | total |
|----|----------| ----- |
| A1 | staff A1 | 20000 |
| A2 | staff A2 | 15000 |
| B1 | staff B1 |     0 |

Reference: SQL Joins参考: SQL 连接

You want a LEFT JOIN and NZ() :你想要一个LEFT JOINNZ()

SELECT s.id, NZ(SUM(p.salary)) AS total
FROM staff as s LEFT JOIN
     payroll as p
     ON s.id = p.id
GROUP BY s.id;

You can also write this using a correlated subquery.您也可以使用相关子查询来编写它。 This makes it easier to keep more columns from staff :这使得更容易从staff那里保留更多列:

select s.*,
       (select nz(sum(p.salary))
        from payroll as p
        where p.id = s.id
       ) as total
from staff as s;

If you have a total column in staff you can update it using the last approach:如果您在staff有一个total列,您可以使用最后一种方法更新它:

update staff
    set total = (select nz(sum(p.salary))
                 from payroll as p
                 where p.id = staff.id
                );

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

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