连接表上的最大日期问题
例如,使用日期:03-16-2011 作为今天的日期
表记录:record_id、record_status、record_subscribed_date
表 record_change_log:change_log_id、record_id、new_record_status、record_change_date、old_record_status
(将只有一个 record_id) 表记录数据:(
1, key, 2011-03-14 08:24:12.04051-04
2, manual, 2011-03-15 09:56:32.04051-04
3, entered, 2011-03-16 07:44:01.04051-04
将有多个record_id和record_change_date) 表 record_change_log 数据:
100, 3, entered, 2011-03-16 09:54:01.04051-04, ''
101, 3, updated, 2011-03-16 09:55:01.04051-04, entered
102, 2, manual, 2011-03-15 09:56:32.04051-04, ''
103, 3, valid, 2011-03-16 10:00:01.04051-04, updated
104, 2, updated, 2011-03-15 10:01:51.04051-04, manual
105, 3, returned, 2011-03-16 10:11:22.04051-04, valid
106, 1, updated, 2011-03-16 11:11:11.04051-04, key
107, 23, manual, ...
108, 100, test, ...
我需要的是每天返回所有 record_id 以及最后输入的更改日志。 因此,record_id 1 将返回:
106, 1, updated, 2011-03-16 11:11:11.04051-04, key
并且 record_id 2 不会返回,因为日期不是今天:
104, 2, updated, 2011-03-15 10:01:51.04051-04, manual
并且 record_id 3 将返回:
105, 3, returned, 2011-03-16 10:11:22.04051-04, valid
但在查询中,我只需要返回的 record_id 和 new_record_status 已随今天的日期更改,因此 record_id 2 将不会在结果中返回,但 1 和 3 会返回。
现在查询的另一部分是我只需要记录表中的 record_id,因此更改日志表中的其他记录也不会返回:
107, 23, manual, ...
108, 100, test, ...
因此,所需的结果是这些是更改日志中的最后一个条目表以及它们是记录表中的记录:
105, 3, returned, 2011-03-16 10:11:22.04051-04, valid
106, 1, updated, 2011-03-16 11:11:11.04051-04, key
我尝试连接表,但我似乎无法限制更改日志表中的结果。我可以限制更改日志表中的结果,但无法选择我需要的所有记录,这会导致 GROUP BY 抱怨某些事情
For the example use date: 03-16-2011 as today's date
table record: record_id, record_status, record_submitted_date
table record_change_log: change_log_id, record_id, new_record_status, record_change_date, old_record_status
(Will only have one record_id)
table record data:
1, key, 2011-03-14 08:24:12.04051-04
2, manual, 2011-03-15 09:56:32.04051-04
3, entered, 2011-03-16 07:44:01.04051-04
(Will have multiple record_id's and record_change_date's)
table record_change_log data:
100, 3, entered, 2011-03-16 09:54:01.04051-04, ''
101, 3, updated, 2011-03-16 09:55:01.04051-04, entered
102, 2, manual, 2011-03-15 09:56:32.04051-04, ''
103, 3, valid, 2011-03-16 10:00:01.04051-04, updated
104, 2, updated, 2011-03-15 10:01:51.04051-04, manual
105, 3, returned, 2011-03-16 10:11:22.04051-04, valid
106, 1, updated, 2011-03-16 11:11:11.04051-04, key
107, 23, manual, ...
108, 100, test, ...
What I need is to return all record_id's daily with the last entered change log.
So record_id 1 would return:
106, 1, updated, 2011-03-16 11:11:11.04051-04, key
and record_id 2 would not be return as the date is not today:
104, 2, updated, 2011-03-15 10:01:51.04051-04, manual
and record_id 3 would return:
105, 3, returned, 2011-03-16 10:11:22.04051-04, valid
But in the query I only need the record_id and the new_record_status returned that have changed with today's date, so record_id 2 would not be returned in the results but 1 and 3 would.
Now the other part of the query is I only need record_id's that are in the records table, so these other records in the change log table would not get returned as well:
107, 23, manual, ...
108, 100, test, ...
So the desired results are as these are the last entries in the change log table as well as they are records in the record table:
105, 3, returned, 2011-03-16 10:11:22.04051-04, valid
106, 1, updated, 2011-03-16 11:11:11.04051-04, key
I have tried to join the tables but I can't seem to limit the results in the change log table. And I can limit the results in the change log table but not able to select all the records I need would out the GROUP BY complaining about something
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以加入相关的子查询,例如...
(我将“仅限今天”部分留给您,因为我认为“更改日志中只有一条记录”是您需要帮助的部分。 )
You could join on a correlated sub_query, comething such as...
(I've left the "today only" parts up to you, as I think the "just one record from the change log" is the part you wanted help on.)