admin管理员组文章数量:1422504
We have a table containing 100 million records where we need to find all dates between date ranges present in each row of the table.
Tabl name: item_details
----------------------------------------
Item_no | item_type | active_from | active_to |
----------------------------------------
10001|AAC|2023-01-01|2099-12-31|
--------------------------------
10002|BOC|2024-06-03|2025-10-01
--------------------------------
10003|COD|2025-01-01|2025-01-01
---------------------------------
Output
Item_no|item_type|active_dates
----------------------------------------
10001|AAC|2023-01-01|
10001|AAC|2023-01-02|
10001|AAC|2023-01-03|
:
:
10001|AAC|2099-12-30|
10001|AAC|2099-12-31|
10002|BOC|2024-06-03|
10002|BOC|2024-06-04|
:
:
10002|BOC|2025-09-30|
10002|BOC|2025-10-01|
10003|COD|2025-01-01|
--------------------------------
I tried cross join and connect by level, but it is taking huge time to print result as date is almost 75 years +..so looping through each record, it gets hung.
tried connect by level by passing date from each row, but query got hang without any results...
We have a table containing 100 million records where we need to find all dates between date ranges present in each row of the table.
Tabl name: item_details
----------------------------------------
Item_no | item_type | active_from | active_to |
----------------------------------------
10001|AAC|2023-01-01|2099-12-31|
--------------------------------
10002|BOC|2024-06-03|2025-10-01
--------------------------------
10003|COD|2025-01-01|2025-01-01
---------------------------------
Output
Item_no|item_type|active_dates
----------------------------------------
10001|AAC|2023-01-01|
10001|AAC|2023-01-02|
10001|AAC|2023-01-03|
:
:
10001|AAC|2099-12-30|
10001|AAC|2099-12-31|
10002|BOC|2024-06-03|
10002|BOC|2024-06-04|
:
:
10002|BOC|2025-09-30|
10002|BOC|2025-10-01|
10003|COD|2025-01-01|
--------------------------------
I tried cross join and connect by level, but it is taking huge time to print result as date is almost 75 years +..so looping through each record, it gets hung.
tried connect by level by passing date from each row, but query got hang without any results...
Share Improve this question edited Jan 18 at 0:26 Narasimhan M asked Jan 17 at 17:50 Narasimhan MNarasimhan M 193 bronze badges 4 |1 Answer
Reset to default 0I'd tend to materialize the connect by level into a table
CREATE TABLE dates (t DATE) PCTFREE 0;
INSERT INTO dates (t)
SELECT DATE '2022-12-31' + level FROM dual CONNECT BY LEVEL < 365.25*77;
EXEC dbms_stats.gather_table_stats(null, 'dates');
and then join this table to your 100 million row table
SELECT i.item_no, i.item_type, d.t as active_dates
FROM item_details i
JOIN dates d ON d.t BETWEEN i.active_from AND i.active_to;
本文标签:
版权声明:本文标题:sql - Find all dates between 2 different date ranges for each row in a huge volume of table data - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745350876a2654759.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
2099-12-31
then that average may be underestimating. At any rate, this is very solvable with a calendar table if you are prepared for the sizable result set. – JNevill Commented Jan 17 at 18:21