【问题标题】:MySQL query to Hiveql对 Hiveql 的 MySQL 查询
【发布时间】:2017-03-19 13:34:30
【问题描述】:

工作(id,等级)

数据:

work
------------------
1 | A
1 | B
1 | C
1 | D
2 | A
2 | C
2 | B
3 | C

我需要找到所有具有共同排名的 id 对,并且仅当排名计数大于 2 时才应显示并按降序打印它们。我为此编写了一个 mysql 查询,但是我是 SparkSQL 和 HIVEQL 的新手。所以请帮助我如何做到这一点。 例如使用上面的数据结果集应该是:

mysql查询是:

select a.id,b.id
from work as a, work as b
where a.id>b.id
group by a.id,b.id having group_concat(distinct a.rank order by a.rank)=group_concat(distinct b.rank order by b.rank)

---------------------
id1 | id2 | Count
---------------------
 A  | B   |  3
 B  | C   |  3

【问题讨论】:

  • rank 是如何定义的?

标签: sql apache-spark hive apache-spark-sql hiveql


【解决方案1】:

我认为 Hive 不支持 group_concat()。我认为这做同样的事情:

select a.id, b.id, a.cnt
from (select a.*, count(*) over (partition by a.id) as cnt
      from work a
     ) a join
     (select b.*, count(*) over (partition by b.id) as cnt
      from work b
     ) b
     on a.rank = b.rank and a.cnt = b.cnt
where a.id < b.id   -- I *think* this is allowed in Hive; it not, a subquery or expression in the `having` clause will do the same thing
group by a.id, b.id, a.cnt
having count(*) = a.cnt;

这是获得具有相同排名的 id 对的更自然的方法。事实上,它在几乎任何数据库中都应该比 MySQL 版本更高效。 cross join 会生成大量数据。

【讨论】:

  • 谢谢 我会试试这个方法!但是,我只想要对,并且仅当两个用户之间的共同排名计数高于 3 并且我希望它们在 desc 中
猜你喜欢
  • 1970-01-01
  • 2014-10-17
  • 2013-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
  • 2021-02-26
相关资源
最近更新 更多