I'm sure I'm not understanding this, but this is defined as
PATTERN: Specifying the Pattern to Match PATTERN ( ) The pattern defines a valid sequence of rows that represents a match. The pattern is defined like a regular expression (regex) and is built from symbols, operators, and quantifiers.
An example was:
For example, suppose that symbol S1 is defined as stock_price < 55, and symbol S2 is defined as stock price > 55. The following pattern specifies a sequence of rows in which the stock price increased from less than 55 to greater than 55:
PATTERN (S1 S2)
So if I do
create or replace table names (id int, name varchar (500), groupid int);
insert into names
select 1, 'andrew', 1
union
select 2, 'andrew2', 1
union
select 3, '3andrew', 1
And then I do
select * from names
match_recognize(
partition by groupid order by id
measures
classifier() as "classifier"
all rows per match
pattern (test test2)
define test as startswith(name, 'and'),
test2 as endswith(name, 'rew')
) t
;
Why do I not get 'andrew' as a record return? If I put either test in the pattern, it does show it. When I put both in, it does not. Instead it is showing 3andrew and andrew2 as the record result which is unexpected for me because the example lead me to believe it works like an AND. Any help is appreciated.
so your pattern
pattern (test test2)as I understand it is say find a row wheretestpasses and latter row wheretest2passes.MATCH_RECOGNIZE says of the operator
(space) which you have between your two rules:But I suspect your question is why does row id 1 not match on
testand then row 3 match ontest2.. and I believe it is theAFTER MATCH SKIPthe defaults toPAST LAST ROWwhich find row 1 & 2 match test, so it starts after the last, thus you only get one match. Not that I could use that section to turn off this behavior.If I change the input data a little:
this gives:
So row 10 can work, but it seems in your data 2 binds closer to 3, but in my data 30 does not match 50. Also of note is you have no order by clause in you match, which means you might get non-deterministic results. Unless you have re-sorted you data elsewhere..
thus change adding a
partitionandorderclause means both andrews now trigger: