简体   繁体   English

SQL Left Join with WHERE 子句 SAS 企业指南

[英]SQL Left Join with WHERE clause SAS Enterprise Guide

I am trying to pair 2 tables (nicknamed PERSIP and ECIF) on their ID field, (labeled TABLE1 & TABLE2) to create a RESULTTABLE, where the ym_id (for both tables) variable is set to my timekey0 variable for a specific datetime.我试图在它们的 ID 字段(标记为 TABLE1 和 TABLE2)上配对 2 个表(昵称 PERSIP 和 ECIF)以创建一个 RESULTTABLE,其中 ym_id(对于两个表)变量设置为特定日期时间的 timekey0 变量。

I am wondering why this code produces 0 rows of resulting data.我想知道为什么这段代码会产生 0 行结果数据。 After looking online, this was the format people posted as solutions to similar problems.在网上看了之后,这是人们发布的解决类似问题的格式。

%let timekey0 = 202110;
proc sql;

CREATE TABLE RESULTTABLE AS
SELECT

PERSIP.col1,
PERSIP.col2,
PERSIP.col3,

ECIF.col1,
ECIF.col2,
ECIF.col3,
ECIF.col4

FROM DB.TABLE1 PERSIP

LEFT JOIN DB.TABLE2 ECIF

ON PERSIP.ID = ECIF.ID 

WHERE ECIF.ym_id = &timekey0.

AND PERSIP.ym_id = &timekey0.;

quit;

I got a result of 0 rows with many columns.我得到了 0 行有很多列的结果。 Not sure if my join type is incorrect but I have 0 rows in the table.不确定我的连接类型是否不正确,但表中有 0 行。

There may be two reasons for this:这可能有两个原因:

  1. There is no records matching to your where criteria (ECIF.ym_id = &timekey0. AND PERSIP.ym_id = &timekey0.)没有与您的 where 条件匹配的记录 (ECIF.ym_id = &timekey0. AND PERSIP.ym_id = &timekey0.)
  2. There is no records to join matching your on criteria (ON PERSIP.ID = ECIF.ID)没有符合您条件的记录加入 (ON PERSIP.ID = ECIF.ID)

Your logic seems off.你的逻辑似乎不对。 You say you want a LEFT JOIN then use a variable from the "RIGHT" table in your WHERE condition.您说您想要 LEFT JOIN,然后在 WHERE 条件中使用“RIGHT”表中的变量。

Most likely you just want to add those conditions to the ON condition.您很可能只想将这些条件添加到 ON 条件中。

FROM TABLE1 PERSIP
LEFT JOIN TABLE2 ECIF
  ON PERSIP.ID = ECIF.ID 
  AND ECIF.ym_id = &timekey0.
  AND PERSIP.ym_id = &timekey0.

Or perhaps just keep the condition that will limit the observations read from the "LEFT" table in the WHERE condition或者也许只是保留限制从 WHERE 条件中的“LEFT”表读取的观察结果的条件

FROM TABLE1 PERSIP
LEFT JOIN TABLE2 ECIF
  ON PERSIP.ID = ECIF.ID 
  AND PERSIP.ym_id = ECIF.ym_id 
WHERE PERSIP.ym_id = &timekey0.

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

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