选择每个位置具有最近日期的行,并使用MariaDB按位置将每行的最近日期增加1



我有一个位置表,它有"日期列"。我必须按每组位置ID查找最近的日期,例如位置ID 1的最近日期为"2022年5月31日"。在从locationID组中找到最近的日期后,我必须在最近的日期中添加14天,并将其存储在NewDate列中。并且在该位置ID组的另一行的新日期中添加+1。

我的桌子是:

id  locationID    Date        NewDate   
1       1       31 May 2022
2       1       16 May 2022
3       1       28 Apr 2021
4       2       29 Mar 2022
5       2       22 Feb 2022
6       3       14 Jun 2022
7       3       27 Oct 2021
8       4       01 Feb 2022
9       4       04 May 2022
10      4       14 Jun 2021
11      5       01 Jun 2022
12      5       29 May 2022
13      5       20 Sep 2022
14      5       11 Aug 2022
15      5       03 Aug 2022

答案如下:

例如,对于位置ID=1

id     locationID       Date             NewDate   
1       1         31 May 2022       14 Jun 2022    // Recent Date + 14 Days - 31 May + 14 Days
2       1         16 May 2022       15 Jun 2022    // Recent Date + 15 Days - 31 May + 15 Days
3       1         28 Apr 2021       16 Jun 2022    // Recent Date + 16 Days - 31 May + 16 Days

我遇到过一些类似的帖子,发现最近的日期是这样的:

SELECT L.*
FROM Locations L 
INNER JOIN 
(SELECT locationID, MAX(Date) AS MAXdate
FROM Locations 
GROUP BY locationID) groupedL
ON L.locationID = groupedL.locationID
AND L.Date = groupedL.MAXdate

使用上面的代码,我可以找到每个位置的最近日期,但我如何添加和增加所需的天数,并将其存储到NewDate列?我是MariaDB的新手,请建议类似的帖子链接,任何参考文件或博客。我是否应该制作一些函数来执行此逻辑,并调用该函数将所需日期存储在NewDate列中?我不确定,请提出建议。非常感谢。

结果应该如下所示:

id     locationID       Date             NewDate   
1       1         31 May 2022       14 Jun 2022    // Recent Date for locationid 1 + 14 Days - 31 May + 14 Days
2       1         16 May 2022       15 Jun 2022    // Recent Date for locationid 1 + 15 Days - 31 May + 15 Days
3       1         28 Apr 2021       16 Jun 2022    // Recent Date for locationid 1 + 16 Days - 31 May + 16 Days
4       2         29 Mar 2022       12 APR 2022    // Recent Date for locationid 2 + 14 Days
5       2         22 Feb 2022       13 APR 2022    // Recent Date for locationid 2 + 15 Days
6       3         14 Jun 2022       28 JUN 2022    // Recent Date for locationid 3 + 14 Days
7       3         27 Oct 2021       29 JUN 2022    // Recent Date for locationid 3 + 15 Days
8       4         01 Feb 2022       18 MAY 2022    // Recent Date for locationid 4 + 14 Days
9       4         04 May 2022       19 MAY 2022    // Recent Date for locationid 4 + 15 Days
10      4         14 Jun 2021       20 MAY 2022    // Recent Date for locationid 4 + 16 Days
11      5         01 Jun 2022       04 OCT 2022    // Recent Date for locationid 5 + 14 Days
12      5         29 May 2022       05 OCT 2022    // Recent Date for locationid 5 + 15 Days
13      5         20 Sep 2022       06 OCT 2022    // Recent Date for locationid 5 + 16 Days
14      5         11 Aug 2022       07 OCT 2022    // Recent Date for locationid 5 + 17 Days
15      5         03 Aug 2022       08 OCT 2022    // Recent Date for locationid 5 + 18 Days

您可以使用cte:

with cte as (
select l1.*, l2.m, (select sum(l4.id < l1.id and l4.locationid = l1.locationid) from locations l4) inc from locations l1 
join (select l3.locationid, max(l3.dt) m from locations l3 group by l3.locationid) l2 on l1.locationid = l2.locationid
)
select c.id, c.locationid, c.dt, c.m + interval 14 + c.inc day from cte c

您可以使用分析窗口函数,并通过加入子查询来更新原始表(适用于MariaDB(:

update t
join (
select Id, 
Date_Add(First_Value(date) over(partition by locationId order by date desc), 
interval (13 + row_number() over(partition by locationId order by date desc)) day 
) NewDate
from t
)nd on t.id = nd.id
set t.Newdate = nd.NewDate;

请参见DB<gt;Fiddle示例

相关内容

  • 没有找到相关文章

最新更新