SAS - sub select 与 like
SAS - sub select with like
祝大家周末愉快
我有一个 table (tableA
),其中一列的值如下所示:
第 1 列:
xyz-12345678
rdr-32343234
fgm-23423423
然后我有另一个 table (tableB
),其中一列包含 tableA.column1
中的所有值,然后还有一些,例如下面的
第 1 列:
rfxyz-1234567800012015-01-029
grrdr-3234323485832015-02-037
tyfgm-2342342343432014-12-148
如您所见,tableA.column1
的值嵌入在 tableB.column1
中。我写了一个如下所示的子选择查询来识别 tableB.column1
中嵌入了 tableA.column1
值的所有实例,但我得到 0 个结果并且没有错误。我可以清楚地看到 tableA.column1
中存在 tableB.column1
中的行值,但我不确定我在这里做错了什么导致没有错误也没有结果,我的 SAS proc sql 如下:
PROC SQL;
select i.*
from tableA i
where exists (select *
from tableB
where i.column1 like '%'||column1||'%'
)
;
quit;
由于我的 SAS 知识不是很丰富,因此我将不胜感激。
你的状态倒退了。 TableB
的列较长,因此您希望它位于 LIKE
的左侧:
PROC SQL;
select i.*
from tableA i
where exists (select *
from tableB b
where b.column1 like '%' || i.column1 || '%'
)
;
quit;
我鼓励您对所有列引用使用 table 别名,尤其是在相关子查询中。
您的尝试与您的描述不太相符。如果您的尝试可信,那么您正在寻找表 A 中的值,而该值包含在表 B 的另一条记录中。如果是这种情况,那么您只是稍微混淆了 where 子句,它应该是:
select i.*
from tableA i
where exists (select *
from tableB
where tableB.column1 like '%'||i.column1||'%'
);
将您之前的值替换为:
WHERE 'xyz-12345678' LIKE '%tyfgm-2342342343432014-12-148%'
这是错误的,你需要它是:
WHERE 'tyfgm-2342342343432014-12-148' LIKE '%xyz-12345678%'
如果您的描述可信,那么您的 where 子句是正确的,但您的表是错误的:
to identify all instances in tableB.column1
that have tableA.column1
values embedded in them
所以你想 select 从 tableB
:
select i.*
from tableB i
where exists (select *
from tableA a
where i.column1 like '%'||a.column1||'%'
);
除了其他答案中指出的问题外,我还建议使用一种基本技术来避免空格问题。
where i.column1 like cats('%',a.column1,'%')
cats
自动去除两边的空格。还有cat
(只是拼接),catt
(只是从右边开始trim),catx
/catq
(分隔,后面的引号也类似至 dsd
).
这些比一堆嵌套函数更具可读性,并且避免了当您忘记 trim、剥离等时出现的此类问题
祝大家周末愉快
我有一个 table (tableA
),其中一列的值如下所示:
第 1 列:
xyz-12345678
rdr-32343234
fgm-23423423
然后我有另一个 table (tableB
),其中一列包含 tableA.column1
中的所有值,然后还有一些,例如下面的
第 1 列:
rfxyz-1234567800012015-01-029
grrdr-3234323485832015-02-037
tyfgm-2342342343432014-12-148
如您所见,tableA.column1
的值嵌入在 tableB.column1
中。我写了一个如下所示的子选择查询来识别 tableB.column1
中嵌入了 tableA.column1
值的所有实例,但我得到 0 个结果并且没有错误。我可以清楚地看到 tableA.column1
中存在 tableB.column1
中的行值,但我不确定我在这里做错了什么导致没有错误也没有结果,我的 SAS proc sql 如下:
PROC SQL;
select i.*
from tableA i
where exists (select *
from tableB
where i.column1 like '%'||column1||'%'
)
;
quit;
由于我的 SAS 知识不是很丰富,因此我将不胜感激。
你的状态倒退了。 TableB
的列较长,因此您希望它位于 LIKE
的左侧:
PROC SQL;
select i.*
from tableA i
where exists (select *
from tableB b
where b.column1 like '%' || i.column1 || '%'
)
;
quit;
我鼓励您对所有列引用使用 table 别名,尤其是在相关子查询中。
您的尝试与您的描述不太相符。如果您的尝试可信,那么您正在寻找表 A 中的值,而该值包含在表 B 的另一条记录中。如果是这种情况,那么您只是稍微混淆了 where 子句,它应该是:
select i.*
from tableA i
where exists (select *
from tableB
where tableB.column1 like '%'||i.column1||'%'
);
将您之前的值替换为:
WHERE 'xyz-12345678' LIKE '%tyfgm-2342342343432014-12-148%'
这是错误的,你需要它是:
WHERE 'tyfgm-2342342343432014-12-148' LIKE '%xyz-12345678%'
如果您的描述可信,那么您的 where 子句是正确的,但您的表是错误的:
to identify all instances in
tableB.column1
that havetableA.column1
values embedded in them
所以你想 select 从 tableB
:
select i.*
from tableB i
where exists (select *
from tableA a
where i.column1 like '%'||a.column1||'%'
);
除了其他答案中指出的问题外,我还建议使用一种基本技术来避免空格问题。
where i.column1 like cats('%',a.column1,'%')
cats
自动去除两边的空格。还有cat
(只是拼接),catt
(只是从右边开始trim),catx
/catq
(分隔,后面的引号也类似至 dsd
).
这些比一堆嵌套函数更具可读性,并且避免了当您忘记 trim、剥离等时出现的此类问题