SQL Server Aggregate using like -
SQL Server Aggregate using like -
i have table 2 columns, 1 1/0 flag whether they've opened email, sec email address (ie joe123@domain.com).
opened email 0 joe123@domain.com 1 sue234@email.net ... ...
i'm trying find if patterns in user names impact open rates using regex patterns in unsure of syntax rows match , not match pattern.
for instance, can do:
select email, sum(opened) table1 email '%joe%'
but gives me rows match. i'd rows , don't match in same output.
i'd this:
pattern opened 'joe' 55 not_joe 15987 'sue' 78 not_sue 15964 ... ...
what's best way this?
if you've got list of patterns, can accomplish like/not using cross bring together (warning: performance hit).
see below example. note: can potentially improve performance of in select statement - there other options available.
declare @matchlist table (id int, pattern varchar(3)) insert @matchlist (id, pattern) values (1, 'joe') insert @matchlist (id, pattern) values (2, 'sue') declare @table1 table (email varchar(100)) insert @table1 (email) values ('joe123@domain.com') insert @table1 (email) values ('sue234@email.net') insert @table1 (email) values ('sue682@email.net') insert @table1 (email) values ('a@domain.com') insert @table1 (email) values ('b@domain.com') insert @table1 (email) values ('c@domain.com') insert @table1 (email) values ('d@domain.com') insert @table1 (email) values ('e@domain.com') select case when email '%'+pattern+'%' pattern else 'not_'+pattern end classification, count(*) opened @matchlist m cross bring together @table1 grouping m.id, case when email '%'+pattern+'%' pattern else 'not_'+pattern end order m.id
sql sql-server regex
Comments
Post a Comment