【问题标题】:Splitting pairs from Column Data in SQL Server从 SQL Server 中的列数据中拆分对
【发布时间】:2015-05-17 13:24:17
【问题描述】:

我有一个简单定义的问题的 Java 解决方案,但我想缩短执行数据处理所需的时间。问题是在关系数据库的列中获取一系列单词,并将这些单词分成对,然后将它们插入到成对的字典中。这些对本身与由 partid 标识的产品相关。

因此零件表有

PartID (int), PartDesc (nvarchar) 

字典有

DictID (int), WordPair (nvarchar).

因此逻辑是:

insert into DictPair (wordpair, partid)
select wordpairs, partid from Part

一个单词对被定义为两个相邻的单词,因此单词会被重复,例如

red car with 4 wheel drive 

将与

配对
{red, car},{car, with}, {with,4}, {4, wheel}, {wheel, drive}

因此,partid 45 的最终字典将具有 (partid, dictionarypair):

45, red car
45, car with
45, with 4
45, 4 wheel
45, wheel drive

这用于产品分类,因此词序很重要(但成对顺序无关紧要)。

有没有人知道如何解决这个问题?我在考虑存储过程,并使用某种解析。出于效率原因,我希望整个解决方案都在 SQL 中实现。

【问题讨论】:

    标签: sql sql-server split


    【解决方案1】:

    基本上,在网上找到一个split()函数,它返回一个单词在字符串中的位置。

    然后做:

    select s.word, lead(s.word) over (partition by p.partId order by s.pos) as nextword
    from parts p outer apply
         dbo.split(p.partDesc, ' ') as s(word, pos);
    

    这会将NULL 放在最后一对,您似乎并不想要。所以:

    insert into DictPair (wordpair, partid)
        select word + ' ' nextword, partid, 
        from (select p.*, s.word, lead(s.word) over (partition by p.partId order by s.pos) as nextword
              from parts p outer apply
                   dbo.split(p.partDesc, ' ') as s(word, pos)
             )
        where nextword is not null;
    

    Here 是一些拆分函数,由谷歌搜索“SQL Server 拆分”提供。和another。来自StackOverflow。还有更多

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多