从一列中检索两个最高值以进行差异的排名比较

发布于 2024-12-14 12:28:01 字数 601 浏览 0 评论 0原文

修改为添加信息:

我意识到有很多“获得两个最高”或“获得第二高”的 SQL 问题并已发布答案,所以如果这个问题是多余的,我提前道歉,但我想做一些不同的事情与其他情况相比,我需要一些从 A 到 B 的帮助。我充其量只是一个 MySQL 爱好者,所以我确信答案对你们中的一些人来说是显而易见的。

我有一堆棒球运动员单赛季统计数据。我想将他们的最高值季节与第二高值季节进行比较。我还希望能够通过从最高季节减去第二高季节来比较两个季节。

当然,我可以使用 MAX 轻松获得最高值,但这对于像我这样的新手来说是一个很大的困难。


感谢您迄今为止的帮助。

我将简化相关的表格结构,使其具有相关性:

playerid, Year, Value

每个球员赛季都按年份分隔。

我希望从查询中返回的是

Player id, 
Year [of Highest Value], 
Value [Highest], 
Year [of Second Highest Value], 
Value [Second-Highest]

我希望它足够简单明了。感谢您的任何帮助。

MODIFIED TO ADD INFORMATION:

I realize that there have been many "get two highest" or "get second-highest" SQL questions and answered posted, so I apologize in advance if this question is redundant, but I want to do something a bit different than the other situations, and I need some help getting from A to B. I am a MySQL hobbyist at best, so I'm sure the answer is obvious to some of you.

I have a bunch of rows of baseball player single-season statistics. I want to compare their season with the highest value with their season with the second highest value. I also want to be able to compare the two seasons by subtracting the second-highest from the highest.

I can easily get the highest value using MAX, of course, but this is a big more difficult for a novice like myself.


Thanks for your help so far.

I will simplify the relevant table structure so that it is relevant:

playerid, Year, Value

Each Player-season is separated by year.

What i want returned from my query is

Player id, 
Year [of Highest Value], 
Value [Highest], 
Year [of Second Highest Value], 
Value [Second-Highest]

I hope that is simple enough and clear. Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红ご颜醉 2024-12-21 12:28:01

在不知道表结构的情况下,您基本上可以这样做:

SELECT score
FROM statstable
WHERE playerID=???
ORDER BY score DESC
LIMIT 2

它将检索分数最高的两行,您可以在客户端中提取分数并减去。

如果您在另一个查询中需要用户的最高下一个最高值,那么它会变得有点复杂。

Without knowing your table structure, you could essentially do:

SELECT score
FROM statstable
WHERE playerID=???
ORDER BY score DESC
LIMIT 2

which would retrieve the two rows with the highest scores, which you can the pull out the scores and subtract in your client.

If you need this highest-next_highest value for user in another query, then it gets a bit more complicated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文