How to do a Postgresql subquery in select clause with join in from clause like SQL Server?

前端 未结 5 2034
野性不改
野性不改 2021-01-30 04:00

I am trying to write the following query on postgresql:

select name, author_id, count(1), 
    (select count(1)
    from names as n2
    where n2.id = n1.id
             


        
5条回答
  •  渐次进展
    2021-01-30 04:28

    I am just answering here with the formatted version of the final sql I needed based on Bob Jarvis answer as posted in my comment above:

    select n1.name, n1.author_id, cast(count_1 as numeric)/total_count
      from (select id, name, author_id, count(1) as count_1
              from names
              group by id, name, author_id) n1
    inner join (select author_id, count(1) as total_count
                  from names
                  group by author_id) n2
      on (n2.author_id = n1.author_id)
    

提交回复
热议问题