我有一个包含3列的表格:
我需要一个查询,获取JournalName以及所有作者在此期刊中获得最高分数的计数。
提前谢谢。
答案 0 :(得分:3)
select
maxscorejournalinstances.journalname,
COUNT(*) as maxscorecount
from
(
select
journalname
from
foo inner join
(
select
authorid,
MAX(authorscore) as maxscore
from
foo
group by
authorid
) maxauthorscores
on foo.AuthorId = maxauthorscores.AuthorId
and foo.AuthorScore = maxauthorscores.maxscore
) maxscorejournalinstances
group by
maxscorejournalinstances.JournalName
请注意,如果作者在两个或更多期刊中具有相同的高分,则每个期刊都将包含在结果集中。
答案 1 :(得分:0)
SELECT AuthorID, MAX(AuthorScore) as AuthorScore,
(
SELECT JournalName
FROM tab t2
WHERE t1.AuthorID = t2.AuthorID AND t2.AuthorScore = MAX(t1.AuthorScore)
) as JournalName
FROM tab t1
GROUP BY AuthorID
答案 2 :(得分:0)
select
x.journalname, count(x.authorid)
from tableX x
inner join
(
select authorid, max(authorscore) max_authorscore
from tableX
group by authorid
) tmp on x.authorid=tmp.authorid and x.authorscore=tmp.max_authorscore
group by journalname
答案 3 :(得分:0)
这样的事可能有用。
select authorid, journalname, authorscore, max(authorscore) over(parition by authorid)
from <table>
order by journalname
对sql olap函数进行一些研究应该指出正确的方向,如果这不起作用。
答案 4 :(得分:0)
听起来你需要2个查询,因为返回的数据无法在一个记录集中返回,数据布局很容易看到。
每个期刊的作者数量,
select JournalName, count(distinct AuthorID)
from table
group by JournalName
作者每个期刊的最高分,
select JournalName, AuthorID, max(AuthorScore)
from table
group by JournalName, AuthorID