2014-09-24 46 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) 
相關問題