首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL Server中的文本/字符串分析

SQL Server中的文本/字符串分析
EN

Stack Overflow用户
提问于 2016-04-11 20:00:48
回答 1查看 1.8K关注 0票数 2

我试图分析SQL Server中的大量文本,根据两个不同表中的数字、重复单词来计算总分。我正在寻找一个查询来完成这个任务。

为了方便起见,我在下面举个例子。

TABLE 1

代码语言:javascript
复制
id | Message                   | 
--  ---------------------------
 1 | mike magic                | 
 2 | sky blue and dark         |
 3 | wars star                 | 
 4 | whistle mountain broke    |  

TABLE 2 (plus)

代码语言:javascript
复制
id | Words        | score
--  -------------- ------
 1 | mike         | +1
 2 | dark         | +1
 3 | wars         | +1

TABLE 3 (minus)

代码语言:javascript
复制
id | Words        | score
--  -------------- ------
 1 | whistle      | -1
 2 | mountain     | -1
 3 | magic        | -1

预期结果:

代码语言:javascript
复制
id | Message                   | plus| minus| sum |
--  --------------------------- ----- ------ -----
 1 | mike magic                |  +1 | -1   |  0  |
 2 | sky blue and dark         |  +1 |  0   | +1  |
 3 | wars star                 |  +1 |  0   | +1  | 
 4 | whistle mountain broke    |   0 | -2   | -2  |
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-11 20:20:28

您可以使用以下查询:

代码语言:javascript
复制
--create table table1 (id int,message varchar(100),[date] date ,[other info] varchar(100));
--insert into table1 values
--(1,'mike magic', '2016-01-01','some other information'),
--(2,'sky blue and dark', '2016-01-02','some other information'),
--(3,'wars star', '2016-10-01','some other information'),
--(4,'whistle mountain broke', '2016-02-01','some other information');
--create table table2 (id int,words varchar(100), score int);
--insert into table2 values
--(1,'mike','+1'),
--(2,'dark','+1'),
--(3,'wars','+1');

--create table table3 (id int,words varchar(100), score int);
--insert into table3 values
--(1,'whistle','-1'),
--(2,'mountain','-1'),
--(3,'magic','-1');

select 
    t1.id, t1.message, t1.date,t1.[other info], 
    isnull(sum(cast(t2.score as int)),0) plus, 
    isnull(sum(cast(t3.score as int)),0) minus,
    isnull(sum(cast(t2.score as int)),0) + isnull(sum(cast(t3.score as int)),0) [sum]

from 
    table1 t1 
        left join table2 t2 
                on ' '+ t1.message+' ' like '% '+t2.Words+' %'
        left join table3 t3 
                on ' '+ t1.message+' ' like '% '+t3.Words+' %'
group by t1.id, t1.message, t1.date,t1.[other info]
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36558164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档