SQL SELECT 語句:分組後使用列別名
資料庫操作通常需要在 子句後面的 SELECT
表達式中使用列別名。 然而,在這種情況下直接使用別名經常會導致錯誤。 GROUP BY
<code class="language-sql">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 * cnt, 2) as slowdown, path FROM loadtime GROUP BY path ORDER BY avg_time DESC LIMIT 10;</code>
語句作為一個整體來處理;別名定義得不夠早,無法在同一語句中使用。 SELECT
<code class="language-sql">SELECT stddev_time, max_time, avg_time, min_time, cnt, ROUND(avg_time * cnt, 2) as slowdown 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, path FROM loadtime GROUP BY path ORDER BY avg_time DESC LIMIT 10 ) X;</code>
以上是SQL SELECT 語句中分組後如何使用列別名?的詳細內容。更多資訊請關注PHP中文網其他相關文章!