我具有以下架构(sqlite):
我正在尝试获取2000-2018年(含)每年每年发表的会议文章数量> =期刊的所有作者的姓名。如果作者一年中每个类别中有0篇文章,则该条件仍然成立。唯一重要的年份是2000-2018年
如果已经整整一年了,查询会容易得多,因为我可以统计期刊文章和会议文章并进行比较,然后获得名称。但是,在尝试每年检查2000-2018年时,我陷入了困境。
我当然不想这些年来重复进行查询。我觉得我可能需要按年份分组,但不确定。 到目前为止,我已经能够将2000年至2018年这两种类型的所有文章作为一个大表来获取,但是我不确定下一步该怎么做。:
select articleID, year
from JournalArticle
where year >= 2000 and year <= 2018
union
select articleID, year
from ConferenceArticle
where year >= 2000 and year <= 2018
答案 0 :(得分:1)
嗯。让我们开始计算每个作者和年份的数量:
select a.name, year, sum(is_journal), sum(is_conference)
from ((select ja.article_id, ja.year, 1 as is_journal, 0 as is_conference
from journalarticle ja
) union all
(select ca.article_id, ca.year, 0 as is_journal, 1 as is_conference
from conferencearticle ca
)
) jc join
authors a
on a.article_id = jc.article_id
group by a.name, jc.year
现在,您可以再次汇总以匹配符合条件的年份:
select ay.name
from (select a.name, year, sum(is_journal) as num_journal, sum(is_conference) as num_conference
from ((select ja.article_id, ja.year, 1 as is_journal, 0 as is_conference
from journalarticle ja
) union all
(select ca.article_id, ca.year, 0 as is_journal, 1 as is_conference
from conferencearticle ca
)
) jc join
authors a
on a.article_id = jc.article_id
group by a.name, jc.year
) ay
where (jc.year >= 2000 and jc.year <= 2018) and
num_journal >= num_conference
group by ay.name;
答案 1 :(得分:0)
听起来像可以在GROUP BY中使用COALESCE
SELECT a.name,
COALESCE(j.year, c.year) as "year",
COUNT(j.articleID) AS JournalArticles,
COUNT(c.articleID) AS ConferenceArticles
FROM Author a
LEFT JOIN JournalArticle j ON (j.articleID = a.articleID AND j.year BETWEEN 2000 AND 2018)
LEFT JOIN ConferenceArticle c ON (c.articleID = a.articleID AND c.year BETWEEN 2000 AND 2018)
WHERE (j.year IS NOT NULL OR c.year IS NOT NULL)
GROUP BY a.name, COALESCE(j.year, c.year)
HAVING COUNT(c.articleID) >= COUNT(j.articleID)