背景
我需要从头开始快速学习一些 PostgreSQL,以便进行有关汽车保险的数据分析项目。我有相当体面的大小的本地存储的PostgreSQL数据库(大约在像汽车和摩托车的车辆保险理赔数据的8GB价值),和我需要JOIN
和UNION ALL
为了几件事情让表我需要为我的统计楷模。
我需要做的第一部分是这件事,关于汽车索赔和摩托车索赔的两个表格之间的JOIN
内部UNION ALL
:
select
l.customer_combined_id,
l.claim_id,
l."Part_Cd",
l.service_date,
h.principal_problem_cd,
h.problem_code_vers_flag
from claims.auto_claims_line_items as l
JOIN claims.auto_claims_general h on l.claim_id = h.claim_id
UNION ALL
select
l.customer_combined_id,
l.claim_id,
l."Part_Cd",
l.entry_date as service_date,
NULL as principal_problem_cd,
NULL as problem_code_vers_flag
from claims.motorcycle_claims_line_items as l
这会产生一个看起来像这样的表(列名缩写是为了美观):
cust_comb_id| claim_id | "Part_Cd" | svc_date | prin_prob_cd | prob_cd_vers_flg |
------------+----------+-----------+----------+--------------+------------------|
| | | | | |
如您所见,汽车声称有一些摩托车声称没有的列。这很好——我已经填写了这些NULL
,以便让UNION ALL
它们工作。现在汽车索赔表很好地堆叠在摩托车索赔表的顶部。到现在为止还挺好。
到目前为止,我所做的第二部分是另一件事,它涉及有关汽车和摩托车保险保单持有人(“客户”)的数据:
select m.customer_dob,
m.customer_id,
m.customer_gender_cd,
m.customer_zip_cd,
c.customer_combined_id
from customer."Customer" m
JOIN customer.customer_combined_crosswalk c on m.customer_id = c.customer_id
其结果如下所示:
dob | customer_id | gender_cd | zip_cd | cust_comb_id |
----+-------------+-----------+--------+--------------|
| | | | |
问题
我已经弄清楚了我的数据操作的两半,但我不知道如何将这两半放在一起,可以这么说。我想(我认为)离开加入这两件事cust_comb_id
,但我不知道如何写。我想保留第一部分(索赔数据)cust_comb_id
中的所有内容,并在匹配时从第二部分(保单持有人/客户)中引入数据,如果不匹配,则给出空值。这是我正在寻找的视觉效果:
cust_comb_id| claim_id | "Part_Cd" | svc_date | prin_prob_cd | prob_cd_vers_flg |dob | cust_id | gender_cd | zip_cd |
------------+----------+-----------+----------+--------------+------------------|----+---------+-----------+--------+
| | | | | | | | | |
我试过的
我尝试使用子查询来加入这些连接,但我不断收到错误消息。编辑:
这是我尝试过的具体示例:
select *
from
(select
l.customer_combined_id,
l.claim_id,
l."Part_Cd",
l.service_date,
h.principal_problem_cd,
h.problem_code_vers_flag
from claims.auto_claims_line_items as l
JOIN claims.auto_claims_general h on l.claim_id = h.claim_id
UNION ALL
select
l.customer_combined_id,
l.claim_id,
l."Part_Cd",
l.entry_date as service_date,
NULL as principal_problem_cd,
NULL as problem_code_vers_flag
from claims.motorcycle_claims_line_items as l) as cl
LEFT JOIN
select m.customer_dob,
m.customer_id,
m.customer_gender_cd,
m.customer_zip_cd,
c.customer_combined_id
from customer."Customer" m
JOIN customer.customer_combined_crosswalk c on m.customer_id = c.customer_id
这会产生错误ERROR: syntax error at or near "select"
。
任何帮助深表感谢。
[注意:customer_combined_id
并且customer_id
是两个不同的东西:它combined id
是独一无二的,并且考虑到客户何时从一个保险计划(他们有一个保险计划)切换到另一个保险计划customer_id
,并在那里他们获得了新的保险计划。]
所以这是一个语法问题。
OP 已经拥有所有需要的部分:
唯一的问题是语法上的挣扎。
我想这种形式将是最易读的:
WITH PartI AS(
select
l.customer_combined_id,
l.claim_id,
l."Part_Cd",
l.service_date,
h.principal_problem_cd,
h.problem_code_vers_flag
from claims.auto_claims_line_items as l
JOIN claims.auto_claims_general h on l.claim_id = h.claim_id
UNION ALL
select
l.customer_combined_id,
l.claim_id,
l."Part_Cd",
l.entry_date as service_date,
NULL as principal_problem_cd,
NULL as problem_code_vers_flag
from claims.motorcycle_claims_line_items as l
),
PartII AS (
select customer_dob,
customer_id,
customer_gender_cd,
customer_zip_cd,
customer_combined_id
from customer."Customer" m
JOIN customer.customer_combined_crosswalk c on m.customer_id = c.customer_id
)
SELECT
*
FROM
PartI P1
LEFT JOIN PartII P2
ON P1.customer_combined_id = P2.customer_combined_id;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句