Объединить столбец onumber на основе значения OGroup с использованием цикла while - PullRequest
0 голосов
/ 25 июня 2019

Проблема кода в том, что я не могу получить значения ONumber для объединения на основе того же значения OGroup

таблица: ztemp ONumber OGroup RowOnly_ID T146 10 3 T591 10 8 T607 10 12 T612 10 16 T713 1021 T704 15 20 T610 15 14 T611 20 15 T47 20 1 T608 25 13 T49 30 2 T722 35 22 T620 40 18 T588 45 7 T594 5 9 T595 5 10 T606 5 11 T618 5 17 T383 70 5 T314 75 4 T703 80 19 T57890 6

Testing – added print to display variable values

===== START Pre-Loop Values =====

 = Pre-Loop @CatSONum
5 = Pre-Loop @iCounter
0 = Pre-Loop @iSOGroupCount
90 = Pre-Loop @iMAXGroupCount

===== END Pre-Loop Values =====

 ___ START Loop Values ___
5 = @iCounter
4 = @iSOGroupCount
90 = @iMAXGroupCount
 ___ END Loop Values ___

 ___ START Loop Values ___
10 = @iCounter
5 = @iSOGroupCount
90 = @iMAXGroupCount
 ___ END Loop Values ___


Desired Output would be
OGroup  NumOGroup  ONumber
10      5        T146, T591, T607, T612, T713
15      2          T704, T610
20      2          T611, T47
25      1          T608
30
35

Проблема кода в том, что я не могу получить значения ONumber для объединения на основе того же значения OGroup

D

ECLARE @iCounter INT
DECLARE @iSOGroupCount INT
DECLARE @iMAXGroupCount INT
DECLARE @CatSONum NVARCHAR(5) = ''

SET @iCounter = (SELECT MIN(CAST(OGroup AS INT)) FROM [ztemp])
SET @iSOGroupCount = 0
SET @iMAXGroupCount = (SELECT MAX(CAST(OGroup AS INT)) FROM [ztemp])
SET @CatSONum = ''

PRINT '===== START Pre-Loop Values ====='
PRINT ''

PRINT  @CatSONum + ' = ' + 'Pre-Loop @CatSONum' 

--PRINT @iCounter
PRINT CAST(@iCounter AS NVARCHAR(5)) + ' = ' + 'Pre-Loop @iCounter' 

--PRINT @iSOGroupCount
PRINT CAST(@iSOGroupCount AS NVARCHAR(5)) + ' = ' + 'Pre-Loop @iSOGroupCount' 

--PRINT @iMAXGroupCount
PRINT CAST(@iMAXGroupCount AS NVARCHAR(5)) + ' = ' + 'Pre-Loop @iMAXGroupCount' 
PRINT ''
PRINT '===== END Pre-Loop Values ====='

WHILE @iCounter <= @iMAXGroupCount
BEGIN
    SET @iSOGroupCount = 
    (
     SELECT COUNT(*) 
     FROM [ztemp] 
     WHERE CAST(OGroup AS INT) = @iCounter 
    )


/*  ---not Concatenating properly

     SET @CatSONum = 
     (
     SELECT
     @CatSONum + ONumber + ','
     --SET @CatSONum = LEFT(@CatSONum,LEN(@CatSONum)-1)
     FROM [ztemp] 
     WHERE CAST(OGroup AS INT) = @iCounter 
     )
*/   

PRINT ''
PRINT ' ___ START Loop Values ___'

--PRINT + '---' + @CatSONum 

PRINT CAST(@iCounter AS NVARCHAR(5)) + ' = ' + '@iCounter' 

    SET @iCounter = @iCounter + 5

--PRINT @iSOGroupCount
PRINT CAST(@iSOGroupCount AS NVARCHAR(5)) + ' = ' + '@iSOGroupCount' 

--PRINT @iMAXGroupCount
PRINT CAST(@iMAXGroupCount AS NVARCHAR(5)) + ' = ' + '@iMAXGroupCount' 

PRINT ' ___ END Loop Values ___'

END
...