Why can't I use column aliases in the next SELECT expression?

前端 未结 4 831
别跟我提以往
别跟我提以往 2020-11-28 16:18

Can I modify the next to use the column aliases avg_time and cnt in an expression ROUND(avg_time * cnt, 2)?

SELECT 
           


        
4条回答
  •  旧时难觅i
    2020-11-28 16:43

    Aliases are not available until the virtual relation is actually created, if you want to do additional expressions using the aliases themselves you will have to create the virtual relation using as sub-query than run an additional query on top of it. So I would modify your query to the following:

    SELECT stddev_time, max_time, avg_time, min_time, ROUND(avg_time * cnt, 2) as slowdown, path FROM
    (
    SELECT 
        COALESCE(ROUND(stddev_samp(time), 2), 0) as stddev_time, 
        MAX(time) as max_time, 
        ROUND(AVG(time), 2) as avg_time, 
        MIN(time) as min_time, 
        COUNT(path) as cnt, 
        ROUND(AVG(time) * COUNT(path), 2) as slowdown, path
    FROM 
        loadtime
    GROUP BY
        path
    ORDER BY
        avg_time DESC
    LIMIT 10;
    )
    

    I want to add here the reason your second query worked is because the query planner recognized those columns as defined directly in the table you're querying them from.

提交回复
热议问题