排除 Qubole 中具有特定值的记录

Exclude records with certain values in Qubole

使用 Qubole

我有

Table A(json 中的列已解析...)

ID  Recommendation    Decision
1     GOOD            GOOD
2     BAD             BAD
2     GOOD            BAD
3     GOOD            BAD
4     BAD             GOOD
4     GOOD            BAD

我只需要 Select 具有良好建议但决策错误的 ID。因此输出应该是 3.

我试过了:

SELECT a.ID  
FROM (
select json_parsed['ID'] as ID
,json_parsed["Decision"] as Decision
,json_parsed["Recommendation"] as Recommendation
from  A  
where create_date >= '2020-11-18') a
Left JOin
(select json_parsed['ID'] as ID
,json_parsed["Decision"] as Decision
,json_parsed["Recommendation"] as Recommendation
from  A
where create_date >= '2020-11-18') as b on a.ID = b.ID and b.Recommendation = "GOOD"
Where
b.Recommendation is NULL

使用解析函数。

演示:

with your_table as (--use your table instead of this sample
select stack(6,
1,'GOOD','GOOD',
2,'BAD','BAD'  ,
2,'GOOD','BAD' ,
3,'GOOD','BAD' ,
4,'BAD','GOOD' ,
4,'GOOD','BAD') as (ID,Recommendation,Decision)
)

select ID,Recommendation,Decision
from
(
select d.*, 
       count(*) over(partition by id) as cnt,
       count(case when Recommendation  = 'GOOD' then 1 end) over(partition by id) cnt_Recommendation_good, 
       count(case when Decision  = 'BAD' then 1 end) over(partition by id) cnt_Decision_BAD
  from 
  your_table d
) s
where cnt_Recommendation_good=cnt
  and cnt_Decision_BAD = cnt

结果:

id  recommendation  decision
3   GOOD            BAD