I have this scenario:
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);
The data must be grouped by id and row and what I would need would be another column with data like this:
-If we have different questions on the same row (grouped by id = 12 and row = 1):
(question1: (5 To 10) [AND] question2: (washing machine) [AND] question3: (yes))
-If we have different questions on the same row and one of them has many answers it should look like this (id = 12 and row = 2):
(question2: (tv) [AND] question1: (11 To 15, 16 To 20) [AND] question4: (employed))
I managed to create the first case, but I’m having problems with the second. For the second I created something like
(question2: (tv) [AND] question1: (11 To 15) OR question1:(16 To 20) OR question4:(employed))
but it's not good, the answers for question1 have to be separated by comma and the name shouldn't be displayed everytime. Moreover, it puts [AND] only between the first two names, it should be between question1 [AND] question4 as well, I just don't know how to replace that OR...
I’ve created a function like this :
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;
I hope I managed to make clear what I want to accomplish. Every suggestion will be highly appreciated. Thanks !
Give this a shot
Example
;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]
Returns
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))