我在Hive中有下表。是否可以通过单个查询执行所有这些操作? 在表中完成以下操作:
在表1中,基于“中心”列的用户总数
在表2中:
输出应为Result=sum[Expense in Rupees]/sum[Users#]
任何指导都值得赞赏。
答案 0 :(得分:0)
如果您还指定了输出值,那将真的很容易理解。对于您的问题,是的,您可以使用CASE在单个查询中实现转化
SELECT
center,
account,
SUM(expenses)/SUM(users) AS result
FROM
(
SELECT
t2.center AS center,
t1.users AS users,
CASE
WHEN t2.currency = 'USD' THEN (t2.expenses*70)
WHEN t2.currency = 'MYR' THEN (t2.expenses*17)
WHEN t2.currency = 'AUD' THEN (t2.expenses*50)
WHEN t2.currency = 'INR' THEN t2.expenses
ELSE 'invalid'
END AS expenses,
t2.currency AS currency,
t2.account AS account
FROM
table1 t1
JOIN
table2 t2
ON(t1.center=t2.center)
) joined_data
GROUP BY center,account
ORDER BY center,account
我没有对此进行测试,但是应该可以。在最坏的情况下,它可能会引发较小的语法错误,您应该可以解决它。 :)