2014-09-24 49 views
0

这里是架构的一个画面: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_idlist 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; 

,但它说我缺少一个括号我不

+0

我的首选项是不使用减号命令。您可以将join加入到一个子查询中,该子查询返回已排除的ID列表和过滤器,其中subquery.id为null,以仅获取列表中未包含的列表.... harsh psuedo code:select * from query left join(select ID's不包括在)id = id其中subquery.id为空。 – Twelfth 2014-09-24 22:50:43

回答

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 
+0

你能解释一下'sum(decode(co.name,'SIGMOD',1,0))= 0'的含义吗?我从来没有听说过有条件的聚合。 – 2014-09-25 00:15:45

+0

@GeorgeRockbraker这就像说:“当共名是'SIGMOD',然后加1或者加0”,它就像COUNTIF一样可以说 – 2014-09-25 00:27:20

+0

更像是一个案例结构,因为我看到它。 – 2014-09-25 00:28:53

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) 
相关问题