【问题标题】:Presto query not getting the correct valuesPresto 查询没有得到正确的值
【发布时间】:2020-10-13 10:25:12
【问题描述】:

请您帮忙。

我有一个表名 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

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

有人可以告诉我是什么问题吗?

【问题讨论】:

  • s6的数据类型是什么?
  • 嗨@GMB - s6 的数据类型是字符串

标签: sql select max min presto


【解决方案1】:

在计算 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;

【讨论】:

  • 感谢 GMB 的帮助。效果很好,感谢您的解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-20
  • 1970-01-01
  • 2022-10-19
  • 2022-08-16
  • 1970-01-01
相关资源
最近更新 更多