仅在满足条件时选择行

时间:2015-02-23 15:42:10

标签: postgresql

我不熟悉PostgreSQL和数据库查询。

我有一个user_id列表,其中包含大学课程,日期开始和结束。 有些用户有多个条目,有时缺少开始日期或结束日期(或两者)。

我需要检索用户所用的最长路线,或者如果缺少开始日期,则需要检索最新的路线。 如果仍有多个选项,则在多个选项中随机选择。

例如

  • 用户2(下方)我只想获得“经济与政治”,因为它有最新的日期;
  • 在用户6上,只有“电气和电子工程”,因为它是较长的路线。

我所做的查询不起作用(我认为我偏离轨道):

    (SELECT Q.user_id, min(Q.started_at) as Started_on, max(Q.ended_at) as Completed_on, 
q.field_of_study 
    FROM
    (select distinct(user_id),started_at, Ended_at, field_of_study
    from educations
    ) as Q
    group by Q.user_id, q.field_of_study )
    order by q.user_id

结果是:

User_id   Started_on     Completed_on   Field_of_studies
   2      "2001-01-01"   ""             "International Economics"
   2      ""             "2002-01-01"   "Economics and Politics"
   3      "1992-01-01"   "1999-01-01"   "Economics, Management of ..."
   5      "2012-01-01"   "2016-01-01"   ""
   6      "2005-01-01"   "2009-01-01"   "Electrical and Electronics Engineering"
   6      "2011-01-01"   "2012-01-01"   "Finance, General"
   6      ""             ""             ""
   6      "2010-01-01"   "2012-01-01"   "Financial Mathematics"

1 个答案:

答案 0 :(得分:0)

我认为这个查询应该做你需要的,它依赖于计算ends_at和started_at之间的天数差异,如果started_at为null(使其间隔很长),则使用0001-01-01

select 
  educations.user_id,
  max(educations.started_at) started_at,
  max(educations.ended_at) ended_at, 
  max(educations.field_of_study) field_of_study
from educations
join (
  select 
  user_id, 
  max(    
    ended_at::date
    -
    coalesce(started_at, '0001-01-01')::date
  ) max_length
  from educations
  where (started_at is not null or ended_at is not null)
  group by user_id
) x on educations.user_id = x.user_id
   and ended_at::date
       -
       coalesce(started_at, '0001-01-01')::date
       = x.max_length
group by educations.user_id
;

Sample SQL Fiddle