从连接表中,选择一列的最大值,但如果有多个最大值,则从另一列中选择具有最大值的一个

发布于 2024-11-07 02:23:02 字数 1496 浏览 1 评论 0原文

我需要将另一个表连接到我的查询中,并从该连接表中获取特定列的最大值。问题是,有时用户会拥有该最大值的倍数(例如:如果最大值是 5.1,则另一行也包含该最大值,因此它会给出多个结果)。我需要知道如何让它获取最大值,以及当存在该最大值的倍数时(并且仅当存在该最大值的倍数时,以便我仍然从没有多个最大值的用户那里获得结果),从另一列获取最大值,而不会忘记查询最初获得的最大值。

我在下面包含了当前的查询,该查询返回多个最大值。我要加入并引用的表是 APPLICATION_VERSION。我需要根据 VERSION_NUMBER 列中的 USER_ACCOUNT_ID(我从 EMPLOYEE 表中获取)获取最大值。如果有多个最大 VERSION_NUMBER,我希望它根据最大 VERSION_CHANGE_DATE 选择最大 VERSION_NUMBER。有时,尽管 VERSION_CHANGE_DATE 也有多个最大值,所以我希望它选择具有最大 VERSION_CHANGE_DATE 的版本,然后选择 APPLICATION_VERSION_ID 的版本。

顺便说一句,抱歉,如果我让事情变得比需要的更复杂。只是想彻底一点。我真的很感激任何帮助:)

SELECT e.user_account_id,e.employee_id,e.external_id_1,e.external_id_2,e.last_name as LAST,e.first_name as FIRST,e.job_profile_type as rank,e.status_change_date,t.name as TEAM,a.alignment_name as TERRITORY,m.machine_node_id as NODE_ID,a.alignment_id,t.division,av.version_change_date,av.version_number as EI_Version,av.login_date as LAST_LOGIN,m.platform_version
FROM employee e, alignment a, machine_node m, team t, application_version av,
(SELECT av.user_account_id,MAX(av.version_change_date) as maxdate,max(av.application_version_id) as maxversionid
FROM application_version av
GROUP BY av.user_account_id) av2
where e.employee_id = a.employee_id
and av.version_change_date = av2.maxdate
and e.employee_id = m.employee_id
and t.team_id = a.team_id
and e.status = 'ACTV'
and m.status = 'ACTV'
and e.user_account_id=av.user_account_id
and m.machine_type = 'REMO'
and e.external_id_1= 'XM68823'
order by e.last_name asc

I need to join another table to my query, and grab the max value of a particular column from that joined table. The problem is that sometimes a user will have multiples of that max (eg: if the max value is 5.1, there is another row that contains that max value as well, so it gives multiple results). I need to know how to have it grab the max, and when there are multiples of that max (and ONLY when there are multiples of that max, so that I still get the results from users that don't have multiple max values), grab the max value from another column, without forgetting the max value that the query initially got.

I've included my current query below that returns the multiple max values. The table I am joining and referring to is APPLICATION_VERSION. I need to grab the max value based off the USER_ACCOUNT_ID (which I get from the EMPLOYEE table) from column VERSION_NUMBER. If there are multiple maxes of VERSION_NUMBER, I want it to choose the max VERSION_NUMBER based on the max VERSION_CHANGE_DATE. Sometimes though there are multiples maxes of the VERSION_CHANGE_DATE also so I would then want it to pick the one with the the max VERSION_CHANGE_DATE and then the APPLICATION_VERSION_ID.

Btw, sorry if I made this more complicated than it needs to be. Just wanted to be thorough. I'd really appreciate any assistance :)

SELECT e.user_account_id,e.employee_id,e.external_id_1,e.external_id_2,e.last_name as LAST,e.first_name as FIRST,e.job_profile_type as rank,e.status_change_date,t.name as TEAM,a.alignment_name as TERRITORY,m.machine_node_id as NODE_ID,a.alignment_id,t.division,av.version_change_date,av.version_number as EI_Version,av.login_date as LAST_LOGIN,m.platform_version
FROM employee e, alignment a, machine_node m, team t, application_version av,
(SELECT av.user_account_id,MAX(av.version_change_date) as maxdate,max(av.application_version_id) as maxversionid
FROM application_version av
GROUP BY av.user_account_id) av2
where e.employee_id = a.employee_id
and av.version_change_date = av2.maxdate
and e.employee_id = m.employee_id
and t.team_id = a.team_id
and e.status = 'ACTV'
and m.status = 'ACTV'
and e.user_account_id=av.user_account_id
and m.machine_type = 'REMO'
and e.external_id_1= 'XM68823'
order by e.last_name asc

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

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

发布评论

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

评论(1

初心未许 2024-11-14 02:23:02

如果我理解正确的话,应该可以做到这一点。我不清楚为什么 version_number 根本没有出现在您的示例查询中,但也许这是一个错误。

将内联视图更改为:

(SELECT av.user_account_id,
        MAX(av.version_number) KEEP (DENSE_RANK LAST ORDER BY version_number,version_change_date, application_version_id) as maxversion,
        MAX(av.version_change_date) KEEP (DENSE_RANK LAST ORDER BY version_number,version_change_date, application_version_id) as maxdate,
        max(av.application_version_id) KEEP (DENSE_RANK LAST ORDER BY version_number,version_change_date, application_version_id) as maxversionid
FROM application_version av
GROUP BY av.user_account_id) av2

If I've understood you correctly, this should do it. I'm unclear why version_number doesn't appear at all in your sample query, but maybe that was a mistake.

Change the inline view to:

(SELECT av.user_account_id,
        MAX(av.version_number) KEEP (DENSE_RANK LAST ORDER BY version_number,version_change_date, application_version_id) as maxversion,
        MAX(av.version_change_date) KEEP (DENSE_RANK LAST ORDER BY version_number,version_change_date, application_version_id) as maxdate,
        max(av.application_version_id) KEEP (DENSE_RANK LAST ORDER BY version_number,version_change_date, application_version_id) as maxversionid
FROM application_version av
GROUP BY av.user_account_id) av2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文