sqlite:count和group by子句未提供预期的结果

时间:2018-11-13 10:12:45

标签: sql sqlite group-by having

在sqlite上,我有桌子

papers: rero_id, doi, year
writtenby: rero_id, authorid, instid
authors: author_id, name, firstname
inst: inst_id, name, see_id

inst是院校列表:大学等。 书面形式的每一行都给出了一篇论文,一位作者,该作者当时所隶属的机构。可以有一个以上的机构,并且对每个机构重复一对夫妇的文件。 对于给定的作者,我想要一份清单和他与之共同撰写论文的机构的数量。 对于我尝试过的列表

SELECT  inst.name as loc
FROM (
(authors INNER JOIN writtenby ON authors.authorid = 
      writtenby.authorid) 
      INNER JOIN writtenby AS writtenby_1 ON writtenby.rero_id = 
       writtenby_1.rero_id

 ) 

 INNER JOIN authors AS auth_1 ON writtenby_1.authorid = 
 auth_1.authorid
 inner join inst on  writtenby_1.instid = inst.inst_id 

 WHERE (authors.name) ="Doe" AND (authors.firstname)= "Joe"
 ORDER BY loc

我得到的列表似乎还可以。

现在,我想重新组合这些机构名称并进行计数。 我尝试过

 SELECT  inst.name, count(inst.name)
 FROM (
    (authors INNER JOIN writtenby ON authors.authorid = 
     writtenby.authorid) 
     INNER JOIN writtenby AS writtenby_1 ON writtenby.rero_id = 
     writtenby_1.rero_id

 ) 

 INNER JOIN authors AS auth_1 ON writtenby_1.authorid = 
 auth_1.authorid
 inner join inst on  writtenby_1.instid = inst.inst_id 
 GROUP BY inst.name
 HAVING (authors.name) ="Doe" AND (authors.firstname)= "John"

我只有三行,没有第一条查询中列出的机构。 感谢您纠正我!

François

2 个答案:

答案 0 :(得分:0)

尝试使用where而不是

 SELECT  inst.name, count(inst.name)
 FROM (
    (authors INNER JOIN writtenby ON authors.authorid = 
     writtenby.authorid) 
     INNER JOIN writtenby AS writtenby_1 ON writtenby.rero_id = 
     writtenby_1.rero_id

 ) 

 INNER JOIN authors AS auth_1 ON writtenby_1.authorid = 
 auth_1.authorid
 inner join inst on  writtenby_1.instid = inst.inst_id 
 where authors.name ='Doe' AND authors.firstname= 'John' 
 GROUP BY inst.name

答案 1 :(得分:0)

我明白了,

SELECT inst.name as loc, count(*) as c
FROM (
(authors INNER JOIN writtenby ON authors.authorid = writtenby.authorid) 
INNER JOIN writtenby AS writtenby_1 ON writtenby.rero_id = 
writtenby_1.rero_id
inner join inst on  writtenby_1.instid = inst.inst_id 
) 
INNER JOIN authors AS auth_1 ON writtenby_1.authorid = auth_1.authorid
WHERE (authors.name) ="Doe" AND (authors.firstname)= "John"
GROUP BY inst.name
ORDER BY c DESC

我仍然可以使用where子句,这与拥有...不同 感谢fa6在下面给出了答案 F。