gpt4 book ai didi

sql - 使用模型子句来扩展日期

转载 作者:行者123 更新时间:2023-12-03 06:23:45 27 4
gpt4 key购买 nike

我有几种涉及日期范围的不同类型的数据,我想将它们合并在一起,但同时按天分割。因此 3 天的数据将产生三行:

start    primary_key
start+1 primary_key
start+2 primary_key

我一直在尝试使用 10g 中 select 语句的模型子句,并正在寻找实现此目的的最佳方法。目前,我正在加入涵盖所有可能日期的日期范围(选择最短(开始日期)、最长(结束日期))。我更愿意选择数据并添加更多行以将其转换为每日数据集。

编辑:

我已经设法想出(现在包括示例数据):

    SELECT * FROM (
SELECT 123 req_code,
345 req_par_code,
TO_DATE('01-03-2010', 'dd-mm-yyyy') req_start_date,
TO_DATE('05-03-2010', 'dd-mm-yyyy') req_end_date
FROM dual
)

MODEL
PARTITION BY (req_code)
DIMENSION BY (0 d)
MEASURES (SYSDATE dt, req_par_code, req_start_date, req_end_date)
RULES ITERATE(365) UNTIL (dt[iteration_number] >= TRUNC(req_end_date[0])) (
dt[iteration_number] = NVL(dt[iteration_number-1] + 1, TRUNC(req_start_date[0])),

--Copy data across
req_par_code[ iteration_number ] = req_par_code[0],
req_start_date[ iteration_number ] = req_start_date[0],
req_end_date[ iteration_number ] = req_end_date[0]
)
ORDER BY dt, req_code;

最佳答案

您可以使用 MODEL 子句来生成行,这是一个小示例:

SQL> SELECT * FROM t_data;

PK START_DATE END_DATE
---------- ----------- -----------
1 20/01/2010 20/01/2010
2 21/01/2010 23/01/2010
3 24/01/2010 27/01/2010

SQL> SELECT pk, start_date, end_date FROM t_data
2 MODEL
3 PARTITION BY (pk)
4 DIMENSION BY (0 AS i)
5 MEASURES(start_date, end_date)
6 RULES
7 ( start_date[FOR i
8 FROM 1 TO end_date[0]-start_date[0]
9 INCREMENT 1] = start_date[0] + cv(i),
10 end_date[ANY] = start_date[CV()] + 1
11 )
12 ORDER BY 1,2;

PK START_DATE END_DATE
---------- ----------- -----------
1 20/01/2010 21/01/2010
2 21/01/2010 22/01/2010
2 22/01/2010 23/01/2010
2 23/01/2010 24/01/2010
3 24/01/2010 25/01/2010
3 25/01/2010 26/01/2010
3 26/01/2010 27/01/2010
3 27/01/2010 28/01/2010

关于sql - 使用模型子句来扩展日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2102058/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com