简体   繁体   English

带有文本搜索的案例陈述

[英]Case Statement with Text Search

I have a data-set (using SQL Server Management Studio) that is used for Sales Analysis. 我有一个用于销售分析的数据集(使用SQL Server Management Studio)。 For this example, when an agent fufills a Sales Call or Account Review, they list (via a drop-down) what topics they discussed in the call/review. 对于此示例,当业务代表完成销售电话或客户复核时,他们(通过下拉列表)列出了他们在电话/复核中讨论的主题。 Then there is a corresponding column of the products that client purchased after-the fact (in this example, I'm using automobiles). 然后,客户在事后购买了相应的产品列(在此示例中,我正在使用汽车)。 I'm thinking maybe a case statement is the way to do but in esscence I need to figure out if any of the makers the person suggested exists in the products column: 我在想也许要通过案例说明的方式来做,但是在本质上,我需要弄清楚该人建议的制造商是否存在于产品栏中:

在此处输入图片说明

So in this example, in line 1, they had suggested Mazda and a Toyota (seperate by ";") and Mazda appears in the products line so that would then be marked as effective. 因此,在此示例中,在第1行中,他们建议马自达和一辆丰田汽车(以“;”分隔),而马自达则出现在产品系列中,以便随后将其标记为有效。 Line 3, they suggested Honda but the person ended up getting a Jeep, so that not effective. 在第3行中,他们建议使用本田,但此人最终得到了一辆吉普车,因此效果不佳。 So on and so forth. 等等等等。

I'd like for it to be dynamic (maybe an EXISTS??) that way I don't have to write/maintain something like 'Effective'=CASE WHEN Topic like '%Mazada%' and Products like '%Mazada%', "Yes", "No" WHEN..... 我希望它是动态的(也许是EXISTS ??),这样,我就不必编写/维护“有效”之类的东西=案例时主题为“%Mazada%”而产品为“%Mazada%” ,“是”,“否”

Thoughts? 有什么想法吗?

If you have a Product table, then you might be able to get away with something like this: 如果您有一个Product表,那么您也许可以摆脱像这样的东西:

select RowId, Topic, Products,
       (case when exists (select 1
                          from Products p
                          where t.Topic like '%'+p.brand+'%' and
                                t.Products like '%'+p.brand+'%'
                         )
             then 'Yes' else 'No'
        end) as Effective
from t;

This is based on the fact that the "brand" seems to be mentioned in both the topic and products fields. 这是基于以下事实:在topicproducts领域中似乎都提到了“品牌”。 If you don't have such a table, you could do something like: 如果没有这样的表,则可以执行以下操作:

with products as (
      select 'Mercedes' as brand union all
      select 'Mazda' union all
      select 'Toyota' . . .
     )
select RowId, Topic, Products,
       (case when exists (select 1
                          from Products p
                          where t.Topic like '%'+p.brand+'%' and
                                t.Products like '%'+p.brand+'%'
                         )
             then 'Yes' else 'No'
        end) as Effective
from t;

However, this may not work, because in the real world, text is more complicated. 但是,这可能不起作用,因为在现实世界中,文本更加复杂。 It has misspellings, abbreviations, and synonyms. 它具有拼写错误,缩写和同义词。 There is no guarantee that there is even a matching word on both lists, and so on. 不能保证两个列表中甚至都存在匹配的单词,依此类推。 But, if your text is clean enough, this approach might be helpful. 但是,如果您的文本足够干净,则此方法可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM