【问题标题】:Create varchar column with mixed values from other columns in SQL使用 SQL 中其他列的混合值创建 varchar 列
【发布时间】:2018-11-22 02:45:36
【问题描述】:

我有这种情况:

CREATE TABLE tbl(templateId INT, id INT, name NVARCHAR(50), value NVARCHAR(50), row INT);

INSERT INTO tbl(templateId, id, name, value, row) 
VALUES
(1, 12, 'question1', '5 To 10', 1),
(2, 12, 'question2', 'washing machine', 1),
(3, 12, 'question3', 'yes', 1),
(4, 12, 'question2', 'tv', 2),
(5, 12, 'question1', '11 To 15', 2),
(6, 12, 'question1', '16 To 20', 2),
(7, 12, 'question4', 'employed' 2);

数据必须按id和row分组 我需要的是另一列包含这样的数据:

-如果我们在同一行有不同的问题(按 id = 12 和 row = 1 分组):

(question1: (5 To 10) [AND] question2: (washing machine) [AND] question3: (yes))

-如果我们在同一行有不同的问题,其中一个有很多答案,它应该看起来像这样(id = 12 和 row = 2):

(question2: (tv) [AND] question1: (11 To 15, 16 To 20) [AND] question4: (employed))

我设法创建了第一个案例,但我遇到了第二个问题。第二次我创建了类似

(question2: (tv) [AND] question1: (11 To 15) OR question1:(16 To 20) OR question4:(employed)) 

但这不好,question1 的答案必须用逗号分隔,并且名称不应每次都显示。此外,它只在前两个名称之间放置 [AND],它应该在 question1 [AND] question4 之间,我只是不知道如何替换那个 OR...

我创建了一个这样的函数:

declare @result varchar(1000), @name1 varchar(250), @name2 varchar(250), 
@duplicates int;
set @result = '';
set @duplicates = 0;
set @name1 = '';
set @name2 = '';

SELECT @result = @result + ' [AND] ' +  t.name + ': (' + t.value + ')',
@duplicates = (len(@result) - len(replace(@result,t.name,''))) / 
LEN(t.name)
FROM tbl t
WHERE t.id = @table_id and t.row = @row

if(len(@result)>0)
  if (@duplicates > 1) 
    begin
         SET @result =replace(substring(@result, 7, LEN(@result) - 4), ' 
 [AND] ', ' OR ');
         SET @name1 = LEFT(@result,CHARINDEX(': ',@result)-1);
         SET @name2 = SUBSTRING(SUBSTRING(@result,CHARINDEX('OR ', @result) 
 + 2,LEN(@result)), 0,CHARINDEX(':', @result) + 0)

         if (@name1 <> @name2)
         begin
            SET @result=STUFF(@result, CHARINDEX('OR', @result), LEN('OR'), 
 '[AND]')
         end 
    end 
else
    begin
         SET @result=substring(@result, 7, LEN(@result) - 4);  
    end

return @result;  

我希望我能够明确我想要完成的任务。每一个建议都将受到高度赞赏。谢谢!

【问题讨论】:

  • 样本数据(不是图像)和期望的结果会更有帮助。
  • John Cappelletti 我更新了问题,希望现在更好:)
  • 好多了。但是,我要在 1 分钟内出去吃晚饭。当我回来时,我会看看你是否有答案。
  • 好的,太好了!非常感谢您的关注,我花了好几天的时间,但我被困住了.. :( 我很确定由于第二种情况需要重写该函数..

标签: sql-server tsql replace substring varchar


【解决方案1】:

试一试

示例

;with cte as (
Select top 1 with ties
       [id]
      ,[row]
      ,[name]
      ,TempValue =  Stuff((Select ', ' + value From tbl Where [Row]=A.[Row] and [Name]=A.[Name] For XML Path ('')),1,2,'')
      ,RN        =  Row_Number() over (Partition By [id],[row] Order by templateId)
 From tbl A
 Order by Row_Number() over (Partition By ID,[name],row order by templateid)
)
Select [Row]
      ,NewValue = '('+Stuff((Select ' [AND] ' +concat(Name,': (',TempValue,')') From cte Where [Row]=A.[Row] Order by RN For XML Path ('')),1,7,'')+')'
 From cte A
 Group By [Row]

退货

Row NewValue
1   (question1: (5 To 10) [AND] question2: (washing machine) [AND] question3: (yes))
2   (question2: (tv) [AND] question1: (11 To 15, 16 To 20) [AND] question4: (employed))

【讨论】:

  • 它有效,非常感谢。但是对于我在工作中的情况,我需要在不使用 (Order by templateId) 的情况下找出解决方案,因为它破坏了我的模板,我在存储过程中使用了这个函数,并给出了输入 id 和行。我会将您的答案标记为正确,因为它在我的问题中描述的场景中实现了我想要的。非常感谢!
猜你喜欢
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 2023-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-24
  • 1970-01-01
相关资源
最近更新 更多