DB table
The table has a TestResult column, which stores the results of the participants (in%) for some indicators:
DECLARE @TestResults TABLE(ParticipCode INT, Elements VARCHAR(100)) INSERT INTO @TestResults VALUES (1, '0,50;0,20;0,30') INSERT INTO @TestResults VALUES (2, '0,60;0,24;0,32') REQUIRED
It is necessary to obtain an average value for each indicator, ie:
And there is no difference in what form they will be presented in the result set -
in rows
or columns
How to get this? After it turns out to make a script, I want to make on its basis a stored procedure that would accept such an array and calculate the averages.
Attempts
1. I work in SQL Server 2016 and naturally I decided to resort to the STRING_SPLIT innovation. But unfortunately my knowledge and attempts were enough only for the ability to parse one line:
DECLARE @str VARCHAR(MAX) = (SELECT TOP 1 Elements FROM @TestResults); SELECT * FROM STRING_SPLIT(@str, ';') 2. Also on the forum I found a solution that is based on the built-in function PARSENAME . It does not fit, because This feature will stop working with more than 4 indicators. I have in example 3, but in reality there are more of them.




