1

我正在使用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。我们正在讨论 100 多个问题,所以我宁愿避免列出代码。

我一直在制作正则表达式(这只是从所有问题中选择正确问题代码的部分,但在我完成之后 - 这将是小菜一碟)。我的自动取款机是:

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
4

2 回答 2

3

这将起作用'(WIF|DT|WT)[([:digit:]|.)]*'

SQLFiddle 演示

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-14T11:11:25.067 回答
0
(WIF|DT|WT)\d{1,2}.?

试试这个.。可选。

于 2014-10-14T11:08:13.360 回答