我正在尝试在SQL中执行查询,但未获得正确的返回值

时间:2019-09-11 19:24:55

标签: sql postgresql

数据库是PostgreSQL 9.6。

create table candidates (
  id            int             not null,
  name          varchar,
  email         varchar,
  created_at    timestamptz,
  updated_at    timestamptz
);

insert into candidates values
  (1,   'John Snow',        'john@snow',    now()+interval '1 seconds', now()+interval '1 seconds'),
  (2,   'Bob Ross',         'bob@ross',     now()+interval '2 seconds', now()+interval '2 seconds'),
  (3,   'Kim Possible',     'kim@possible', now()+interval '3 seconds', now()+interval '3 seconds'),
  (4,   'Ash Ketchum',      'ash@ketchum',  now()+interval '4 seconds', now()+interval '4 seconds'),
  (5,   'Goku Son',         'goku@son',     now()+interval '5 seconds', now()+interval '5 seconds'),
  (6,   'Mortimer Smith',   'morty@smh',    now()+interval '6 seconds', now()+interval '6 seconds');

create table jobs (
  id            int             not null,
  title         varchar,
  positions     int,
  created_at    timestamptz,
  updated_at    timestamptz
);

insert into jobs values
  (1,   'Night Watch Commander',    1,      now()+interval '11 seconds',    now()+interval '11 seconds'),
  (2,   'Pokemon Master',           8,      now()+interval '12 seconds',    now()+interval '12 seconds'),
  (3,   'Blessed Painter',          100,    now()+interval '13 seconds',    now()+interval '13 seconds');

create table applications (
  id            int             not null,
  candidate_id  int             not null,
  job_id        int             not null,
  score         decimal,
  created_at    timestamptz,
  updated_at    timestamptz
);

insert into applications values
  (1,   1,  1,  90,     now()+interval '21 seconds',    now()+interval '21 seconds'),
  (2,   1,  3,  80,     now()+interval '22 seconds',    now()+interval '22 seconds'),
  (3,   2,  1,  10,     now()+interval '23 seconds',    now()+interval '23 seconds'),
  (4,   2,  2,  10,     now()+interval '24 seconds',    now()+interval '24 seconds'),
  (5,   2,  3,  99,     now()+interval '25 seconds',    now()+interval '25 seconds'),
  (6,   3,  1,  60,     now()+interval '26 seconds',    now()+interval '26 seconds'),
  (7,   3,  2,  70,     now()+interval '27 seconds',    now()+interval '27 seconds'),
  (8,   3,  3,  50,     now()+interval '28 seconds',    now()+interval '28 seconds'),
  (9,   4,  2,  90,     now()+interval '29 seconds',    now()+interval '29 seconds'),
  (10,  5,  1,  9000,   now()+interval '30 seconds',    now()+interval '30 seconds');

对于至少具有一个应用程序的每个候选人,查询要求提供姓名,其最大应用程序分数的职位名称以及该应用程序的分数。 (如果有关系,请选择最后创建的应用程序)。要进行测试,请使用网站http://sqlfiddle.com

Expected output:
name             title                   score
Ash Ketchum      Pokemon Master          90
Bob Ross         Blessed Painter         99
Goku Son         Night Watch Commander   9000
John Snow        Night Watch Commander   90
Kim Possible     Pokemon Master          70

1 个答案:

答案 0 :(得分:0)

按分数排序的第一位,按候选人划分。这就是我在下面的“ rankApps”中所做的。然后为每个候选人选择排名最高的职位,加入候选人和职位,然后输出。

with 

    rankApps as (

        select     candidate_id, job_id, score,
                   row_number() over(partition by candidate_id order by score desc) as rnk
        from       applications

    )

    select        c.name, j.title, a.score
    from          rankApps a
    join          candidates c on a.candidate_id = c.id 
    join          jobs j on a.job_id = j.id
    where         rnk = 1
    order by      c.name