Accessing column alias in postgresql

前端 未结 1 544
谎友^
谎友^ 2020-12-12 00:46

Having a little bit of trouble understanding how a query alias works in postgresql. I have the following:

SELECT DISTINCT robber.robberid,
                ni         


        
相关标签:
1条回答
  • 2020-12-12 01:02

    In general, you can't refer to an aggregate column's alias later in the query, and you have to repeat the aggregate

    If you really want to use its name, you could wrap your query as a subquery

    SELECT * 
    FROM
    (
        SELECT DISTINCT robber.robberid, nickname, count(accomplices.robberid)  
        AS count1 FROM robber                   
        INNER JOIN accomplices  
        ON accomplices.robberid = robber.robberid  
        GROUP BY robber.robberid, robber.nickname  
    ) v
    ORDER BY count1 desc
    
    0 讨论(0)
提交回复
热议问题