如何打印产生 multi-like case 语句结果的匹配模式?
How do I print the matched pattern that produced the result of a multi-like case statement?
我希望将 multi-like case when 语句中的字符串字段(标题)的匹配模式作为字符串值存储在列 (matched_pattern) 中 [=30] =].
示例table:
| Title | Email |
|---------------------|------------------|
| CEO | henry@test.com |
|---------------------|------------------|
| COO | julien@test.com |
|---------------------|------------------|
我的查询是
select
title,
email,
CASE WHEN lower(title) LIKE '%cco%' OR
lower(title) LIKE '%ceo%' THEN 'CxO' ELSE null END persona
FROM table
我想要的结果是
| Title | Email | Persona |matched_pattern |
|------------|------------------|---------------------------|
| CEO | henry@test.com | CxO | '%ceo%' |
|------------|------------------|---------|-----------------|
| CcO | julien@test.com | CxO | '%cco%' |
|------------|------------------|---------|-----------------|
有没有办法将生成结果的代码存储在单独的列中?我使用了另一个案例语句来创建 matched_pattern 列
SELECT
title,
CASE WHEN lower(title) LIKE '%cco%' THEN 'CxO'
CASE WHEN lower(title) LIKE '%ceo%' THEN 'CxO'
ELSE null END persona,
CASE WHEN lower(title) LIKE '%cco%' THEN '%cco%'
CASE WHEN lower(title) LIKE '%ceo%' THEN '%ceo%'
ELSE null END matched_pattern
FROM table
有更好的方法吗?
如果你在另一个 table 或临时 table 或其他东西中实现模式,你可以简单地这样做:
SELECT *
FROM DataSource DS
LEFT JOIN Paterns P
ON DS.[Title] LIKE P.[patern]
这是 SQL 服务器上的完整工作示例:
CREATE TABLE DataSource
(
[Title] VARCHAR(12)
,[Email] VARCHAR(32)
);
CREATE TABLE Paterns
(
[patern] VARCHAR(12)
,[value] VARCHAR(12)
);
GO
INSERT INTO DataSource ([Title], [Email])
VALUES ('CEO', 'henry@test.com')
,('CEO', 'julien@test.com');
INSERT INTO Paterns ([patern], [value])
VALUES ('%ceo%', 'CxO')
,('%cco%', 'CxO')
SELECT *
FROM DataSource DS
LEFT JOIN Paterns P
ON DS.[Title] LIKE P.[patern]
我希望将 multi-like case when 语句中的字符串字段(标题)的匹配模式作为字符串值存储在列 (matched_pattern) 中 [=30] =].
示例table:
| Title | Email |
|---------------------|------------------|
| CEO | henry@test.com |
|---------------------|------------------|
| COO | julien@test.com |
|---------------------|------------------|
我的查询是
select
title,
email,
CASE WHEN lower(title) LIKE '%cco%' OR
lower(title) LIKE '%ceo%' THEN 'CxO' ELSE null END persona
FROM table
我想要的结果是
| Title | Email | Persona |matched_pattern |
|------------|------------------|---------------------------|
| CEO | henry@test.com | CxO | '%ceo%' |
|------------|------------------|---------|-----------------|
| CcO | julien@test.com | CxO | '%cco%' |
|------------|------------------|---------|-----------------|
有没有办法将生成结果的代码存储在单独的列中?我使用了另一个案例语句来创建 matched_pattern 列
SELECT
title,
CASE WHEN lower(title) LIKE '%cco%' THEN 'CxO'
CASE WHEN lower(title) LIKE '%ceo%' THEN 'CxO'
ELSE null END persona,
CASE WHEN lower(title) LIKE '%cco%' THEN '%cco%'
CASE WHEN lower(title) LIKE '%ceo%' THEN '%ceo%'
ELSE null END matched_pattern
FROM table
有更好的方法吗?
如果你在另一个 table 或临时 table 或其他东西中实现模式,你可以简单地这样做:
SELECT *
FROM DataSource DS
LEFT JOIN Paterns P
ON DS.[Title] LIKE P.[patern]
这是 SQL 服务器上的完整工作示例:
CREATE TABLE DataSource
(
[Title] VARCHAR(12)
,[Email] VARCHAR(32)
);
CREATE TABLE Paterns
(
[patern] VARCHAR(12)
,[value] VARCHAR(12)
);
GO
INSERT INTO DataSource ([Title], [Email])
VALUES ('CEO', 'henry@test.com')
,('CEO', 'julien@test.com');
INSERT INTO Paterns ([patern], [value])
VALUES ('%ceo%', 'CxO')
,('%cco%', 'CxO')
SELECT *
FROM DataSource DS
LEFT JOIN Paterns P
ON DS.[Title] LIKE P.[patern]