简体   繁体   English

需要找到我表中每个人的最大 term_code 值

[英]Need to find the max term_code value for each person in my table

I have a list of people with multiple term_code values.我有一个具有多个 term_code 值的人员列表。 I need to find the max for each person that has a 201930 or 201940 record.我需要找到每个拥有 201930 或 201940 记录的人的最大值。 I need to take the 201930 if there is both such as the case with Bob.如果两者都有,比如鲍勃的情况,我需要拿 201930。 I then need to return other fields for each person with that term.然后,我需要为每个具有该术语的人返回其他字段。 Only the red records will be returned.只会返回红色记录。 Fred should not show up in the output. Fred 不应该出现在 output 中。

在此处输入图像描述

Here is the query I currently have, but it grabs the 201940 record for Bob.这是我目前的查询,但它获取了 Bob 的 201940 记录。 The total number of records is correct with it, but it gets some incorrect values.它的记录总数是正确的,但它得到了一些不正确的值。

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');

Using this line seems logical to me and it gets the right value for Bob, but it cuts my results by about 30%.使用这条线对我来说似乎是合乎逻辑的,它为 Bob 获得了正确的值,但它使我的结果减少了大约 30%。

WHERE term_code <= COALESCE ('201930','201940') 

Any suggestions?有什么建议么?

With NOT EXISTS : 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
)

Or if you only want the userid and term_code then you can do it with simple aggregation:或者,如果您只想要useridterm_code ,那么您可以通过简单的聚合来完成:

select userid, min(term_code) term_code
from mytable 
where term_code in (201930, 201940)
group by userid

If you want the full row from the table then you can join to the table:如果您想要表格中的整行,那么您可以加入表格:

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

Or with ROW_NUMBER() window function:或使用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

See the demo .演示
Results:结果:

> 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

Please note the term_code values are not the same except for the ones that you are interested in. For each USERID the term_code is ranked based on your condition using a SUM() analytic function.请注意,除了您感兴趣的值之外,term_code 值并不相同。对于每个 USERID,term_code 使用 SUM() 分析 function 根据您的条件进行排名。 Once that is worked out, the outer query simply filters out the 1st ranked row produced in the inner query.一旦解决了这个问题,外部查询就会简单地过滤掉内部查询中产生的排名第一的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM