简体   繁体   English

SQL选择以消除重复并返回子表上记录的最新日期

[英]SQL select to eliminate duplicates and return the latest date for records on a child table

I am trying to retrieve data from a primary table, my_form, and some from a table linked via a linktable. 我正在尝试从主表my_form和通过链接表链接的表中检索数据。 There is a possibility that the actions table could contain more than one action_required = 'Close' record for the header record. 动作表可能包含多个头记录的action_required ='Close'记录。

Here are my three tables: 这是我的三个表:

my_form:
# idmy_form, summary, description, station
'1', 'OSR puncture', 'Puncture while driving to work', 'Thornbury'
'2', 'Windscreen wiper broke', 'Wiper broke while going shopping', 'Bristol'
'3', 'Out of screenwash', 'Run out of screen wash en route to Cardiff', 'Cardiff'


form_action:
# idmy_form, action_id
'1', '201'
'1', '202'
'1', '203'
'2', '301'
'2', '302'
'3', '401'
'3', '402'
'3', '403'
'3', '404'
'3', '405'

actions:
# action_id, action_detail, action_required, action_date
'201', 'Fix', 'Open', '2013-01-01 00:00:00'
'202', 'Fix', 'Amend', '2013-01-04 00:00:00'
'203', 'Fix', 'Close', '2013-01-06 00:00:00'
'301', 'Fix', 'Open', '2013-03-01 00:00:00'
'302', 'Fix', 'Review', '2013-04-12 00:00:00'
'401', 'Fix', 'Open', '2013-09-04 00:00:00'
'402', 'Fix', 'Review', '2013-09-12 00:00:00'
'403', 'Fix', 'Close', '2013-09-17 00:00:00'
'404', 'Fix', 'Reopen', '2013-09-21 00:00:00'
'405', 'Fix', 'Close', '2013-09-23 00:00:00'

So far the sql I have is this: 到目前为止,我拥有的SQL是这样的:

SELECT 
mf.idmy_form, 
mf.summary, 
mf.station, 
a.action_id, 
a.action_required, 
a.action_date 
FROM my_form mf
LEFT JOIN (
SELECT 
   act.action_id, 
   act.action_required, 
   act.action_date, 
   fa.idmy_form 
FROM form_action fa
JOIN actions act 
 ON fa.action_id = act.action_id 
 AND act.action_required = 'Close'
) a 
ON a.idmy_form = mf.idmy_form

This returns the following results: 这将返回以下结果:

# idmy_form, summary, station, action_id, action_required, action_date
'1', 'OSR puncture', 'Thornbury', '203', 'Close', '2013-01-06 00:00:00' 
'2', 'Windscreen wiper broke', 'Bristol', NULL, NULL, NULL
'3', 'Out of screenwash', 'Cardiff', '403', 'Close', '2013-09-17 00:00:00'
'3', 'Out of screenwash', 'Cardiff', '405', 'Close', '2013-09-23 00:00:00'

Ideally I would like to only retrieve the most recent by date_completed for each header record where the related actions have a action_required = 'Close'. 理想情况下,我只想为每个标头记录(由相关动作具有action_required ='Close')检索最新的date_completed。 So eliminating the third row from the results above. 因此,从上面的结果中删除第三行。

I have tried adding group by on the inner select, but it throws an oracle not a group by expression of missing right parenthesis. 我已经尝试在内部选择上添加group by,但是它会通过缺少右括号的表达式抛出一个oracle而不是group。

Add a subquery to get the MAX() date. 添加一个子查询以获取MAX()日期。 Here are more ways to do this described. 这里介绍了执行此操作的更多方法。

SELECT 
mf.idmy_form, 
mf.summary, 
mf.station, 
a.action_id, 
a.action_required, 
a.action_date 
FROM my_form mf
LEFT JOIN (
SELECT 
   act.action_id, 
   act.action_required, 
   act.action_date, 
   fa.idmy_form 
FROM form_action fa
JOIN actions act 
 ON fa.action_id = act.action_id 
 AND act.action_required = 'Close'
WHERE action_date = (SELECT MAX(action_date) 
                     FROM actions 
                     JOIN form_action USING(action_id)
                     WHERE actions.action_required = 'Close'
                     AND fa.idmy_form = form_action.idmy_form)
) a 
ON a.idmy_form = mf.idmy_form

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

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