繁体   English   中英

Presto 查询没有得到正确的值

[英]Presto query not getting the correct values

请需要你的帮助。

我有一个表名 wqmram,我想按月计算列名 s6 的最小值和最大值。 最小值不应该为零,因为我想要大于零的最小值。

I have written the below query:
SELECT min(s6) AS Mins6,
max(s6) AS Maxs6,
partition_0,
partition_1
FROM wqmram
WHERE cast(s6 AS decimal(30,2)) != 0.00
GROUP BY partition_0,partition_1
ORDER BY partition_0,partition_1;

partition_0 是年,patition_1 是月。 我得到的结果如下,这是错误的:

    Mins6   Maxs6   partition_0 partition_1
1   1017    996 2019    11
2   1002    994 2019    12
3   00.09   958 2020    01
4   00.01   997 2020    02
5   100 999 2020    03
6   100 999 2020    04
7   1   99  2020    05
8   1000    998 2020    06

如果您看到上述结果,那么最小值将大于最大值,并且它们也是错误的。

有人可以让我知道是什么问题吗?

在计算它们的minmax之前,您需要将字符串值转换为数字,否则,它们将按字符串进行比较; 通常, '996'大于'1017' ,因为前者以'9'开头,而后者以'1'开头。

在子查询中转换可能比在外部查询中重复cast()表达式更简单:

select 
    partition_0,
    partition_1
    min(s6) as mins6,
    max(s6) as maxs6
from (
    select partition_0, partition_1, cast(s6 as decimal(30,2)) s6 
    from wqmram
) t
where s6 != 0.00
group by partition_0,partition_1
order by partition_0,partition_1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM