Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to Convert nx1 dimension table to 1x1 dimension table Expand / Collapse
Author
Message
Posted Tuesday, January 13, 2009 7:42 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, July 10, 2013 10:13 PM
Points: 83, 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,
Post #635414
Posted Tuesday, January 13, 2009 7:50 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 21, 2013 12:32 AM
Points: 2,551, Visits: 2,593
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

Post #635423
Posted Tuesday, January 13, 2009 8:37 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, July 10, 2013 10:13 PM
Points: 83, 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
Post #635482
Posted Thursday, January 15, 2009 12:27 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, July 10, 2013 10:13 PM
Points: 83, 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
Post #636872
Posted Thursday, January 15, 2009 4:55 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, June 22, 2009 11:04 AM
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
Post #636987
Posted Thursday, January 15, 2009 9:56 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, July 10, 2013 10:13 PM
Points: 83, Visits: 253
it is great, Thank you Liliana.
Post #637737
Posted Wednesday, January 21, 2009 8:42 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, July 10, 2013 10:13 PM
Points: 83, 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

Post #640815
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse