How to Convert nx1 dimension table to 1x1 dimension table

  • Dear Members,

    How to Convert nx1 dimension table to 1x1 dimension table

    here n - rows and 1 - columns.

    -------

    For example,

    DECLARE @tblVariable table(Roles varchar(2000))

    insert into @tblVariable VALUES('Role1,Role2,')

    insert into @tblVariable VALUES('Role1,Role3')

    insert into @tblVariable VALUES('Role1,Role4')

    insert into @tblVariable VALUES('Role2,Role5)

    insert into @tblVariable VALUES('Role6,Role1)

    insert into @tblVariable VALUES('Role7')

    insert into @tblVariable VALUES('Role8)

    SELECT * FROM @tblVariable

    --Out Put of the select query

    Role1,Role2,

    Role1,Role3,

    Role1,Role4,

    Role2,Role5,

    Role6,Role1,

    Role7,

    Role8,

    --My final requierment is to get all distinct roles with comma separation.

    --

    -- Role1, Role2, Role3, Role4, Role5, Role6, Role7, Role8

    actually the Roles in the table will be in Roles Tag (here roles table is just like html tags)

    Please Help me in this scenario.

    Thank you,

  • There are many articles on this forum about concatenating strings.

    Here is an excellent article by Jeff Moden on it

    http://www.sqlservercentral.com/articles/Test+Data/61572/%5B/url%5D%5B/u%5D

    --Ramesh


  • yes you are rigth,

    refernce to the link

    -----------------

    http://www.sqlservercentral.com/articles/Test+Data/61572/

    Using function we can do,

    CREATE FUNCTION dbo.fnConcatTest (@SomeID INT)RETURNS VARCHAR(8000) AS BEGIN DECLARE @Return VARCHAR(8000)

    SELECT @Return = ISNULL(@Return+',','')+SomeCode FROM dbo.TestData WHERE SomeID = @SomeID ORDER BY RowNum

    RETURN @Return

    END

    but my problem i want the query to get the result as "Role1, Role2, Role3..."

    so i can subquery and filter all the records with these roles.

    here i my scenario i am not using the stored procedure and function.

    is it possible to achieve my requiremt without using function.

    -------

    Santosh

  • Hi All,

    Thanks a lot for your help,

    I have done this using Cursors,

    Here is the complete code,

    DECLARE @tblRolesWithComma TABLE(Roles VARCHAR(8000))

    insert into @tblRolesWithComma VALUES('Role1,Role2,')

    insert into @tblRolesWithComma VALUES('Role1,Role3')

    insert into @tblRolesWithComma VALUES('Role1,Role4')

    insert into @tblRolesWithComma VALUES('Role2,Role5')

    insert into @tblRolesWithComma VALUES('Role6,Role1')

    insert into @tblRolesWithComma VALUES('Role7')

    insert into @tblRolesWithComma VALUES('Role8')

    SELECT * FROM @tblRolesWithComma

    DECLARE @tblRolesInSingleColumn TABLE(RoleId VARCHAR(8000))

    DECLARE cur_Roles CURSOR

    READ_ONLY

    FOR (SELECT * FROM @tblRolesWithComma)

    DECLARE @RoleIds VARCHAR(8000)

    OPEN cur_Roles

    declare @pos int

    declare @RoleId VARCHAR(500)

    FETCH NEXT FROM cur_Roles INTO @RoleIds

    WHILE (@@fetch_status <> -1)

    BEGIN

    IF (@@fetch_status <> -2)

    BEGIN

    IF RIGHT(RTRIM(@RoleIds),1) <> ','

    SET @RoleIds = @RoleIds + ','

    SET @pos = PATINDEX('%,%' , @RoleIds)

    WHILE @pos <> 0

    BEGIN

    SET @RoleId = left(@RoleIds, @pos - 1)

    INSERT INTO @tblRolesInSingleColumn VALUES(CAST(@RoleId as varchar(500)))

    SET @RoleIds = STUFF(@RoleIds, 1, @pos, '')

    SET @pos = PATINDEX('%,%' , @RoleIds)

    END

    END

    FETCH NEXT FROM cur_Roles INTO @RoleIds

    END

    CLOSE cur_Roles

    DEALLOCATE cur_Roles

    --SELECT DISTINCT * FROM @tblRolesInSingleColumn

    DECLARE @Result varchar(8000)

    SET @Result = ''

    SELECT @Result = @Result + CASE WHEN LEN(@Result)>0 THEN ', ' ELSE '' END + RoleId

    FROM (SELECT DISTINCT RoleId FROM @tblRolesInSingleColumn)Roles

    SELECT @Result RolesIds

    SET NOCOUNT OFF

    GO

    Thank you very much dudes for you help

    -----

    Santosh

  • Hi!

    Try this, without cursors.

    Regards,

    Liliana.

    SELECT TOP 100 n = IDENTITY(INT, 1, 1) INTO #Numbers

    FROM

    sysobjects a1

    CROSS JOIN

    sysobjects a2

    CROSS JOIN

    sysobjects a3

    DECLARE @tblVariable table (Roles varchar(2000))

    insert into @tblVariable VALUES('Role1,Role2,')

    insert into @tblVariable VALUES('Role1,Role3,')

    insert into @tblVariable VALUES('Role1,Role4,')

    insert into @tblVariable VALUES('Role2,Role5,')

    insert into @tblVariable VALUES('Role6,Role1,')

    insert into @tblVariable VALUES('Role7,')

    insert into @tblVariable VALUES('Role8,')

    DECLARE @tblRolesInSingleColumn TABLE(RoleId VARCHAR(8000))

    INSERT @tblRolesInSingleColumn

    SELECT DISTINCT

    SUBSTRING (t.Roles + ',', N.n, CHARINDEX (',', t.Roles + ',', N.n) - N.n) as RoleId

    FROM @tblVariable t

    CROSS JOIN #Numbers N

    WHERE SUBSTRING (t.Roles, N.n - 1, 1) = ',' OR N.n = 1

    ORDER BY 1

    DECLARE @Result varchar(8000)

    SET @Result = ''

    SELECT @Result = @Result + CASE WHEN LEN(@Result)>0 THEN ', ' ELSE '' END + RoleId

    FROM (SELECT DISTINCT RoleId FROM @tblRolesInSingleColumn)Roles

    SELECT @Result RolesIds

    GO

  • it is great, Thank you Liliana.

  • Hi All,

    In the above code, i have noticed a problem that in the #Numbers table,

    It is not a problem, just suppose if the query results more than the no.of the rows in the identity column, then i will not give.

    In this scenario, we have inserted 100 numbers in the #numbers, so it will return up to 100.

    ...

    one person suggested the other logic... in the same fourm

    CREATE TABLE #t2(ID INT IDENTITY, Column1 NVARCHAR(100))

    CREATE TABLE #t3(Column1 NVARCHAR(100))

    INSERT INTO #t2

    SELECT 'Value1, Value2'

    UNION ALL SELECT 'Value1, Value3'

    UNION ALL SELECT 'Value1, Value2'

    UNION ALL SELECT 'Value4, Value5'

    UNION ALL SELECT 'Value1, Value3'

    UNION ALL SELECT 'Value1, Value2'

    UNION ALL SELECT 'Value4, Value5'

    SELECT * FROM #t2

    UPDATE #t2 SET Column1 = replace(Column1,' ','')+','

    SELECT * FROM #t2

    DECLARE @Values NVARCHAR(100), @ID INT

    SELECT @ID = MIN(ID) FROM #t2

    WHILE @ID is NOT NULL

    BEGIN

    SELECT @Values = SUBSTRING(Column1, 1,PATINDEX('%,%',Column1)) FROM #t2 WHERE ID = @ID

    WHILE ISNULL(@Values,'') <> ''

    BEGIN

    INSERT INTO #t3

    SELECT @Values

    UPDATE #t2 SET Column1 = STUFF(Column1, 1, LEN(@Values), '') WHERE ID = @ID

    SELECT @Values = SUBSTRING(Column1, 1,PATINDEX('%,%',Column1)) FROM #t2 WHERE ID = @ID

    END

    SELECT @ID = MIN(ID) FROM #t2 WHERE ID > @ID

    END

    UPDATE #t3 SET Column1 = replace(Column1,',','')

    SELECT DISTINCT *

    FROM #t3

    DROP TABLE #t2

    DROP TABLE #t3

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply