SQL GROUP BY并检索最后一个子记录

时间:2014-10-31 19:36:06

标签: sql sql-server group-by

我正在编写一个从多个表中提取数据的数据库视图。目标是确定公司的最新状态,每个记录(按company_id分组)记录的最高vetting_event_type_position

基本上我试图抓住每家公司的最新记录。我根本不是SQL大师;我知道我需要group by才能删除相关记录,但我无法让它工作。

当前结果

company_id | name | ... | vetting_event_type_position
-----------------------------------------------------
         1 | ABC  | ... |                          1 
         1 | ABC  | ... |                          2 
         1 | ABC  | ... |                          3 
         2 | CBS  | ... |                          1 
         2 | CBS  | ... |                          2 
         3 | HBO  | ... |                          1 

渴望的结果

company_id | name | ... | vetting_event_type_position
-----------------------------------------------------
         1 | ABC  | ... |                          3 
         2 | CBS  | ... |                          2 
         3 | HBO  | ... |                          1 

SQL代码

SELECT
    companies.id as company_id,
    companies.name as name,
    companies.uuid as uuid,
    companies.company_type as company_type,
    companies.description as overview,
    practice_areas.id as practice_area_id,
    practice_areas.name as practice_area_name,
    companies.created_at as created_at,
    companies.updated_at as updated_at,
    companies.created_by as created_by,
    companies.updated_by as updated_by,
    vettings.id as vetting_id,
    vettings.name as vetting_name,
    vetting_event_types.name as vetting_event_status,
    vetting_events.id as vetting_event_id,
    vetting_event_types.position as vetting_event_type_position
FROM
    vettings
        LEFT OUTER JOIN vetting_events ON (vettings.id = vetting_events.vetting_id)
        LEFT OUTER JOIN vetting_event_types ON (vetting_events.vetting_event_type_id = vetting_event_types.id)
        RIGHT OUTER JOIN companies ON (companies.id = vettings.company_id)
        LEFT OUTER JOIN practice_areas ON (companies.practice_area_id = practice_areas.id)
        LEFT OUTER JOIN dispositions ON (companies.disposition_id = dispositions.id)
ORDER BY
    name, vetting_name, vetting_event_type_position
;

表格之间的关联

companies      has_many   vettings
vettings       has_many   vetting_events
vetting_events belongs_to vetting_event_types

或换句话说......

companies -> vettings -> vetting_events <- vetting_event_types

我正在尝试检索每个组的vetting_event_types.position值最高的公司记录。

2 个答案:

答案 0 :(得分:0)

您可以使用row_number分析函数。

Select * from (
    Select ...,
    Row_number() over ( partition by    company_id order by     vetting_event_type_position desc) as seq) T
 Where seq=1

答案 1 :(得分:0)

SELECT company_id
      ,name
      ,uuid
      ,company_type
      ,overview
      ,practice_area_id
      ,practice_area_name
      ,created_at
      ,created_by
      ,updated_by
      ,vetting_id
      ,vetting_name
      ,vetting_event_status
      ,vetting_event_id
      ,vetting_event_type_position
FROM (
     SELECT
    companies.id as company_id,
    companies.name as name,
    companies.uuid as uuid,
    companies.company_type as company_type,
    companies.description as overview,
    practice_areas.id as practice_area_id,
    practice_areas.name as practice_area_name,
    companies.created_at as created_at,
    companies.updated_at as updated_at,
    companies.created_by as created_by,
    companies.updated_by as updated_by,
    vettings.id as vetting_id,
    vettings.name as vetting_name,
    vetting_event_types.name as vetting_event_status,
    vetting_events.id as vetting_event_id,
    vetting_event_types.position as vetting_event_type_position,
    ROW_NUMBER() OVER (PARTITION BY companies.id ORDER BY vetting_event_types.position DESC) rn

    FROM vettings
        LEFT OUTER JOIN vetting_events ON (vettings.id = vetting_events.vetting_id)
        LEFT OUTER JOIN vetting_event_types ON (vetting_events.vetting_event_type_id = vetting_event_types.id)
        RIGHT OUTER JOIN companies ON (companies.id = vettings.company_id)
        LEFT OUTER JOIN practice_areas ON (companies.practice_area_id = practice_areas.id)
        LEFT OUTER JOIN dispositions ON (companies.disposition_id = dispositions.id)
     ) A
WHERE A.rn = 1
ORDER BY name, vetting_name, vetting_event_type_position