如何在sql redshift中对表进行分区后比较两列值

mcdcgff0  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(356)

我在一个表中有4列(person\u id、account\u name、account\u id、account\u id)。对于每个人员id,我要查找在该人员id的“帐户id”列中不存在的所有帐户id。下面是一个示例表:

Person_id Account_Name Account_ID Account_IDs
--------------------------------------------------
123 Name3   ,000000ihi4MAQ, ,000000TF5MAHZ,000000TF5MAQQ,000000grVA6AM,000000ihi4MAQ,000016ILMhAO,
123 Name2   ,000016ILMhAO,  ,000000TF5MAHZ,000000TF5MAQQ,000000grVA6AM,000000ihi4MAQ,000016ILMhAO,
123 Name1   ,000000grVA6AM, ,000000TF5MAHZ,000000TF5MAQQ,000000grVA6AM,000000ihi4MAQ,000016ILMhAO,
123 Name4   ,000000TF5MAQQ, ,000000TF5MAHZ,000000TF5MAQQ,000000grVA6AM,000000ihi4MAQ,000016ILMhAO,
123 Name5   ,000000TF5MAHZ, ,000000TF5MAHZ,000000TF5MAQQ,000000grVA6AM,000000ihi4MAQ,000016ILMhAO,
124 Name2   ,000016ILMhAO,  ,000000frVA6AM,000016ILMhAO,
124 Name7   ,000024ILMhAO,  ,000000frVA6AM,000016ILMhAO,
124 Name8   ,000000frVA7XZ, ,000000frVA6AM,000016ILMhAO,
124 Name5   ,000000TF5MAHZ, ,000000frVA6AM,000016ILMhAO,
124 Name6   ,000000frVA6AM, ,000000frVA6AM,000016ILMhAO,
125 Name11  ,000000frXC6A,  ,000000frVA6BC,000024ILMhJZ,000000frXC6A,000024YTMhA,
125 Name9   ,000000frVA6BC, ,000000frVA6BC,000024ILMhJZ,000000frXC6A,000024YTMhA,
125 Name10  ,000024ILMhJZ,  ,000000frVA6BC,000024ILMhJZ,000000frXC6A,000024YTMhA,
125 Name12  ,000024YTMhA,   ,000000frVA6BC,000024ILMhJZ,000000frXC6A,000024YTMhA,
125 Name13  ,000024IXThJY,  ,000000frVA6BC,000024ILMhJZ,000000frXC6A,000024YTMhA,

所以从这个样本来看,答案应该是:

Person_ID   Account_ID      Account_Name
-----------------------------------------

   124      000000TF5MAHZ     Name5
   124      000024ILMhAO      Name7
   124      000000frVA7XZ     Name8
   125      000024IXThJY      Name13

我不明白在划分表之后如何比较两列中的值。
提前谢谢你的帮助。

whhtz7ly

whhtz7ly1#

假设 Account_IDs 是包含逗号分隔值的字符串,请使用:

WHERE Account_IDs NOT LIKE '%' || Account_ID || '%'

相关问题