all groups > sql server programming > august 2006 >
You're in the

sql server programming

group:

Get unique poolID value for interlinked items


Get unique poolID value for interlinked items Veeraraje Urs
8/18/2006 6:45:51 PM
sql server programming:
Hi All

I have a table with the data in the following format
Col1 Col2 PoolID
BOOK1 BOOK2 0
BOOK3 BOOK1 0
BOOK3 BOOK4 0
BOOK5 BOOK6 0


As per the above data our business logic says that
Book1 is linked with Book2
and Book3 is linked with Book1
and Book3 is linked with Book4

Since Book1 is linked with Book2 and
Book1 is linked with Book3
as per our business logic, Book2 is also linked with Book3

So going by the above rule, Book1, Book2, Book3 and Book4 are linked each
other.
Our requirement is that we need to identify these interlinked items and
update the poolid

So the net result will be Book1, Book2, Book3 and Book4 will have a value of
1
and Book5 and Book6 will have a value of 2

Script for creating the table :

CREATE TABLE TABLE_A
(Col1 varchar(255),
Col2 varchar(255),
PoolID int)
GO
INSERT INTO TABLE_A
SELECT 'BOOK1', 'BOOK2',0
GO
INSERT INTO TABLE_A
SELECT 'BOOK3', 'BOOK1',0
GO
INSERT INTO TABLE_A
SELECT 'BOOK3', 'BOOK4',0
O
INSERT INTO TABLE_A
SELECT 'BOOK5', 'BOOK6',0
GO


Select * from Table_A
--Drop table Table_A

Kindly Help

Thanks
Veeraraje Urs


Re: Get unique poolID value for interlinked items Hugo Kornelis
8/19/2006 1:10:36 AM
[quoted text, click to view]

Hi Veeraraje Urs,

Do the values have to be 1 and 2, or do they just have to be two
different values? In the latter case, try this:

WITH X (Col1, Col2, PoolID, rn)
AS (SELECT Col1, Col2, PoolID,
ROW_NUMBER() OVER (ORDER BY Col1, Col2) AS rn
FROM TABLE_A)
UPDATE X
SET PoolID = X.rn;

WHILE (@@ROWCOUNT > 0)
BEGIN;
UPDATE TABLE_A
SET PoolID = (SELECT MIN(x.PoolID)
FROM TABLE_A AS x
WHERE x.Col1 IN (TABLE_A.Col1, TABLE_A.Col2)
OR x.Col2 IN (TABLE_A.Col1, TABLE_A.Col2))
WHERE PoolID <> (SELECT MIN(x.PoolID)
FROM TABLE_A AS x
WHERE x.Col1 IN (TABLE_A.Col1, TABLE_A.Col2)
OR x.Col2 IN (TABLE_A.Col1, TABLE_A.Col2));
END;

The first part (which uses a SQL Server 2005 function - let me know if
you need a SQL Server 2000 equivalent) assigns a unique PoolID to each
row in the table. The next part (which should run on all versions of SQL
Server) replaces each PoolID with the lowest PoolID from any interlinked
item, and repeats this in a loop until no more changes are made.

--
Re: Get unique poolID value for interlinked items amish
8/19/2006 2:42:59 AM

[quoted text, click to view]

If you are using SQL Server 2005 then you can try this query



WITH temp (Col1, Col2, id)
AS (SELECT Col1, Col2,
ROW_NUMBER() OVER (ORDER BY Col1, Col2) AS num
FROM TABLE_A) ,

t as
(select col1 , col2,id,1 as poolid from temp where id= 1
union all
select temp.col1 , temp.col2,temp.id, case when
(temp.col1 in (t.col1, t.col2) or
temp.col2 in (t.col1, t.col2) ) then poolid
else poolid + 1 end
from temp , t
where temp.id= t.id+1
)

update table_a set poolid = t.poolid
from t
where t.col1 = table_a.col1 and
t.col2 = table_a.col2
go
select * from table_a


First it assigns row_number and then it use common table expression ,
both are sql server 2005 features.

Regards
Amish Shah
http://shahamishm.tripod.com
AddThis Social Bookmark Button