查询返回字符串中特定值出现的次数?

时间:2014-01-19 00:14:35

标签: sql sql-server subquery

好吧我有两张桌子。表1具有参考编号列(A),以及具有随机性字符串(B)的第二列。 Table2只有一列,其中包含可能包含或不包含在Table1字符串中的值列表。

dbo.Tbl_1

+--+---------------------------------------+
|A |B                                      |
+--+---------------------------------------+
|24|BLUE; KITTEN; WHITE; PINK; SLOPE; GREEN|
+--+---------------------------------------+
|51|GREEN; CLOUDY; WHITE; CHIPS            |
+--+---------------------------------------+
|78|PATRIOTS; PINK; PINK; WHITE            |
+--+---------------------------------------+
|22|WHITE; RED; TREES; AMY; GREEN          |
+--+---------------------------------------+

dbo.Tbl_2

+-----+
|C    |
+-----+
|BLUE |
+-----+
|WHITE|
+-----+
|PINK |
+-----+
|BROWN|
+-----+

什么SQL查询将确定在Table1的字符串中找到Table2中的值的次数?基本上我想返回以下结果集:

+-----+----+
|BLUE |1   |
+-----+----+
|WHITE|4   |
+-----+----+
|PINK |3   |
+-----+----+
|BROWN|NULL|
+-----+----+

仅供参考:实际上,Table2有大约200条独特记录。 Table1有大约1.6M记录,带有唯一的参考号。这两个表都不是静态的。

2 个答案:

答案 0 :(得分:0)

我玩了一下,想出了这个SQL fiddle

相关的SELECT查询看起来像这样(虽然需要两次表扫描,但我相信它可以提高效率):

select C, sum(dbo.CountOccurancesOfString(B, C)) as number
from Tbl_1 join Tbl_2 on 1=1
group by C
order by number desc

编辑这是我从this answer获得的功能:

CREATE FUNCTION dbo.CountOccurancesOfString
(
 @searchString nvarchar(max),
 @searchTerm nvarchar(max)
)
RETURNS INT
AS
BEGIN
 return (LEN(@searchString)-LEN(REPLACE(@searchString,@searchTerm,'')))/LEN(@searchTerm)
END

答案 1 :(得分:0)

拆分功能

CREATE FUNCTION [dbo].[udf_Split]
(
    @RowData nvarchar(2000),
    @SplitOn nvarchar(5)
)  
RETURNS @RtnValue table 
(
    Id int identity(1,1),
    Data nvarchar(100)
) 
AS  
BEGIN 
    Declare @Cnt int
    Set @Cnt = 1

    While (Charindex(@SplitOn,@RowData)>0)
    Begin
        Insert Into @RtnValue (data)
        Select 
            Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))

        Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
        Set @Cnt = @Cnt + 1
    End

    Insert Into @RtnValue (data)
    Select Data = ltrim(rtrim(@RowData))

    Return
END

您的数据

DECLARE @Table_1 TABLE ([A] INT,[B] VARCHAR(1000)) 
INSERT INTO @Table_1 VALUES
(24,'BLUE; KITTEN; WHITE; PINK; SLOPE; GREEN'),
(51,'GREEN; CLOUDY; WHITE; CHIPS'), 
(78,'PATRIOTS; PINK; PINK; WHITE'), 
(22,'WHITE; RED; TREES; AMY; GREEN')

DECLARE @Table_2 TABLE (ColumnName VARCHAR(100))
INSERT INTO @Table_2 VALUES 
('BLUE'),('WHITE'),('PINK'),('BROWN')

<强>查询

SELECT T2.ColumnName, TotalNums
 FROM 
(SELECT Data, COUNT(DATA) TotalNums 
FROM  @Table_1 t  CROSS APPLY (SELECT * FROM [dbo].[udf_Split](t.B, ';'))C
GROUP BY Data) T1
RIGHT JOIN @Table_2 T2 
ON T1.Data = T2.ColumnName

结果集

╔════════════╦═══════════╗
║ ColumnName ║ TotalNums ║
╠════════════╬═══════════╣
║ BLUE       ║ 1         ║
║ WHITE      ║ 4         ║
║ PINK       ║ 3         ║
║ BROWN      ║ NULL      ║
╚════════════╩═══════════╝