【问题标题】:Concat variable #s of strings连接变量 #s 的字符串
【发布时间】:2017-05-31 00:26:50
【问题描述】:

我有一个如下所示的表格:

Names                         uniqueid
crosby,stills,nash            1
crosby,stills,nash,young      2
crosby                        3
stills                        4
nash                          5
young                         6

我把它分成:

Names                         uniqueid    name
crosby,stills,nash            1           crosby
crosby,stills,nash            1           stills
crosby,stills,nash            1           nash
crosby,stills,nash,young      2           crosby
crosby,stills,nash,young      2           stills
crosby,stills,nash,young      2           nash
crosby,stills,nash,young      2           young
crosby                        3           crosby
stills                        4           stills
nash                          5           nash
young                         6           young

我想要什么:

Namecombos                    count
crosby                        3
stills                        3
nash                          3
young                         2
crosby,stills                 2
stills,nash                   2
nash,young                    1
crosby,stills,nash            2
stills,nash,young             1
crosby,stills,nash,young      1

我要做的是找到名称的唯一组合。 Crosby,Stills,Nash,Young 有 10 种独特的组合: 克罗斯比 剧照 纳什 年轻的 克罗斯比,剧照 剧照,纳什 纳什,杨 克罗斯比,剧照,纳什 剧照,纳什,年轻, 克罗斯比,剧照,纳什,杨

顺序很重要。

只需要解析成唯一的组合,然后计算有多少结果。

希望这是有道理的。

【问题讨论】:

  • 对于每个唯一的单个名称实例是否总是有一行,就像您的示例数据中的情况一样,或者有可能,例如,Nash 只在一个元组中,并且从不单独存在?
  • 第二个是正确的。我想我正在使用自定义功能,如果我能使其工作,我会发布解决方案。
  • 很容易得到所有可能的组合,但不是唯一的有序组合。即克罗斯比,剧照,纳什 |克罗斯比纳什剧照| 高分辨率照片| CLIPARTO剧照,纳什,克罗斯比等所有组合。将这些等同于 1 是困难的部分。递归 cte 的一些诡计和我想象的东西

标签: sql-server ssms


【解决方案1】:

哇,这让我的大脑崩溃(并绞尽脑汁)。我不是专业人士,所以如果有人想美化我的所作所为,请这样做,我会接受你的回答!

但它完成了最难的部分,即创建独特的组合。数数应该是容易的部分。

注意:我从原始逗号分隔值开始,而不是中间表。

CREATE FUNCTION [dbo].[func_Split] 
    (   
    @OrigStr    varchar(8000),
    @Delimiter              varchar(100) 
    )
RETURNS @tblArray TABLE
    (
    ElementID   int IDENTITY(1,1),  -- Array index
    Element     varchar(1000)               -- Array element contents
    )
AS
BEGIN

    -- Local Variable Declarations
    -- ---------------------------
    DECLARE         @Index1      smallint,
                    @Index2      smallint,
                    @incr           smallint,
                    @OuterLoop  smallint,
                    @n          smallint,
                    @LastDelim  smallint

    SET @n = LEN(@OrigStr) - LEN(REPLACE(@OrigStr, ',', ''))
    SET @OuterLoop = 1
    SET @LastDelim = LEN(@OrigStr) + 1 - CHARINDEX(@Delimiter,REVERSE(@OrigStr))

    WHILE @OuterLoop <= @n+1
    BEGIN
        SET @Index1 = 1
        SET @Index2 = @Index1

        WHILE @Index2 < LEN(@OrigStr)
        BEGIN
            SET @Index2 = @Index1

            SET @incr = 1
            WHILE @incr <= @OuterLoop
            BEGIN
                SET @Index2 = CHARINDEX(@Delimiter,@OrigStr,@Index2+1)
                IF @Index2 = 0 BEGIN SET @Index2 = LEN(@OrigStr) END
                SET @incr = @incr + 1
            END

                    IF @Index2 = LEN(@OrigStr) 
                    BEGIN 
                        INSERT INTO
                            @tblArray 
                            (Element)
                        VALUES
                            (LTRIM(RTRIM(SUBSTRING(@OrigStr,@Index1,@Index2-@Index1+1))))
                    END
                    ELSE
                    BEGIN
                        INSERT INTO
                            @tblArray 
                            (Element)
                        VALUES
                            (LTRIM(RTRIM(SUBSTRING(@OrigStr,@Index1,@Index2-@Index1))))
                    END


            SET @Index1 = CHARINDEX(@Delimiter,@OrigStr,@Index1+1)+1

        END

        SET @OuterLoop = @OuterLoop + 1



    END

    RETURN
END

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2016-11-15
    • 2018-06-23
    • 1970-01-01
    • 2021-12-12
    相关资源
    最近更新 更多