Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Division in postgres

Tags:

sql

postgresql

I've 2 tables, Paper and Author. Paper contains all the papers published in a given time period and Authors has the name of author in one column and id of paper published in another(so, the same author may occur several times). Now I'm supposed to find the average number of authors per paper.

My try:-

    (SELECT COUNT(*) FROM Author as decimal)/(SELECT COUNT(*) FROM Paper);

Clearly the first part will give me the number of rows in table Author and the second gives me the total no. of papers published. But, I'm getting an error "ERROR: syntax error at or near "/"". What I believe is that it's because I'm probably dividing a table by a table(since I'm just a beginner in postgres, I may be horribly wrong, please correct me if that's the case). I just wanna know how can we solve this error. Thanks!


1 Answers

Assuming all papers have at least one author:

select count(*) * 1.0 / count(distinct paper_id)
from authors;

The * 1.0 is to avoid integer division.

like image 86
Gordon Linoff Avatar answered Sep 08 '25 10:09

Gordon Linoff