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

PROC SQL双表JOIN求和时仅统计到单表记录问题求助

问题产生原因

  • 关联条件完全错误:expired和active两张分表的关联键应该是分组维度policy_vintage,你写的on wy.expired= ak.active是用两个统计指标值做匹配,两者大概率没有对应关系,会导致几乎所有行匹配失败,expired表的字段匹配后全为缺失值,求和自然只能得到active表的统计结果。
  • 连接逻辑不符合需求:你需要的是两张表各自的全量汇总值,左连接只能覆盖active表存在的policy_vintage范围,若expired表存在active表没有的policy_vintage,对应过期量也会被遗漏。
  • 多余的关联逻辑:你的需求是全局总求和,完全不需要做表关联,额外的关联操作反而引入了匹配错误的问题。

修复方案

方案1(最优:无需关联,直接统计)

不需要先生成分表,直接从原始表按条件统计全局总和即可,逻辑最高效:

proc sql;
create table graphbar as
select
  sum(case when CREDIT = "A" then 1 else 0 end) as ACTIVE,
  sum(case when CREDIT = "W" then 1 else 0 end) as EXPIRED
from PolisyEnd
where CREDIT in ("A","W");
quit;

方案2(保留先做分表的逻辑)

不需要关联,分别对两张分表求和后合并为单行结果即可:

proc sql;
create table graphbar as
select
  (select sum(ACTIVE) from active) as ACTIVE,
  (select sum(EXPIRED) from expired) as EXPIRED;
quit;

方案3(必须用关联逻辑的修正写法)

如果必须通过关联实现,先修正关联条件为policy_vintage,用全连接覆盖所有维度值后再求和:

proc sql;
create table graphbar as
select
  sum(ACTIVE) as ACTIVE,
  sum(EXPIRED) as EXPIRED
from (
  select 
    coalesce(ak.policy_vintage, wy.policy_vintage) as policy_vintage,
    coalesce(ak.ACTIVE, 0) as ACTIVE,
    coalesce(wy.EXPIRED, 0) as EXPIRED
  from active ak
  full join expired wy
  on ak.policy_vintage = wy.policy_vintage
) t;
quit;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 20:45:09