首页 > 解决方案 > 尝试应用 WHERE 时,Presto 中的嵌套 SQL 无法解析列名

问题描述

我一直在尝试为我的路由器的利用率创建一个自动图表/查询。

我有一个嵌套查询,它返回以下内容:

Record_Date | Mbps_IN | Mbps_OUT
YYYYMMDD HH:00 | 1234 | 1234

这应该每小时有一个条目,但由于我的路由器的数据收集问题,经常会丢失数小时甚至数天的数据。计数器的性质是“增量”,因此在“原始数据”的其他地方,我正在捕获前一个记录之间的数据量增量,这会导致几个小时的平线,然后是一个非常大的数据值,通常是 2 -3 倍大,因为它包含与数据馈送返回的第一个小时相比记录的多个小时的利用率。

最终,我想找到一种方法来平滑/建立这个峰值的平均值并回填缺失的时间。(但这是另一天的挑战)。

在第一种情况下,我只想选择 Mbps_In 中的值小于 1000 的行。

但是,当我从元数据库或直接连接到 PrestoDB 的 dbeaver 连接执行此操作时,我收到一个错误:

Column 'results.Mbps_In' cannot be resolved {:message "line 27:7: Column 'results.Mbps_in' cannot be resolved", :errorCode 47, :errorName "COLUMN_NOT_FOUND",

我的查询可以很好地提供包括异常值的表格输出,如下所示:

select
          metrics_date_hour Record_Date
          ,round(In_Utilisation_Mbps_Total,2) as Mbps_In
          ,round(Out_Utilisation_Mbps_Total,2) as Mbps_Out
from (
nested query
) results
-- WHERE results.Mbps_In < 1000
Group By Record_Date, Order By Record_Date desc

当我取消注释 Where 子句时,我收到关于无法解析列名的错误。我觉得这应该不难,但我已经尝试了一些变体和努力来引用之前处理的一些原始列以获得此结果输出,但我仍然无法正确引用结果表中的列。

更新成功查询:

select
          metrics_date_hour Record_Date
          ,round(sum(In_Utilisation_Mbps_Total),2) as Mbps_In
          ,round(sum(Out_Utilisation_Mbps_Total),2) as Mbps_Out
from (
nested query
) results
-- WHERE results.Mbps_In < 1000 - I didn't get this to work 
Group By Record_Date
Having (sum(In_Utilisation_Mbps_Total) <1000
Order By Record_Date desc

标签: sqlnestedwhere-clause

解决方案


产生错误是因为您的嵌套查询中没有名为 Mbps_In 的列。我认为你真的需要一个 HAVING 子句而不是 WHERE。尝试将其更改为:

select
          metrics_date_hour Record_Date
          ,round(In_Utilisation_Mbps_Total,2) as Mbps_In
          ,round(Out_Utilisation_Mbps_Total,2) as Mbps_Out
from (
      nested query
     ) results
Group By Record_Date
Having Mbps_In<1000
Order By Record_Date desc

如果您仍想使用 WHERE 子句,则需要更改列名:

select
          metrics_date_hour Record_Date
          ,round(In_Utilisation_Mbps_Total,2) as Mbps_In
          ,round(Out_Utilisation_Mbps_Total,2) as Mbps_Out
from (
      nested query
     ) results
Where In_Utilisation_Mbps_Total<1000
Group By Record_Date
Order By Record_Date desc

推荐阅读