最早的日期作为开始日期和最新日期作为结束日期

发布于 2025-01-20 02:08:54 字数 275 浏览 4 评论 0原文

我有一个要求,我需要获取最早的日期作为开始日期,如果存在最晚日期,那么我需要将其作为结束日期,如果最新日期为空白,这意味着该人仍然活跃,那么我需要将其作为空白。

我在日期字段上使用了最小和最大,但如果日期不存在,我的最新日期字段不会捕获为空白。

输入图片此处描述

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.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以心动 2025-01-27 02:08:54

如果你想获取最早的start_date,通过ID。并且还要带上 End_date 字段中的任何内容 - 无论它是 NULL 还是有日期。然后,您可以首先按 ID 进行分组(在给定的示例中这不是唯一的),然后在 start_date 上使用 MIN() 。然后获取这些值属于哪一行,从而获得 End_date。这是可行的,但如果您有多个具有相同 ID 的开始日期,事情就会变得复杂 - 在这种情况下,我们需要更多示例数据,并对其应该如何工作进行更多解释。但是,这里是:

小提琴: https://www.db-fiddle.com/f /o2NyDpAc76TLYdmGFGHqag/3

CREATE TABLE my_table (
  ID int,
  Start_Date date,
  End_date date null
);
INSERT INTO my_table (ID,Start_Date, End_date) 
VALUES 
(1,'2021-01-01', '2022-04-05'),
(1,'2022-01-01', '2022-04-02'),
(2,'2022-07-01', '2022-05-07'),
(2,'2022-01-01', null);

SELECT a.*
FROM my_table a
join (SELECT 
ID,
MIN(my_table.Start_date) as 'Start_date'
FROM my_table
GROUP BY ID) jn
on a.ID=jn.ID and a.Start_date=jn.Start_date

源表:

IDStart_DateEnd_date
1'2021-01-01''2022-04-05'
1'2022-01-01'2022-04-02
2'2022-07-01''2022-05-07'
2'2022-01-01'NULL

结果表:

IDStart_DateEnd_date
1'2021-01-01''2022-04-05'
2'2022-01-01'

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

CREATE TABLE my_table (
  ID int,
  Start_Date date,
  End_date date null
);
INSERT INTO my_table (ID,Start_Date, End_date) 
VALUES 
(1,'2021-01-01', '2022-04-05'),
(1,'2022-01-01', '2022-04-02'),
(2,'2022-07-01', '2022-05-07'),
(2,'2022-01-01', null);

SELECT a.*
FROM my_table a
join (SELECT 
ID,
MIN(my_table.Start_date) as 'Start_date'
FROM my_table
GROUP BY ID) jn
on a.ID=jn.ID and a.Start_date=jn.Start_date

Source table:

IDStart_DateEnd_date
1'2021-01-01''2022-04-05'
1'2022-01-01'2022-04-02
2'2022-07-01''2022-05-07'
2'2022-01-01'NULL

Results table:

IDStart_DateEnd_date
1'2021-01-01''2022-04-05'
2'2022-01-01'NULL
吐个泡泡 2025-01-27 02:08:54

这可能有效:

SELECT ID, MIN(start_date) Start_Date, 
    NULLIF(MAX(COALESCE(end_date,'29991231')), '29991231') End_Date
FROM MyTable
GROUP BY ID

在这里查看它的工作原理:

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=5febc25e9c79840fe6aa2e55d77cf5d0

至少它会根据可用的示例数据给出正确的结果。但是,如果开始日期较早的记录具有 null 结束日期,并且开始日期较晚的记录确实具有结束日期,则仍会显示 null。在真实数据中,这种情况很可能永远不会发生,但真实数据往往会很混乱,即使它不应该如此。


要真正正确地做到这一点,您需要找到具有最新开始日期的整行,然后查看该行的结束日期值。幸运的是,我们有一个很棒的计算行数的方法:row_number() 窗口函数:

SELECT ID, Start_Date, End_Date
FROM (
    SELECT ID, Start_Date, End_Date, 
        row_number() over (PARTITION BY ID ORDER BY Start_Date DESC) rn
    FROM MyTable
) t0
WHERE rn=1

但这只是解决方案的一部分。现在应该始终具有正确的 End_Date,但通常会具有错误的 Start_Date。我们可以更新它来修复该错误,如下所示:

SELECT ID, (SELECT MIN(Start_Date) FROM MyTable t WHERE t.ID=t0.ID) Start_Date, End_Date
FROM (
    SELECT ID, Start_Date, End_Date, 
        row_number() over (PARTITION BY ID ORDER BY Start_Date DESC) rn
    FROM MyTable
) t0
WHERE rn=1

现在我们将始终得到正确的结果。

在这里查看它的工作原理:

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=4b7d4cba4849eee9ba3bf978cebfc3bf


最后,所有这些假设您有一个使用 nullDateTime< 的合理架构/code> 值,而不是使用 varchar 和空字符串的不合理架构。如果您确实遇到了后者,则架构设计确实损坏,您应该修复它

这还假设至少是 MySql 8.0。如果您使用的是比这更旧的东西,请表示哀悼。 5.7 及更早版本植根于 2006 年的基本设计,并不真正符合现代数据库平台的资格。

This might work:

SELECT ID, MIN(start_date) Start_Date, 
    NULLIF(MAX(COALESCE(end_date,'29991231')), '29991231') End_Date
FROM MyTable
GROUP BY ID

See it work here:

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=5febc25e9c79840fe6aa2e55d77cf5d0

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 a null 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:

SELECT ID, Start_Date, End_Date
FROM (
    SELECT ID, Start_Date, End_Date, 
        row_number() over (PARTITION BY ID ORDER BY Start_Date DESC) rn
    FROM MyTable
) t0
WHERE rn=1

But this is only part of the solution. This should now always have the right End_Date, but will usually have the wrong Start_Date. We can update it to fix that error like this:

SELECT ID, (SELECT MIN(Start_Date) FROM MyTable t WHERE t.ID=t0.ID) Start_Date, End_Date
FROM (
    SELECT ID, Start_Date, End_Date, 
        row_number() over (PARTITION BY ID ORDER BY Start_Date DESC) rn
    FROM MyTable
) t0
WHERE rn=1

And now we will always get the right result.

See it work here:

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=4b7d4cba4849eee9ba3bf978cebfc3bf


Finally, all this assumes you have a reasonable schema using null and DateTime values, and not an unreasonable schema using varchar 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文