从连接表中,选择一列的最大值,但如果有多个最大值,则从另一列中选择具有最大值的一个
我需要将另一个表连接到我的查询中,并从该连接表中获取特定列的最大值。问题是,有时用户会拥有该最大值的倍数(例如:如果最大值是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,应该可以做到这一点。我不清楚为什么
version_number
根本没有出现在您的示例查询中,但也许这是一个错误。将内联视图更改为:
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: