You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MS Access中合并交叉表的重复记录信息?

问题描述

我在工作中使用MS Access进行遗产规划,目前数据呈现如下:

当前数据

File_NameExecutor_1Executor_2Beneficiary_1Beneficiary_2
Hill, HankPeggy HillPeggy Hill
Hill, HankBobby HillBobby Hill
Gribble, DaleNancy Gribble
Gribble, DaleJoseph GribbleJoseph Gribble
Gribble, DaleJohn Redcorn

但我需要将其整理为如下格式,用于Word的MailMerge功能生成遗嘱:

目标数据

File_NameExecutor_1Executor_2Beneficiary_1Beneficiary_2
Hill, HankPeggy HillBobby HillPeggy HillBobby Hill
Gribble, DaleNancy GribbleJoseph GribbleJoseph GribbleJohn Redcorn

目前我们没有使用专门的遗产规划软件,任何无需手动在Word中重新输入的方法都可以。


补充:当前使用的SQL代码

TRANSFORM Last(File_Roles.File_Name) AS LastOfFile_Name

SELECT File_Roles.Executor_1, 
File_Roles.Executor_2, 
File_Roles.Beneficiary_1, 
File_Roles.Beneficiary_2, 
File_Roles.Trustee_1,
File_Roles.Trustee_2, 
File_Roles.Guardian_1, 
File_Roles.Guardian_2, 
File_Roles.ATTY_IF_1, File_Roles.ATTY_IF_2, 
File_Roles.HCATTY_IF_1, 
File_Roles.HCATTY_IF_2

FROM File_Roles

GROUP BY File_Roles.Executor_1, 
File_Roles.Executor_2, 
File_Roles.Beneficiary_1, 
File_Roles.Beneficiary_2,
File_Roles.Trustee_1,
File_Roles.Trustee_2, 
File_Roles.Guardian_1, 
File_Roles.Guardian_2, 
File_Roles.ATTY_IF_1, 
File_Roles.ATTY_IF_2, 
File_Roles.HCATTY_IF_1, 
File_Roles.HCATTY_IF_2

PIVOT File_Roles.File_Name;
解决方案

你当前的SQL分组逻辑错误,应该按File_Name分组,对每个角色字段取非空值。Access的First()或Last()聚合函数会忽略空值,直接提取分组内的有效数据,刚好满足需求。

修改后的SQL代码如下:

SELECT 
    File_Roles.File_Name,
    First(File_Roles.Executor_1) AS Executor_1,
    First(File_Roles.Executor_2) AS Executor_2,
    First(File_Roles.Beneficiary_1) AS Beneficiary_1,
    First(File_Roles.Beneficiary_2) AS Beneficiary_2,
    First(File_Roles.Trustee_1) AS Trustee_1,
    First(File_Roles.Trustee_2) AS Trustee_2,
    First(File_Roles.Guardian_1) AS Guardian_1,
    First(File_Roles.Guardian_2) AS Guardian_2,
    First(File_Roles.ATTY_IF_1) AS ATTY_IF_1,
    First(File_Roles.ATTY_IF_2) AS ATTY_IF_2,
    First(File_Roles.HCATTY_IF_1) AS HCATTY_IF_1,
    First(File_Roles.HCATTY_IF_2) AS HCATTY_IF_2
FROM File_Roles
GROUP BY File_Roles.File_Name;

关键说明

  • First()函数会在同一File_Name分组中跳过空值,直接取该字段的第一个非空值,完美实现同文件下分散数据的合并。
  • 若某字段存在多个非空值(极端情况),可根据实际需求替换为Last()或Max(),结果一致。
  • 执行该查询后,得到的结果即为单条记录对应单个File_Name的格式,可直接用于Word邮件合并。

内容的提问来源于stack exchange,提问作者Liz Furtado

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 17:48:15