Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to Convert nx1 dimension table to 1x1 dimension table


How to Convert nx1 dimension table to 1x1 dimension table

Author
Message
santoooo
santoooo
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 253
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,
Ramesh Saive
Ramesh Saive
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2564 Visits: 2643
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/


--Ramesh


santoooo
santoooo
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 253
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
santoooo
santoooo
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 253
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
Liliana Sorrentino
Liliana Sorrentino
SSC-Enthusiastic
SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)SSC-Enthusiastic (108 reputation)

Group: General Forum Members
Points: 108 Visits: 29
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
santoooo
santoooo
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 253
it is great, Thank you Liliana.
santoooo
santoooo
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 253
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search