[英]Need to find the Max value in a SQL table and Based on the MAX value need to generate the Id's
[英]Need to find the max term_code value for each person in my table
我有一个具有多个 term_code 值的人员列表。 我需要找到每个拥有 201930 或 201940 记录的人的最大值。 如果两者都有,比如鲍勃的情况,我需要拿 201930。 然后,我需要为每个具有该术语的人返回其他字段。 只会返回红色记录。 Fred 不应该出现在 output 中。
这是我目前的查询,但它获取了 Bob 的 201940 记录。 它的记录总数是正确的,但它得到了一些不正确的值。
SELECT userid, term_code, race, gender
FROM mytable a JOIN (
SELECT userid, MAX(term_code) AS term_code
FROM mytable
WHERE term_code <= '201940'
GROUP BY userid
) b ON (a.userid = b.userid and a.term_code = b.term_code)
WHERE term_code IN ('201930', '201940');
使用这条线对我来说似乎是合乎逻辑的,它为 Bob 获得了正确的值,但它使我的结果减少了大约 30%。
WHERE term_code <= COALESCE ('201930','201940')
有什么建议么?
NOT EXISTS
:
select m.* from mytable m
where m.term_code = (
case when not exists (select 1 from mytable where userid = m.userid and term_code = 201930)
then 201940
else 201930
end
)
或者,如果您只想要userid
和term_code
,那么您可以通过简单的聚合来完成:
select userid, min(term_code) term_code
from mytable
where term_code in (201930, 201940)
group by userid
如果您想要表格中的整行,那么您可以加入表格:
select m.*
from mytable m inner join (
select userid, min(term_code) term_code
from mytable
where term_code in (201930, 201940)
group by userid
) t on t.userid = m.userid and t.term_code = m.term_code
或使用ROW_NUMBER()
window function:
select t.userid, t.term_code, t.race, t.gender
from (
select m.*,
row_number() over (partition by userid order by term_code) rn
from mytable m
where m.term_code in (201930, 201940)
) t
where t.rn = 1
见演示。
结果:
> USERID | TERM_CODE | RACE | GENDER
> :----- | --------: | :--- | :-----
> Bob | 201930 | null | null
> Tim | 201940 | null | null
with t (USERID, term_code ) as (
select 'Bob', 201601 from dual union all
select 'Bob', 201605 from dual union all
select 'Bob', 201609 from dual union all
select 'Bob', 202930 from dual union all
select 'Bob', 202940 from dual union all
select 'Bob', 202950 from dual union all
select 'Tom', 202940 from dual union all
select 'Tom', 201605 from dual union all
select 'Tom', 201609 from dual union all
select 'Mac', 201601 from dual union all
select 'Mac', 201605 from dual union all
select 'Mac', 201609 from dual
)
select userid, term_code from
(
SELECT t.*
, sum(case when term_code in (202930, 202940) then 1 end) over (partition by userid order by term_code) rnk
FROM t
)
where rnk = 1
USE TERM_CODE
--- ----------
Bob 202930
Tom 202940
请注意,除了您感兴趣的值之外,term_code 值并不相同。对于每个 USERID,term_code 使用 SUM() 分析 function 根据您的条件进行排名。 一旦解决了这个问题,外部查询就会简单地过滤掉内部查询中产生的排名第一的行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.