首页 > 解决方案 > 如何为至少一次具有付费来源的所有 user_ids 获取结果 1

问题描述

如何获得Result=1所有user_ids至少一次有来源的付费。我的意思不仅仅是针对其中的一行source=paid,而是针对所有行user_id

表中不存在结果列!我们应该使用代码以某种方式得到它!

Row Table
source  session_number  user_id
NULL    1                12345  
NULL    2                12345  
NULL    3                12345  
NULL    4                12345  
NULL    1                67890  
paid    2                67890  
NULL    3                67890  
Desired Table
source  session_number  user_id result
NULL    1                12345  0
NULL    2                12345  0
NULL    3                12345  0
NULL    4                12345  0
NULL    1                67890  1
paid    2                67890  1
NULL    3                67890  1

标签: sqlpostgresql

解决方案


使用存在

select a.* from table_name a
where exists( select 1 from table_name b where a.userid=b.userid
                              and b.source='paid')
and result=1

推荐阅读