Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

stored procedures - How to store SQL result in a variable and parse the result to identify a possible pattern?

The query below

INSERT INTO temp 
SELECT esd, 
       'E' 
FROM   test_data_sovlp 
WHERE  esd IS NOT NULL 
UNION ALL 
SELECT td, 
       CASE is_db 
         WHEN 0 THEN 'S' 
         WHEN 1 THEN 'H' 
       END AS FLAG 
FROM   test_data_sovlp 
WHERE  td IS NOT NULL 

return the following data:

|----------|----------|
|  DT      |  FLAG    |
|----------|----------|
|  10      |  E       |
|  20      |  H       |
|  30      |  E       |
|  40      |  E       |
|  50      |  E       |
|  60      |  S       |
|  70      |  H       |
|  75      |  E       |
|  80      |  H       |
|  100     |  H       |
|----------|----------|

when run against this table:

|----------|----------|----------|----------|----------|
|    ID    |   ESD    |  TD      |   IS_DB  | TEST_SET |
|----------|----------|----------|----------|----------|
|    1     |  10      |  20      |    1     |    2     |
|    2     |  30      |  (null)  |    1     |    2     |
|    3     |  40      |  (null)  |    1     |    2     |
|    4     |  50      |  60      |    0     |    2     |
|    5     |  (null)  |  70      |    1     |    2     |
|    6     |  75      |  100     |    1     |    2     |
|    7     |  (null)  |  80      |    1     |    2     |
|----------|----------|----------|----------|----------|

Note: See the demo here or my previous post here for more details.

What I'm interested in is to concatenate the FLAG value return by the query above, in the DT order.

So for the query above, the concatenation (let's call it q_result) value is: q_result = EHEEESEHH.

I want then to parse then q_result by block of 2 characters to detect the possible presence of any of the following sequence:

HH      EE      HS      SE

During the parse, if a pattern match anywhere in q_result, the proc I would like to write must return 0. If no pattern match then the proc must return 1.

Question

How can this be done ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understand correctly, you can do something like this:

select count(*)
from (select listagg(flag) within group (order by dt) as flags
      from temp
     ) x
where not regexp_like(flags, 'HH|EE|HS|SE');

Alternatively, you can use lag():

select (case when count(*) = sum(case when flag2 not in ('HH', 'EE', 'HS', 'SE')
             then 1 else 0
        end) as return_value
from (select t.*,
             (lag(flag) over (order by dt) || flag) as flag2
      from temp
     ) t;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...