表格结构:
Article(
model int(key),
year int(key),
author varchar(key),
num int)
num:年内写的文章数量 p>
找到所有作者,他们在一年内至少写了一篇文章的最大数量(相对于所有其他作者)
我试过了:
SELECT author FROM Article,
(SELECT year,max(sumnum) s FROM
(SELECT year,author,SUM(num) sumnum FROM Article GROUP BY year,author)
GROUP BY year) AS B WHERE Article.year=B.year and Article.num=B.s;
这是正确的答案吗? 感谢。
答案 0 :(得分:2)
你可能想尝试自我 - JOIN
来获得你想要的东西:
SELECT Main.author
FROM Article AS Main
INNER JOIN (
SELECT year
,author
,SUM(num) AS sumnum
FROM Article
GROUP BY year
,author
) AS SumMain
ON SumMain.year = Main.year
AND SumMain.author = Main.author
GROUP BY Main.author
HAVING SUM(Main.num) = MAX(SumMain.sumnum)
;
这可以保证(因为它是ANSI)您获得了MAX
med SUM
的{{1}},并且只返回您需要的结果。请记住,由于所提供的信息,我只对num
这两个字段进行了编辑...如果您有JOIN
的唯一ID,或者您需要更多的特异性来获得1-to- 1匹配,相应调整。
根据您使用的DBMS,可以通过以下两种方式之一简化:
JOIN
某些DBMS允许您执行多个聚合函数,这可以在那里工作。
如果您的DBMS允许您执行OLAP功能,您可以执行以下操作:
SELECT author
FROM (
SELECT year
,author
,SUM(num) AS sumnum
FROM Article
GROUP BY year
,author
HAVING SUM(num) = MAX(sumnum)
) AS Main
;
哪个会将结果集限制为最高SELECT author
FROM (
SELECT year
,author
,SUM(num) AS sumnum
FROM Article
GROUP BY year
,author
) AS Main
QUALIFY (
ROW_NUMBER() OVER (
PARTITION BY author
,year
ORDER BY sumnum DESC
) = 1
)
;
,但如果您希望sumnum
参与,则可能需要更多参数来处理事情(您year
它,只是我提出它的原因。)
希望这有帮助!
答案 1 :(得分:2)
你提到家庭作业和有效的尝试,无论多么不正确。
这是在一个前提下(不清楚,因为没有样本数据)模型列就像一个自动增量,每个作者每年只有一个条目,并且同一作者在同一个作品中不会有多个条目年。例如:
model year author num
===== ==== ====== ===
1 2013 A 15
2 2013 C 18
3 2013 X 17
4 2014 A 16
5 2014 B 12
6 2014 C 16
7 2014 X 18
8 2014 Y 18
因此预期的结果是2013年的最高文章数= 18,并且只返回作者“C”。在2014年,最高文章数是18,并将返回作者“X”和“Y”
首先,查询所写文章的最大数量...
select
year,
max( num ) as ArticlesPerYear
from
Article
GROUP BY
year
这将为您提供每年一条记录,以及发布的文章的最大数量......因此,如果您有2010 - 2014年的数据,那么您将在MOST返回5条记录。现在,就像将其加入到具有匹配年份和文章
的原始表格一样简单select
A2.*
from
( select
year,
max( num ) as ArticlesPerYear
from
Article
GROUP BY
year ) PreQuery
JOIN Article A2
on PreQuery.Year = A2.Year
AND PreQuery.ArticlesPerYear = A2.num
答案 2 :(得分:0)
我建议CTE
WITH maxyear AS
(SELECT year, max(num) AS max_articles
FROM article
GROUP BY year)
SELECT DISTINCT author
FROM article a
JOIN maxyear m
ON a.year=m.year AND a.num=m.max_articles;
并将性能与分区进行比较,这是另一种方式
SELECT DISTINCT author FROM
(SELECT author, rank() AS r
OVER (PARTITION BY year ORDER BY num DESC)
FROM article) AS subq
WHERE r = 1;
我认为一些RDBMS会让你将HAVING rank()=1
放在子查询上,然后你不需要嵌套查询。