我正在使用PL/SQL Developer v10
开发Oracle 11g
数据库。
在我们的系统中,我们有一个问答表,对于每个被回答的问题,我需要对每个客户进行“扁平化”。
一组问题在问题描述中有特定的代码(PIFQ
),这使得以下脚本成为可能。简单地说是UPPER(substr(q.questiondescription,1,6))
,然后转到可能的代码列表上。
select * from (
select
tqs.transactiondetailid as transactiondetailid,
q.productid as productid,
tqs.answer as QAnswer,
UPPER(substr(q.questiondescription,1,6)) as QDesc,
tqs.transactionversion as transactionversion
from TRANSACTIONDETAILQSHIS tqs
inner join question q on q.questionid = tqs.questionid and
q.questiondescription like 'PIFQ%'
) pivot (
min(QAnswer) for QDesc in (
'PIFQ01','PIFQ02','PIFQ03','PIFQ05','PIFQ06','PIFQ07','PIFQ08','PIFQ09','PIFQ10',
'PIFQ11','PIFQ12','PIFQ13','PIFQ14','PIFQ15','PIFQ16','PIFQ17','PIFQ18','PIFQ19','PIFQ20',
'PIFQ21','PIFQ22','PIFQ23','PIFQ24','PIFQ25','PIFQ26','PIFQ27','PIFQ28','PIFQ29','PIFQ30',
'PIFQ31','PIFQ32','PIFQ33','PIFQ34','PIFQ35')
)
结果是在TRANSACTIONDETAILQSHIS中回答的所有问题的一行。
现在其他的问题集有三个不同长度的代码(DT,WIF,WT)。
一些例子:
DT01. Are you married?
DT05. Do you have children?
WIF1.1.1 First Name
WIF1.2 Date Of Birth
WIF7.10 How many other properties do you own?
WIF14.3.7 Post Code
WIF15 Notes to solicitor
WT01. Will Type
没有一个代码之间有空格,后面有空格,但其他所有代码都没有。我想在这里使用REGEXP_SUBSTR
提取支点的代码,并使用相同的逻辑来捕获QDesc
。我们谈论的是1000多个问题,所以我宁愿避免列出代码。
我被困在做regex (这只是一部分,选择正确的问题代码,但在我完成它-这将是小菜一碟)。我的自动取款机是:
select UPPER(REGEXP_SUBSTR(q.questiondescription,'(WIF|DT|WT)\d{1,2}.')) from question q
它确实选择了第一个组(WIF|DT|WT)
和后面的第一个数字
DT01. Are you married?
DT05. Do you have children?
WT01. Will Type
我如何实现能够捕获.
的逻辑,以及在结束时没有它们的逻辑(WIF15
)。
WIF1.1.1 First Name
WIF1.2 Date Of Birth
WIF7.10 How many other properties do you own?
WIF14.3.7 Post Code
WIF15 Notes to solicitor
发布于 2014-10-14 03:11:25
这将适用于'(WIF|DT|WT)[([:digit:]|.)]*'
with my_data(str) as
(
select 'WIF1.1.1 First Name' from dual
union all
select 'WIF1.2 Date Of Birth' from dual
union all
select 'WIF7.10 How many other properties do you own?' from dual
union all
select 'WIF14.3.7 Post Code' from dual
union all
select 'WIF15 Notes to solicitor' from dual
)
select str, regexp_substr(str,'(WIF|DT|WT)[([:digit:]|.)]*') as result from my_data;
结果:
STR RESULT
--------------------------------------------------------
WIF1.1.1 First Name WIF1.1.1
WIF1.2 Date Of Birth WIF1.2
WIF7.10 How many other properties do you own? WIF7.10
WIF14.3.7 Post Code WIF14.3.7
WIF15 Notes to solicitor WIF15
发布于 2014-10-14 03:08:13
(WIF|DT|WT)\d{1,2}.?
尝试this.Make .
可选。
https://stackoverflow.com/questions/26359188
复制