用于过滤日期的ID的SQL查询

时间:2014-08-18 08:06:17

标签: mysql sql

基本上我有一个从跟踪工具返回数据的查询。查询是:

SELECT `bh`.`bug_id`, `p`.`name` AS `project`, `p2`.`name` AS `portfolio`, `u`.`username` AS `user`,FROM_UNIXTIME( `bh`.`date_modified` ) AS `modify_ts`, 
CASE `b`.`category_id`
when '0' then 'No Category'
when '1' then 'All Projects General'
when '185' then '1. REQ - Requirements'
when '186' then '2. DES - Design'
when '180' then '3. DEV - Development Phase Testing'
when '181' then '4. QAT - Quality Assurance Testing'
when '182' then '5. BAT -Experian Business Accecptance Testing'
when '183' then '6. UAT - External Client User Testing'
when '184' then '7. WAR - Warranty Period'
when '223' then '[Mort Phase 3] 8. Defect Density'
else `b`.`category_id`
END as `Category`,
CASE `b`.`severity`
when '40' then 'Cosmetic'
when '50' then 'Minor'
when '60' then 'Major'
when '70' then 'Critical'
END as `Severity`,
CASE `b`.`priority` 
when '20' then 'Low'
when '30' then 'Medium'
when '40' then 'High'
when '50' then 'Very High'
END as `Priority`,
CASE `b`.`resolution`
when '10' then 'Open' 
when '20' then 'Passed Testing' 
when '60' then 'Duplicate' 
when '70' then 'Change Request' 
when '80' then 'Deferred' 
when '90' then 'Rejected' 
END as `Resolution`,
CASE `b`.`Status` 
when '10' then 'New' 
when '20' then 'Clarification'
when '40' then 'Fixed'
when '50' then 'Open' 
when '80' then 'Ready to test' 
when '90' then 'Closed' 
END as `Status`
FROM `bugtracker`.`mantis_bug_history_table` AS `bh`
LEFT JOIN `bugtracker`.`mantis_bug_table` `b` ON `bh`.`bug_id` = `b`.`id`
LEFT JOIN `bugtracker`.`mantis_user_table` `u` ON `bh`.`user_id` = `u`.`id` 
LEFT JOIN `bugtracker`.`mantis_project_table` `p` ON `b`.`project_id` = `p`.`id` 
LEFT JOIN `bugtracker`.`mantis_project_hierarchy_table` `ph` ON `b`.`project_id` = `ph`.`child_id`
LEFT JOIN `bugtracker`.`mantis_project_table` `p2` ON `ph`.`parent_id` = `p2`.`id`

删除列的输出示例代码为:

> 19213 Triggers UAT    14/08/2014 11:00    Major   Very High   Open    New
19213   Triggers UAT    14/08/2014 11:00    Major   Very High   Open    New
19213   Triggers UAT    14/08/2014 11:00    Major   Very High   Open    New
19213   Triggers UAT    04/08/2014 15:30    Major   Very High   Open    New
19213   Triggers UAT    04/08/2014 15:30    Major   Very High   Open    New
19394   Triggers UAT    15/08/2014 14:24    Major   High    Open    New
19394   Triggers UAT    15/08/2014 14:24    Major   High    Open    New
19394   Triggers UAT    14/08/2014 10:56    Major   High    Open    New
19394   Triggers UAT    14/08/2014 10:56    Major   High    Open    New
19432   Triggers UAT    15/08/2014 14:32    Major   High    Open    New
19432   Triggers UAT    15/08/2014 14:32    Major   High    Open    New

我想在我的查询中做的是对每个ID(最左边的列 - bhbug_id)说,然后只返回具有最新日期的那个(第3列 - { {1}}。`DATE_MODIFIED)。然后我可以在那之上抛出一个区别,如果需要的话可以返回相同的区域。

我有办法过滤掉那个查询吗?

已实施添加到上述查询中的代码:

bh

2 个答案:

答案 0 :(得分:0)

您可以在获得所有数据后通过bug_id和max(日期)尝试分组。

尝试select bug_id,max(date_column),(/*other columns*/) from (/*your sql here*/) as tmp_tabel group by bug_id

答案 1 :(得分:0)

加入这样的最长日期:

SELECT DISTINCT
...
FROM `bugtracker`.`mantis_bug_history_table` AS `bh`
INNER JOIN (SELECT bug_id, MAX(date_modified) md FROM mantis_bug_history_table GROUP BY bug_id) max_dates ON bh.bug_id = max_dates.bug_id AND bh.date_modified = max_dates.md
LEFT JOIN ...

关于另一个例子以及解决此问题的其他方法,这是关于The Rows Holding the Group-wise Maximum of a Certain Column的优秀手册条目