SQL再次计数

时间:2019-01-11 14:56:37

标签: sql sqlite count

我正在使用here

中描述的示例数据

我想提供一个机构列表,其中列出给定作者在所有论文中出现的机构。

此查询给出的列表是正确的

SELECT  DISTINCT p.name || ", " || p.country as col1,  w2.paper_id,   w2.inst_id
FROM (
    authors a
    INNER JOIN writtenby w1 
   ON a.author_id = w1.author_id 
    INNER JOIN writtenby w2 
   ON w1.paper_id = w2.paper_id 
     INNER JOIN inst p 
   ON  w2.inst_id = p.inst_id 

) 
WHERE (a.name) = "De La Rue"  AND (a.firstname) = "A"

例如,我计算了3次出现的inst_id = 5。

我无法使用SQL转换此计数。

我尝试过

SELECT  DISTINCT p.name || ", " || p.country as col1,  count(w2.paper_id)
FROM (
    authors a
    INNER JOIN writtenby w1 
   ON a.author_id = w1.author_id 
    INNER JOIN writtenby w2 
   ON w1.paper_id = w2.paper_id 
     INNER JOIN inst p 
   ON  w2.inst_id = p.inst_id 

) 
WHERE (a.name) = "De La Rue"  AND (a.firstname) = "A"
GROUP BY  w2.inst_id

哪个返回:

col1    count(w2.paper_id)
Universite de Paris, France 12
Universite de Lyon, France  6
Univ Freiburg, Germany  6
EPFZ, Switzerland   8

那似乎是完全错误的...

感谢任何纠正!

François

1 个答案:

答案 0 :(得分:0)

尝试使用COUNT(DISTINCT w2.paper_id)而不是COUNT(w2.paper_id)