最早的日期作为开始日期和最新日期作为结束日期
我有一个要求,我需要获取最早的日期作为开始日期,如果存在最晚日期,那么我需要将其作为结束日期,如果最新日期为空白,这意味着该人仍然活跃,那么我需要将其作为空白。
我在日期字段上使用了最小和最大,但如果日期不存在,我的最新日期字段不会捕获为空白。
I Have a requirement where I need to get earliest date as start date and If latest date is present then I need to have it as end date, if latest date is blanks which means the person is still active then I need to have it as blanks.
I used Min and Max on date fields but My latest date field is not capturing as Blanks if date is absent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想获取最早的start_date,通过ID。并且还要带上 End_date 字段中的任何内容 - 无论它是 NULL 还是有日期。然后,您可以首先按 ID 进行分组(在给定的示例中这不是唯一的),然后在 start_date 上使用 MIN() 。然后获取这些值属于哪一行,从而获得 End_date。这是可行的,但如果您有多个具有相同 ID 的开始日期,事情就会变得复杂 - 在这种情况下,我们需要更多示例数据,并对其应该如何工作进行更多解释。但是,这里是:
小提琴: https://www.db-fiddle.com/f /o2NyDpAc76TLYdmGFGHqag/3
源表:
结果表:
If you want to get the earliest start_date, by ID. And also bring with whatever is in the End_date field - No matter if it is NULL, or has an date. Then you can first get group by ID(which is not unique in your example given), then use MIN() on start_date. Then you fetch which row these values belong to, and thereby get the End_date. This works, but if you've got several start dates with the same ID, that complicates things - and in that case we need some more example data with a bit mor explanation of how it is supposed to work. But, here goes:
Fiddle: https://www.db-fiddle.com/f/o2NyDpAc76TLYdmGFGHqag/3
Source table:
Results table:
这可能有效:
在这里查看它的工作原理:
至少它会根据可用的示例数据给出正确的结果。但是,如果开始日期较早的记录具有
null
结束日期,并且开始日期较晚的记录确实具有结束日期,则仍会显示null
。在真实数据中,这种情况很可能永远不会发生,但真实数据往往会很混乱,即使它不应该如此。要真正正确地做到这一点,您需要找到具有最新开始日期的整行,然后查看该行的结束日期值。幸运的是,我们有一个很棒的计算行数的方法:
row_number()
窗口函数:但这只是解决方案的一部分。现在应该始终具有正确的
End_Date
,但通常会具有错误的Start_Date
。我们可以更新它来修复该错误,如下所示:现在我们将始终得到正确的结果。
在这里查看它的工作原理:
最后,所有这些假设您有一个使用
null
和DateTime< 的合理架构/code> 值,而不是使用
varchar
和空字符串的不合理架构。如果您确实遇到了后者,则架构设计确实损坏,您应该修复它。这还假设至少是 MySql 8.0。如果您使用的是比这更旧的东西,请表示哀悼。 5.7 及更早版本植根于 2006 年的基本设计,并不真正符合现代数据库平台的资格。
This might work:
See it work here:
At least it will seem to give the right results based on the sample data available. However, this would still show a
null
if a record with an earlier start date has anull
end date, and a record with a later start date does have an end date. It's likely this should never happen in real data, but then real data tends to be messy even when it shouldn't be.To really do this properly, you need to find the whole row with the latest start date and then look at the end date value from that row. Fortunately, we have a great way to count rows: the
row_number()
windowing function:But this is only part of the solution. This should now always have the right
End_Date
, but will usually have the wrongStart_Date
. We can update it to fix that error like this:And now we will always get the right result.
See it work here:
Finally, all this assumes you have a reasonable schema using
null
andDateTime
values, and not an unreasonable schema usingvarchar
and empty strings. If the latter really is your situation the schema design really is BROKEN and you should fix it.This also assumes at least MySql 8.0. If you're using something older than that, condolences. 5.7 and earlier are rooted in basic design from 2006, and don't really qualify as a modern database platform.