创建一个使用描述列中的可变子字符串数据动态填充临时列的视图
Posted
技术标签:
【中文标题】创建一个使用描述列中的可变子字符串数据动态填充临时列的视图【英文标题】:Creating a View that dynamicaly populates a temporary column with variable substring data from the description column 【发布时间】:2014-12-22 22:28:34 【问题描述】:我的 SQL 技能充其量只是从初学者到中级,所以我在这里查看我需要做的这个项目的任何帮助。我不是要求你给我我需要的代码,但是任何能帮助我指出正确方向的帮助都将不胜感激。
我使用的是 SQL Server 2008 r2,需要创建一个视图。视图将是表 tblDataHistory。该表由 5 列组成(视图中只需要 3 列)。
它们如下。
-HistoryEventID (autonumber) - not needed
-ExecutionID (int) - Needed
-EventTypeID (int) - not needed
-Timestamp (datetime) - needed
-Description (varchar(max)) - needed
视图只需要保留这 3 列 ExecutionID、Timestamp 和 Description。它还需要一列,这就是我在执行过程中迷失的地方。我需要在 Description 列中查找一些关键词短语,如果它存在于该行中,则使用该短语填充第 4 列。
我要找的词组如下
-"Conversations"
-"Error"
-"Failed"
-"On Behalf Of"
-"Acted Upon"
-"Approve"
-"Delete"
-"Route"
-"Reject"
-"Release"
-"Duplicate"
-"Restart"
-"Send"
-"Submit"
-"Comment"
-"Conversation"
-"Attach"
-"Redaction"
-"Matched"
-"Return to Start"
-"Print"
-"Started"
-"Ended"
-"Saved"
-"Backfill"
-"Reassign"
-"Note"
-"Action Button"
-"Priority"
-"Completed"
基本上,我需要创建最后一列,并根据描述列是否包含该短语来填充这些关键短语。
这似乎应该很容易做到,但我似乎在任何地方都找不到任何可以应用于我的情况的简单解决方案。关于从哪里开始有什么建议吗?
【问题讨论】:
【参考方案1】:首先创建一个关键字表格,可能如下:
CREATE TABLE keyphrases (keyPhrase VARCHAR(100))
INSERT keyphrases VALUES ('Conversations')
INSERT keyphrases VALUES ('Error')
INSERT keyphrases VALUES ('Failed')
INSERT keyphrases VALUES ('On Behalf Of')...etc
然后在您的 VIEW 中,使用 LIKE 运算符将新表左连接到您的查询:
SELECT
DH.ExecutionID,
DH.Timestamp,
DH.Description,
K.keyPhrase AS MatchedKeyPhrase
FROM
tblDataHistory DH
LEFT JOIN keyphrases K ON DH.[Description] LIKE '%' + K.keyPhrase + '%'
如果您只想包含匹配的行,请将 LEFT JOIN 更改为 just JOIN。
请注意,这些是昂贵的连接类型。明智地使用它们。使关键短语表尽可能短。
【讨论】:
【参考方案2】:在与一些同事合作后,我们想出了解决问题的方法。首先,我们“不关心”的关键字太多,所以我们缩小了列表。即使这样,我们也可以保持列表不变,只是让这个脚本更长。创建了一个视图,在填充视图的 select 语句中,我们使用 case 语句和通配符来查找我们正在寻找的字符块,并让这些字符块根据描述中的数据填充其他列。这是 Select 语句
SELECT HistoryEventID, ExecutionID, EventTypeID, Timestamp, Description,
CASE WHEN [DESCRIPTION] LIKE '%Invoice Approval started%' THEN 'STARTED' WHEN [DESCRIPTION] LIKE '%acted upon the document with the command%' AND
[DESCRIPTION] LIKE '%Submit.%' THEN 'SUBMITTED' WHEN [DESCRIPTION] LIKE '%on behalf of%' AND [DESCRIPTION] LIKE '%clicked the action button %' AND
[DESCRIPTION] LIKE '%Submit%' THEN 'SUBMITTED' WHEN [DESCRIPTION] LIKE '%acted upon the document with the command%' AND
[DESCRIPTION] LIKE '%Send to AP.%' THEN 'SENDTOAP' WHEN [DESCRIPTION] LIKE '%on behalf of%' AND [DESCRIPTION] LIKE '%clicked the action button %' AND
[DESCRIPTION] LIKE '%Send to AP.%' THEN 'SENDTOAP' WHEN [DESCRIPTION] LIKE '%acted upon the document with the command%' AND
[DESCRIPTION] LIKE '%Release to GP.%' THEN 'RELEASED' WHEN [DESCRIPTION] LIKE '%on behalf of%' AND [DESCRIPTION] LIKE '%clicked the action button %' AND
[DESCRIPTION] LIKE '%Release to GP.%' THEN 'RELEASED' WHEN [DESCRIPTION] LIKE '%acted upon the document with the command%' AND
[DESCRIPTION] LIKE '%Reject.%' THEN 'REJECTED' WHEN [DESCRIPTION] LIKE '%on behalf of%' AND [DESCRIPTION] LIKE '%clicked the action button %' AND
[DESCRIPTION] LIKE '%Reject%' THEN 'REJECTED' WHEN [DESCRIPTION] LIKE '%acted upon the document with the command%' AND
[DESCRIPTION] LIKE '%Route.%' THEN 'ROUTED' WHEN [DESCRIPTION] LIKE '%on behalf of%' AND [DESCRIPTION] LIKE '%clicked the action button %' AND
[DESCRIPTION] LIKE '%Route%' THEN 'ROUTED' WHEN [DESCRIPTION] LIKE '%acted upon the document with the command%' AND
[DESCRIPTION] LIKE '%Approve.%' THEN 'APPROVED' WHEN [DESCRIPTION] LIKE '%on behalf of%' AND [DESCRIPTION] LIKE '%clicked the action button %' AND
[DESCRIPTION] LIKE '%APPROVE%' THEN 'APPROVED' WHEN [DESCRIPTION] LIKE '%acted upon the document with the command%' AND
[DESCRIPTION] LIKE '%Restart Workflow.%' THEN 'RESTARTED' WHEN [DESCRIPTION] LIKE '%on behalf of%' AND [DESCRIPTION] LIKE '%clicked the action button %' AND
[DESCRIPTION] LIKE '%Restart%' THEN 'RESTARTED' WHEN [DESCRIPTION] LIKE 'Invoice Approval ended.%' THEN 'ENDED' ELSE '' END AS TYPE,
CASE WHEN [DESCRIPTION] LIKE '%Invoice Approval started%' THEN 'SYSTEM' WHEN [DESCRIPTION] LIKE '%Invoice Approval ended%' THEN 'SYSTEM' WHEN [DESCRIPTION]
LIKE '%(symetra\%' THEN SUBSTRING(Description, (CHARINDEX('symetra\', Description)) + 8, 6) ELSE '' END AS [USER],
CASE WHEN [DESCRIPTION] LIKE '%on behalf of%' THEN 'YES' ELSE '' END AS IMPERSONATED,
CASE WHEN [DESCRIPTION] LIKE '%on behalf of%' THEN SUBSTRING(Description, (CHARINDEX('symetra\', Description, CHARINDEX('on behalf of', Description))) + 8, 6)
ELSE '' END AS IMPERSONATED_USERID
FROM Data_HistoryEvents
我相信这本可以做得更优雅,但这很有效,所以我们就这么做了。
【讨论】:
以上是关于创建一个使用描述列中的可变子字符串数据动态填充临时列的视图的主要内容,如果未能解决你的问题,请参考以下文章