简体   繁体   English

Presto 查询没有得到正确的值

[英]Presto query not getting the correct values

Require your help please.请需要你的帮助。

I have a table name wqmram and i want to calculate the min and max value for a column name s6 by month wise.我有一个表名 wqmram,我想按月计算列名 s6 的最小值和最大值。 Min value should not be zero as i want minimum value which is above zero.最小值不应该为零,因为我想要大于零的最小值。

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 is year and patition_1 is month. partition_0 是年,patition_1 是月。 I get the results as below which is wrong:我得到的结果如下,这是错误的:

    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

If you see the above result then minimum values are coming greater than maximum values and that too they are wrong.如果您看到上述结果,那么最小值将大于最大值,并且它们也是错误的。

Can somebody please let me know what is the issue?有人可以让我知道是什么问题吗?

You need to cast the string values to numbers before you compute their min and max , otherwise, they are compared string-wise;在计算它们的minmax之前,您需要将字符串值转换为数字,否则,它们将按字符串进行比较; typically, '996' is greater than '1017' , since the former starts with '9' , while the latter starts with '1' .通常, '996'大于'1017' ,因为前者以'9'开头,而后者以'1'开头。

It might be simpler to convert in a subquery rather than repeating the cast() expression in the outer query:在子查询中转换可能比在外部查询中重复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