Presto 查询没有得到正确的值

Presto query not getting the correct values

需要您的帮助。

我有一个 table 名称 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;