如果在一列上满足条件但在单独的列中存在重复...标记所有 - Oracle SQL

If a condition is met on one column but duplicates exist in a separate column... mark all - Oracle SQL

我不太确定如何表达这个,但我会尽力解释我需要的结果。

使用 case 语句(或任何建议的)我需要知道的是,是否存在受限制的标题代码并且 CTN 在多个标题代码中相同(重复)... case 语句需要return 'Restricted from Call Recording' 对于 CTN 相同的所有实例。

我的查询:

select 
unique ae.ctn, 
ae.heading_code,
case
when ae.heading_code in ('8003520') then 'Restricted from Call Recording'
end as status
from advertiser_extract ae
where ae.customer_id = '245433513'
and ae.tier in ('20','21','22','25','26','27','30','40','50','60','100','800');

生成的结果:

 ctn           | heading_code | status
---------------+--------------+-------------------------------
(888) 350-5030 | 8004777      | 
(612) 315-1320 | 8015370      |
(888) 350-5030 | 8015370      |
(888) 350-5030 | 8003520      | Restricted from Call Recording
(612) 315-1320 | 8008781      | 
(888) 350-5030 | 8000121      | 
(612) 315-1310 | 8000121      | 
(612) 315-1910 | 8003520      | Restricted from Call Recording
(612) 315-1910 | 8004777      | 
(888) 350-5030 | 8008781      |

然而,我需要的是这样的:

 ctn           | heading_code | status
---------------+--------------+-------------------------------
(888) 350-5030 | 8004777      | Restricted from Call Recording
(612) 315-1320 | 8015370      |
(888) 350-5030 | 8015370      | Restricted from Call Recording
(888) 350-5030 | 8003520      | Restricted from Call Recording
(612) 315-1320 | 8008781      | 
(888) 350-5030 | 8000121      | Restricted from Call Recording
(612) 315-1310 | 8000121      | 
(612) 315-1910 | 8003520      | Restricted from Call Recording
(612) 315-1910 | 8004777      | Restricted from Call Recording
(888) 350-5030 | 8008781      | Restricted from Call Recording

这基本上是在告诉我,虽然标题代码存在两行,但与该标题代码相关的 CTN 存在于多行中,应该限制通话记录。在此示例中,CTN:(888) 350-5030 & (612) 315-1910 的标题代码 (8003520) 受到限制,但这些 CTN 与其他标题代码相关联,默认情况下应贴上标签。

使用我的查询的任何想法和示例都会有很大帮助。

那么你应该改变你的case表达方式来反映

case
when ae.ctn = '(888) 350-5030' or ae.heading_code = '8003520' 
then 'Restricted from Call Recording' end as status

您可以使用 max() 分析函数执行此操作:

select distinct ae.ctn, ae.heading_code,
       max(case when ae.heading_code in ('8003520') then 'Restricted from Call Recording'
           end) over (partition by ae.ctn) as status
from advertiser_extract ae
where ae.customer_id = '245433513' and
      ae.tier in ('20','21','22','25','26','27','30','40','50','60','100','800');

注意:你不应该使用 select distinct 除非你真的必须。