为什么我的列别名在子查询中不起作用?

时间:2018-12-15 17:25:06

标签: sql subquery common-table-expression snowflake-datawarehouse column-alias

我试图在Snowflake上运行以下代码:

SELECT 
agg_zone_delivery_metrics_hourly.zone_code,
agg_zone_delivery_metrics_hourly.start_of_period_local_date,
MIN(CASE WHEN azdmh.start_of_period_local_hour_of_day = '18' THEN agg_zone_delivery_metrics_hourly.throughput_actual END) AS TP18,
MIN(CASE WHEN azdmh.start_of_period_local_hour_of_day = '19' THEN agg_zone_delivery_metrics_hourly.throughput_actual END) AS TP19,
MIN(CASE WHEN azdmh.start_of_period_local_hour_of_day = '20' THEN agg_zone_delivery_metrics_hourly.throughput_actual END) AS TP20
FROM (
SELECT 
    CAST(EXTRACT(HOUR FROM CAST(agg_zone_delivery_metrics_hourly.START_OF_PERIOD_LOCAL  AS TIMESTAMP)) AS INT) AS "azdmh.start_of_period_local_hour_of_day",
    TO_CHAR(TO_DATE(agg_zone_delivery_metrics_hourly.START_OF_PERIOD_LOCAL ), 'YYYY-MM-DD') AS "agg_zone_delivery_metrics_hourly.start_of_period_local_date",
    agg_zone_delivery_metrics_hourly.ZONE_CODE  AS "agg_zone_delivery_metrics_hourly.zone_code",
    nullif(sum(agg_zone_delivery_metrics_hourly.orders_delivered), 0) / nullif(sum(agg_zone_delivery_metrics_hourly.rider_hours_worked_dhw_sum), 0)
       AS "agg_zone_delivery_metrics_hourly.throughput_actual"
FROM tblB  AS agg_zone_delivery_metrics_hourly

WHERE ((((agg_zone_delivery_metrics_hourly.START_OF_PERIOD_LOCAL ) >= ((DATEADD('day', -7, DATE_TRUNC('week', DATE_TRUNC('day', CONVERT_TIMEZONE('UTC', 'Hongkong', CAST(CURRENT_TIMESTAMP() AS TIMESTAMP_NTZ))))))) AND (agg_zone_delivery_metrics_hourly.START_OF_PERIOD_LOCAL ) < ((DATEADD('day', 7, DATEADD('day', -7, DATE_TRUNC('week', DATE_TRUNC('day', CONVERT_TIMEZONE('UTC', 'Hongkong', CAST(CURRENT_TIMESTAMP() AS TIMESTAMP_NTZ))))))))))) AND (((CASE TO_CHAR(agg_zone_delivery_metrics_hourly.START_OF_PERIOD_LOCAL , 'DY')
WHEN 'Tue' THEN 'Tuesday'
WHEN 'Wed' THEN 'Wednesday'
WHEN 'Thu' THEN 'Thursday'
WHEN 'Sat' THEN 'Saturday'
ELSE TO_CHAR(agg_zone_delivery_metrics_hourly.START_OF_PERIOD_LOCAL , 'DY') || 'day' END) = 'Sunday')) AND (CAST(EXTRACT(HOUR FROM CAST(agg_zone_delivery_metrics_hourly.START_OF_PERIOD_LOCAL  AS TIMESTAMP)) AS INT) IN (20,18,19)) AND (agg_zone_delivery_metrics_hourly.COUNTRY_NAME = 'Hong Kong') AND agg_zone_delivery_metrics_hourly.is_within_zone_hours AND (agg_zone_delivery_metrics_hourly.COUNTRY_NAME = 'Hong Kong')
GROUP BY 1,TO_DATE(agg_zone_delivery_metrics_hourly.START_OF_PERIOD_LOCAL ),3) AS tblA

但是,我收到以下错误消息:

SQL compilation error: error line 2 at position 0 invalid identifier 'AGG_ZONE_DELIVERY_METRICS_HOURLY.ZONE_CODE'

我相信这里的问题是代码无法从子查询中识别列别名。

2 个答案:

答案 0 :(得分:0)

如果像在内部查询的列中那样在"中包含别名,则也必须在"中包含任何引用,并使用准确的名称(区分大小写)。 / p>

尝试

SELECT 
"agg_zone_delivery_metrics_hourly.zone_code",
...

(其他列也是如此)。

或者删除别名周围的"并使用不带.的别名,这反而会造成混淆。

答案 1 :(得分:0)

这与您使用的表别名有关。这种别名:

AS "agg_zone_delivery_metrics_hourly.throughput_actual"

令人困惑,因为:

  • 如果在表别名中使用双引号«”,则必须在引用该别名的每个表达式中使用它们

  • 点(«。»)通常用于将表别名与列名分开,例如«table.column»

较短的解决方案是将所有对这些怪异别名的引用也都用双引号引起来。

但是,为了便于阅读和维护,最好更改表别名,方法是删除双引号并用另一个字符替换点(通常使用下划线)。上面的内容可以更改为:

AS agg_zone_delivery_metrics_hourly_throughput_actual

PS1:您应该考虑改进查询的格式,因为它的确很难读取。

PS2:您的查询还有其他问题。也就是说,您在顶部查询(«SUM ...»)中已经聚合了函数,但是没有«GROUP BY»子句:这将不起作用。您需要在查询末尾添加一个GROUP BY子句,该子句引用输出中未聚合的列,例如:

GROUP BY
    agg_zone_delivery_metrics_hourly.zone_code,
    agg_zone_delivery_metrics_hourly.start_of_period_local_date,