sql - 如何为至少一次具有付费来源的所有 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
解决方案
使用存在
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
推荐阅读
- git - git push 上正在删除 www 下的文件夹
- java - 在 LinkedList 中插入大于参数的值
- flutter - Flutter:ButtonStyle() 和 .styleFrom() 有什么区别
- linux - 如何使用 awk 仅匹配给定字符串而不匹配其他任何内容
- uwp - UWP:如何从重叠内容中排除 GridView 滚动条?
- java - 如何在 Eclipse 中使用 p12 证书?
- python-3.x - 在抓取 Web Selenium WebDriver 时定义用户代理字符串
- mule - 如何在 mule 4 的 JAVA SDK 操作中将函数作为参数传递
- javascript - 查找 GP 系列中的术语数使 3280
- sql - 如何在选择查询中添加条件?