有2个表,A(列a)和B(列b)。我想找到表B列b中表A列a中每个值的出现次数

时间:2016-03-08 00:30:36

标签: sql-server

有2个表格,A(列' a')和B(列' b')。我想在表A列中找到每个值的出现次数' a'在表B栏中' b'。

目前,我正在使用蛮力方法,而且速度太慢了。有什么可能是解决这个问题的更好方法?

示例:

enter image description here

输出

enter image description here

蛮力方法:

WHILE(SELECT COUNT(*) FROM A) > 0
BEGIN
    DECLARE @Val VARCHAR(100);
    DECLARE @ValSpaceMod VARCHAR(100);
    DECLARE @ModSpaceValSpaceMod VARCHAR(100);
    DECLARE @ModSpaceVal VARCHAR(100);
    DECLARE @cnt integer;

    SELECT TOP 1 @Val = a FROM A

    SET @ValSpaceMod = @Val + ' %';
    SET @ModSpaceVal = '% ' + @Val;
    SET @ModSpaceValSpaceMod = '% ' + @Val + ' %';

    SELECT @cnt = COUNT(*) FROM B 
    WHERE ( LOWER(B.b) LIKE lower(@Val) 
       OR   LOWER(B.b) LIKE lower(@ValSpaceMod)
       OR   LOWER(B.b) LIKE LOWER(@ModSpaceValSpaceMod)
       OR   LOWER(B.b) LIKE LOWER(@ModSpaceVal)
          )

    PRINT @Val + ' ' + CAST(@cnt AS VARCHAR)

    DELETE A WHERE a= @Val
END

2 个答案:

答案 0 :(得分:1)

这是一种可能的解决方案。结果并不完全等于您的预期输出,但这是因为我认为表B上的第四行应该增加A计数。如果这不对,请告诉我,以便我可以调整脚本。

if object_id ('tempdb..#tbT1') is not null
drop table #tbT1
GO

if object_id ('tempdb..#tbT2') is not null
drop table #tbT2
GO

create table #tbT1 (
    v char(1) collate SQL_Latin1_General_CP1_CI_AS
)

create table #tbT2 (
    t varchar(255) collate SQL_Latin1_General_CP1_CI_AS
)

insert into #tbT1
values ('A'), ('B'), ('C'), ('D')

insert into #tbT2
values ('A first value B second value'),
    ('B second value D fourth value'),
    ('D fourth value'),
    ('C third value Afirst')


select t1.v, sum (
    len(t2.t) - len(replace(t2.t, ' ' + t1.v + ' ', '  ')) -- middle occurrences
  + case when t2.t like t1.v + ' %' then 1 else 0 end -- add starting occurence
  + case when t2.t like '% ' + t1.v then 1 else 0 end -- add ending occurence
  ) as nOccurencies
from #tbT1 t1, #tbT2 t2
group by t1.v

答案 1 :(得分:1)

请尝试以下查询(使用关键字like获取表A列中的每个值的出现次数' a'在表B列中' b' )

SELECT  [A].a AS SearchTerm
        ,(  SELECT  count(*)
            FROM    [B]
            WHERE   b LIKE '%'+[A].a+' %'
            COLLATE Latin1_General_CS_AS )  AS  Occurrences
FROM    [A]