[英]View to show/hide row values based on another table in SQL
我有一个包含客户联系数据的表,例如:
cust_id tel_no email
cust1 234238 a@a.com
cust2 423443 a@ab.com
cust3 234238 d@a.com
我有一张表格,该表格显示了不用于特定客户的渠道(我们称其为optout)
cust_id channel
cust1 tel_no
cust2 tel_no
cust2 email
cust3 email
我想在SQL中创建一个视图,该视图将根据退出表中的客户/渠道组合在第一个表中隐藏联系人数据,以便结果如下所示:
cust_id tel_no email
cust1 a@a.com
cust2
cust3 234238
什么是实现这一目标的最佳方法? 我在考虑类似case when cust1 in (select cust_id from optout where channel = 'tel_no')
作为每个字段的检查时,如果有大约50万个客户,那可能就毫无用处。 谢谢。
试试这个查询:
SELECT c.cust_id,
nvl2( q.tel_no, null, c.tel_no ) tel_no,
nvl2( q.email, null, c.email ) email
FROM contact_data c
LEFT JOIN (
SELECT cust_id,
min(case channel when 'tel_no' then 1 end) tel_no,
min(case channel when 'email' then 1 end) email
FROM opt
GROUP BY cust_id
) q
ON c.cust_id = q.cust_id
ORDER BY c.cust_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.