Oracle SQL:如何连接文本直到达到最大大小
Posted
技术标签:
【中文标题】Oracle SQL:如何连接文本直到达到最大大小【英文标题】:Oracle SQL: how to concatenate text until reaches maximum size 【发布时间】:2021-05-27 10:11:33 【问题描述】:我有一个案例:
一些要重现的脚本:
create table test_Txt(
id number(1),
text varchar(2000)
);
insert into test_txt values(1,'BRUG HIDDEN 4. The student is obliged to justify the absence from educational classes within the specified period:
1) parents or legal guardians excuse the student''s absence by the end of the second week of the following month / for the previous month / in justified cases (e.g. a visit to a doctor), the parent or legal guardian may exempt some classes in person or in writing;
2) in the event of a student''s resignation from participation in non-compulsory classes, written information from the parent or legal guardian is required;
3) expected absence of a student for more than one week (e.g. stay in a sanatorium, hospital, chronic disease), parents or legal guardians are obliged to notify the class teacher within 3 days;
4) if the absence is not excused within the prescribed period, the class teacher explains the reasons for the absence with the parents or legal guardians;
5) information about frequent absenteeism of students is passed to the school pedagogue, parents are called
6)xfasdfkjasdgkdsethkgjaskgjbsajgbkjsdguihifafa');
insert into test_txt values(2,'BRUG HIDDEN 4. The student is obliged to justify the absence from educational classes within the specified period:
1) parents or legal guardians excuse the student''s absence by the end of the second week of the following month / for the previous month / in justified cases (e.g. a visit to a doctor), the parent or legal guardian may exempt some classes in person or in writing;
2) in the event of a student''s resignation from participation in non-compulsory classes, written information from the parent or legal guardian is required;
3) expected absence of a student for more than one week (e.g. stay in a sanatorium, hospital, chronic disease), parents or legal guardians are obliged to notify the class teacher within 3 days;
4) if the absence is not excused within the prescribed period, the class teacher explains the reasons for the absence with the parents or legal guardians;
5) information about frequent absenteeism of students is passed to the school pedagogue, parents are called
6)xfasdfkjasdgkdsethkgjaskgjbsajgbkjsdguihifafa');
insert into test_txt values(3,'BRUG HIDDEN 4. The student is obliged to justify the absence from educational classes within the specified period:
1) parents or legal guardians excuse the student''s absence by the end of the second week of the following month / for the previous month / in justified cases (e.g. a visit to a doctor), the parent or legal guardian may exempt some classes in person or in writing;
2) in the event of a student''s resignation from participation in non-compulsory classes, written information from the parent or legal guardian is required;
3) expected absence of a student for more than one week (e.g. stay in a sanatorium, hospital, chronic disease), parents or legal guardians are obliged to notify the class teacher within 3 days;
4) if the absence is not excused within the prescribed period, the class teacher explains the reasons for the absence with the parents or legal guardians;
5) information about frequent absenteeism of students is passed to the school pedagogue, parents are called
6)xfasdfkjasdgkdsethkgjaskgjbsajgbkjsdguihifafa');
insert into test_txt values(4,'BRUG HIDDEN 4. The student is obliged to justify the absence from educational classes within the specified period:
1) parents or legal guardians excuse the student''s absence by the end of the second week of the following month / for the previous month / in justified cases (e.g. a visit to a doctor), the parent or legal guardian may exempt some classes in person or in writing;
2) in the event of a student''s resignation from participation in non-compulsory classes, written information from the parent or legal guardian is required;
3) expected absence of a student for more than one week (e.g. stay in a sanatorium, hospital, chronic disease), parents or legal guardians are obliged to notify the class teacher within 3 days;
4) if the absence is not excused within the prescribed period, the class teacher explains the reasons for the absence with the parents or legal guardians;
5) information about frequent absenteeism of students is passed to the school pedagogue, parents are called
6)xfasdfkjasdgkdsethkgjaskgjbsajgbkjsdguihifafa');
insert into test_txt values(5,'BRUG HIDDEN 4. The student is obliged to justify the absence from educational classes within the specified period:
1) parents or legal guardians excuse the student''s absence by the end of the second week of the following month / for the previous month / in justified cases (e.g. a visit to a doctor), the parent or legal guardian may exempt some classes in person or in writing;
2) in the event of a student''s resignation from participation in non-compulsory classes, written information from the parent or legal guardian is required;
3) expected absence of a student for more than one week (e.g. stay in a sanatorium, hospital, chronic disease), parents or legal guardians are obliged to notify the class teacher within 3 days;
4) if the absence is not excused within the prescribed period, the class teacher explains the reasons for the absence with the parents or legal guardians;
5) information about frequent absenteeism of students is passed to the school pedagogue, parents are called
6)xfasdfkjasdgkdsethkgjaskgjbsajgbkjsdguihifafa');
insert into test_txt values(6,'BRUG HIDDEN 4. The student is obliged to justify the absence from educational classes within the specified period:
1) parents or legal guardians excuse the student''s absence by the end of the second week of the following month / for the previous month / in justified cases (e.g. a visit to a doctor), the parent or legal guardian may exempt some classes in person or in writing;
2) in the event of a student''s resignation from participation in non-compulsory classes, written information from the parent or legal guardian is required;
3) expected absence of a student for more than one week (e.g. stay in a sanatorium, hospital, chronic disease), parents or legal guardians are obliged to notify the class teacher within 3 days;
4) if the absence is not excused within the prescribed period, the class teacher explains the reasons for the absence with the parents or legal guardians;
5) information about frequent absenteeism of students is passed to the school pedagogue, parents are called
6)xfasdfkjasdgkdsethkgjaskgjbsajgbkjsdguihifafa');
然后选择:
SELECT t.*,FLOOR((sum_text)/3000)
FROM (SELECT id,
TO_CHAR(text) AS text,
LENGTH(text) AS len_Text,
SUM(LENGTH(text)) OVER(ORDER BY id DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS sum_text
FROM test_txt) t;
我想要实现的是连接文本,直到它们的总和小于 3000。如果更大,那么另一个文本。
ID LEN_TEXT SUM_TEXT FLOOR((SUM_TEXT)/3000)
6 1039 1039 0
5 1039 2078 0
4 1039 3117 1
3 1039 4156 1
2 1039 5195 1
1 1039 6234 2
问题来了:当你对它们求和时,第 1 组的文本大于 3000。id = 2 的文本应该有第 2 组,但它是 1。
有人有想法吗? 谢谢
【问题讨论】:
【参考方案1】:从 Oracle 12 开始,您可以使用MATCH_RECOGNIZE
:
SELECT id,
LENGTH( text ) AS text_len,
match_num,
SUM( LENGTH( text ) ) OVER ( PARTITION BY match_num ) AS total_len
FROM test_txt
MATCH_RECOGNIZE(
ORDER BY id
MEASURES
MATCH_NUMBER() - 1 AS match_num
ALL ROWS PER MATCH
PATTERN ( a+ )
DEFINE
A AS SUM( LENGTH( text )) <= 3000
)
对于您的样本数据,输出:
ID TEXT_LEN MATCH_NUM TOTAL_LEN 1 1039 0 2078 2 1039 0 2078 3 1039 1 2078 4 1039 1 2078 5 1039 2 2078 6 1039 2 2078
db小提琴here
【讨论】:
不错!我在哪里可以学习这个 match_recoginze? @q4za4 只需将“oracle match_recognize”放入搜索引擎,您应该会获得大量示例,语法在 Oracle 文档中“行模式子句”下的SELECT
语句中。
以上是关于Oracle SQL:如何连接文本直到达到最大大小的主要内容,如果未能解决你的问题,请参考以下文章
如何让 UIScrollView 适合其内容大小,直到在自动布局中滚动之前达到最大值