【问题标题】:Count over previously defined aliasCount over previously defined alias
【发布时间】:2022-12-02 07:34:20
【问题描述】:

I'm trying to avoid writing twice the EXTRACT condition. There is any way to do that? Count over the previous alias apparently is not valid.

SELECT EXTRACT(DECADE FROM to_date(released_year::text, 'yyyy')) AS decade, 
    count(decade) AS total_by_decade
FROM album ...

【问题讨论】:

  • no you can avoid it, you can only make a cte

标签: sql postgresql


【解决方案1】:

Basically you can avoit it completely, but you can make a CTE

WITH
 CTE as(
SELECT EXTRACT(DECADE FROM to_date(released_year::text, 'yyyy')) AS decade 
 
FROM album ...)
SELECT  decade,  count(decade) AS total_by_decade FROM CTe GROUP BY decade

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-15
    • 2016-01-14
    • 2023-02-03
    • 2014-03-04
    • 2020-12-08
    • 2016-07-29
    • 1970-01-01
    • 2021-07-04
    相关资源
    最近更新 更多