如何使用减号或不存在

时间:2014-09-24 22:30:30

标签: sql oracle

这是架构的图片:http://i.stack.imgur.com/yr26g.png

我正在尝试创建一个查询来查找ICDE出版物数量最多的作者,他们没有在SIGMOD中发布。

我认为我在ICDE上获得最多出版物的作者数量正在走上正轨:

select AUTHOR.NAME, aid, count(PUBLICATION.id)
from PUBLICATION, AUTHORPUBLICATION, CONFERENCE, AUTHOR
where aid=AUTHOR.id and pid=PUBLICATION.id and cid=CONFERENCE.id and CONFERENCE.name='ICDE'
group by AUTHOR.name, aid order by count(PUBLICATION.id) desc;

但不知道如何形成查询,以便排除已在SIGMOD中发布的作者

我尝试过的事情之一:

with aut(n,a,i) as
((select AUTHOR.NAME, aid, count(PUBLICATION.id) as cnt
from PUBLICATION, AUTHORPUBLICATION, CONFERENCE, AUTHOR
where aid=AUTHOR.id and pid=PUBLICATION.id and cid=CONFERENCE.id and CONFERENCE.name='ICDE'
group by AUTHOR.NAME, aid)
minus
(select AUTHOR.NAME, aid, count(PUBLICATION.id) as cnt
from PUBLICATION, AUTHORPUBLICATION, CONFERENCE, AUTHOR
where aid=AUTHOR.id and pid=PUBLICATION.id and cid=CONFERENCE.id and CONFERENCE.name='SIGMOD'
group by AUTHOR.NAME, aid))
select n,a,i from aut order by i desc;

但它似乎根本没有用。我有一份清单 author, id, paper_id 要么 list of author, id, count(paper)(我已经尝试了两种方式)并且由于纸质ID,我认为我有一个不相交的设置,无法让它工作。已经花了3个小时:(

我还尝试了摆脱纸张ID的东西:

with aut(n,a) as
((select AUTHOR.NAME, aid
from PUBLICATION, AUTHORPUBLICATION, CONFERENCE, AUTHOR
where aid=AUTHOR.id and pid=PUBLICATION.id and cid=CONFERENCE.id and CONFERENCE.name='ICDE'
order by count(PUBLICATION.id) desc)
minus
(select AUTHOR.NAME, aid
from PUBLICATION, AUTHORPUBLICATION, CONFERENCE, AUTHOR
where aid=AUTHOR.id and pid=PUBLICATION.id and cid=CONFERENCE.id and CONFERENCE.name='SIGMOD'
order by count(PUBLICATION.id) desc))
select n,a from aut;

但它说我错过了一个括号?我不是。

2 个答案:

答案 0 :(得分:1)

我认为使用条件聚合可以更好地编写:

select au.name,
       au.id,
       sum(decode(co.name, 'ICDE', 1, 0)) as num_publications
  from publication pu
  join authorpublication ap
    on pu.id = ap.pid
  join author au
    on au.id = ap.aid
  join conference co
    on co.id = pu.cid
 where co.name in ('ICDE', 'SIGMOD')
 group by au.name, au.id
having sum(decode(co.name, 'SIGMOD', 1, 0)) = 0
 order by num_publications desc

这会过滤两个会议,但只计算SELECT列表中的ICDE,并通过HAVING子句过滤掉SIGMOD会议上任何有出版物的作者。

作为另一种选择,虽然我认为效率较低,但您也可以使用内联视图进行外连接,选择要排除的作者,然后过滤结果为null的位置(与内联不匹配)视图):

select au.name, au.id, count(pu.id) as num_publications
  from publication pu
  join authorpublication ap
    on pu.id = ap.pid
  join author au
    on au.id = ap.aid
  join conference co
    on co.id = pu.cid
  left join (select au.id
               from publication pu
               join authorpublication ap
                 on pu.id = ap.pid
               join author au
                 on au.id = ap.aid
               join conference co
                 on co.id = pu.cid
              where co.name = 'SIGMOD') x
    on x.id = au.id
 where co.name = 'ICDE'
   and x.id is null
 group by au.name, au.id
 order by num_publications desc

答案 1 :(得分:0)

你的第二次尝试很接近。试试这样的事情;

select etc
from etc
where author_id in 
(select the author id's you want
minus
select the author id's you don't want)