[英]Hive sql find out how many common customer in each country
我有一个名为 custtable 的表,有 3 列 custid,country,date 国家有 5 个国家:'CH'、'US'、'UK'、'FR' 和 'GE' 我希望有 elegent 查询以了解如何许多独特的 [custid] 在 5 个国家/地区。
目前,我可以使用子查询和临时表来查找重叠集,但有更简单的方法的任何建议。
这是我找出两个国家重叠的方法,然后我需要做另一个子查询
with t1 AS
(SELECT DISTINCT [custid]
FROM custtable
where date>20140101
and country='CH'),
t2 as
(SELECT DISTINCT [custid]
FROM custtable
where date>20140101
and country='FR'),
t3 AS
(SELECT DISTINCT [custid]
FROM custtable
where date>20140101
and country='US'),
t4 as
(SELECT DISTINCT [custid]
FROM custtable
where date>20140101
and country='UK')
select count (distinct t1.custid)
from t1
inner join t3
on (t1.custid=t3.custid)
inner join t2
on (t1.custid=t2.custid)
inner join t4
on (t1.custid=t4.custid)
感谢您提供任何意见
我认为更好的方法是计算每个custid
有多少个不同的国家并过滤计数 >= 5,例如,
with count_table as (
select custid, count(distinct country) as cnt
from custtable
where date>20140101
)
select custid, cnt
from count_table
where cnt >= 5
然后数你的cusid
SELECT COUNTRY
, COUNT(DISTINCT CUSTID) AS CNT
FROM CUSTTABLE
GROUP BY COUNTRY
如果您想要所有五个国家/地区的客户:
select custid
from custtable
where date > 20140101
group by custid
having count(distinct country) = 5;
如果您想要那些特定的五个国家(如您的查询所示):
select custid
from custtable
where date > 20140101 and
country in ('CH','US', 'UK','FR', 'GE')
group by custid
having count(distinct country) = 5;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.