数据表
Name Company Continent country state district
Tom HP Asia India Assam Kdk
George SAP Africa Sudan Chak ksk
Bill EBAY Europe Denmark Lekh Sip
Charles WM Asia India Haryana Jhat
Chip WM Asia India Punjab Chista
Chia WM Asia India Punjab Mast规则表
Continent country state district Pass
Asia India ALL ALL Yes
Asia India Punjab ALL NO
Asia India Punjab Mast Yes我在蜂巢有两张桌子。根据规则,我必须过滤掉数据表中的数据。
在规则表中有一个名为pass的列,它决定数据表中的记录是否需要过滤。
在本例中,有不同类型的规则。他们是在更广泛的层面和狭窄的层面。狭义的规则不应影响更广泛层面的规则。这意味着狭义上的规则是更广泛层次上的规则的例外。例句:在规则表中,有3条记录。第一个记录是更广泛层面的规则。其他的则处于狭窄的水平。
第一条规定,所有有印度、邦和地区的记录都要通过。第二条规则规定,印度、旁遮普邦和地区的所有记录都不能通过。第三条规定,所有有印度、旁遮普邦和马斯特地区的记录都要通过。
第二条规则是第一条规则的例外。第三条规则是第二条规则的例外。
考虑到数据表中的数据和规则表中的规则,对于印度(国家)记录,pass列如下。
Name Company Continent country state district Pass
Tom HP Asia India Assam Kdk Yes
Charles WM Asia India Haryana Jhat Yes
Chia WM Asia India Punjab Mast Yes
Chip WM Asia India Punjab Chista No这只是一个例子。在生产中,数据将是不同的。
如何使用SQL/Sql脚本实现这一点?
帮助是非常感谢的。
发布于 2020-09-11 12:09:42
你想要最具体的规则。在Hive中,您可以使用多个left join:
select d.*, coalesce(r1.pass, r2.pass, r3.pass)
from data d left join
rules r1
on r1.Continent = d.Continent and
r1.country = d.country and
r1.state = d.state and
r1.district = d.district left join
rules r2
on r2.Continent = d.Continent and
r2.country = d.country and
r2.state = d.state and
r2.district = 'ALL' left join
rules r3
on r3.Continent = d.Continent and
r3.country = d.country and
r3.state = 'ALL' and
r3.district = 'ALL' ;如果允许LEFT JOIN和country使用'ALL',您可能希望继续使用continent。
发布于 2020-09-11 12:25:15
@TomG :如果这有帮助,请看下面的代码
select * from TEMP_TESTING where country ='India' and district<>'Chista'
union
(select * from TEMP_TESTING where country ='India' except
select * from TEMP_TESTING where country ='India' and state='Punjab')
union
select * from TEMP_TESTING where country ='India'and state='Punjab' and district='Mast'https://stackoverflow.com/questions/63846775
复制相似问题