sql - 来自多个列的“Hive”最大列值
问题描述
嗨:我有一种情况,我需要找到 3 个计算字段的最大值并将其存储在另一个字段中,是否可以在一个 SQL 查询中完成?下面是示例
SELECT Income1 ,
Income1 * 2% as Personal_Income ,
Income2 ,
Income2 * 10% as Share_Income ,
Income3 ,
Income3 * 1% as Job_Income ,
Max(Personal_Income, Share_Income, Job_Income )
From Table
我尝试的一种方法是Personal_Income, Share_Income, Job_Income
在第一遍和第二遍中计算
Select
Case when Personal_income > Share_Income and Personal_Income > Job_Income
then Personal_income
when Share_income > Job_Income
then Share_income
Else Job_income as the greatest_income
但这需要我对十亿行表进行 2 次扫描,我怎样才能避免这种情况并一次性完成?非常感谢任何帮助。
解决方案
As of Hive 1.1.0 you can use greatest()
function. This query will do in a single table scan:
select Income1 ,
Personal_Income ,
Income2 ,
Share_Income ,
Income3 ,
Job_Income ,
greatest(Personal_Income, Share_Income, Job_Income ) as greatest_income
from
(
SELECT Income1 ,
Income1 * 2% as Personal_Income ,
Income2 ,
Income2 * 10% as Share_Income ,
Income3 ,
Income3 * 1% as Job_Income ,
From Table
)s
;
推荐阅读
- python - 更改脚本以停止返回非素数并仅返回素数
- xcode - 由于 AudioFileID,无法在 Xcode 上编译它
- python-3.x - Numpy将数组减少到给定的形状
- asp.net-core - 使用 .Net Core 中的 PKCE 和 Okta 从 Swagger UI 连接 OpenID
- amazon-web-services - 如何从特定的 AWS 代码提交账户克隆分支
- spring-cloud - Spring Cloud Load Balancer 中基于上下文的部署 Vip 地址
- python-3.x - 如何使用 Apache2 和 Mod-WSGI 运行 Flask
- javascript - 如何将“隐藏”属性添加到现有 HTML 5 元素,然后将其从现有 HTML 5 元素中删除?
- java - 检查字符串是否按字母顺序排列
- node.js - express-validator :如何检查电子邮件 ID 数组?