排除 Qubole 中具有特定值的记录
Posted
技术标签:
【中文标题】排除 Qubole 中具有特定值的记录【英文标题】:Exclude records with certain values in Qubole 【发布时间】:2020-11-24 08:00:21 【问题描述】:使用 Qubole
我有
表 A(json 中的列已解析...)
ID Recommendation Decision
1 GOOD GOOD
2 BAD BAD
2 GOOD BAD
3 GOOD BAD
4 BAD GOOD
4 GOOD BAD
我只需要选择推荐良好但决策不良的 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
【问题讨论】:
Decission
字段中的值似乎是错误的 - 根据您的逻辑,它应该是 FAIL,NULL
等?你能纠正一下吗?
@KoushikRoy 谢谢,我更正了。
你可以试试这个吗?这将在 Recommendation = GOOD AND Decision=BAD - select json_parsed['ID'] as ID ,json_parsed["Decision"] as Decision ,json_parsed["Recommendation"] as Recommendation from A where create_date >= '2020-11-18' AND json_parsed["Decision"] ='BAD' AND json_parsed["Recommendation"] ='GOOD'
时为您提供
此查询还返回具有推荐“GOOD”的 ID,例如 ID 2,但我需要排除这些。在完美的场景中,我希望看到只有 ID 3 的建议只有 GOOD。
问题与***.com/questions/15389091/… 相同,但提供的解决方案似乎在 Qubole 中不起作用。
【参考方案1】:
使用分析函数。
演示:
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
【讨论】:
以上是关于排除 Qubole 中具有特定值的记录的主要内容,如果未能解决你的问题,请参考以下文章