# Cross Table , Interchange information

I've been cracking my head for some time on this now,
I need to record interchange infomration:
the records come in like this:
Record Type1: Item 'Q' = Item 'W'  ::Item Q is the same as item W
Record Type2: Item 'A' -> Item 'B' ::Item A Replaced by Item B

Now my guest guess was to seperate this into 2 tables
Where Type 1:
Item | Group
'1'   |  'A'
'2'   |  'B'

'3'   |  'C'
'4'   |  'C'
'5'   |  'C'

If i want to see #4 then trhough group i get "3,4,5" all '='

Type 2:
Item | Into Item
'1' | '2'
'1' | '3'
'4' | '5'
If i want to see #1 then though i get "2,3" all  replace '1'

Now here comes the problem
1.With close to a million records it takes a really of a long time to process,
1.1 I Create an array in keep looping throught table 1 and 2 untill no new records are found
Here is now it works Based on the above tables

Query:#1
:  1
:2 | 3,4
:6 | 5

*** I also Identify loops with this procedure, if i have records like "5 ->1"
LVL 1
###### Who is Participating?

x

Commented:
Ok, that's all,

you have T1 { item, group } and T2 { fromitem, toitem }

you need two final relations, item equal item, and item replaced with item.

for first relation I suggest generate other final table Q1 { itemK, item } when itemK is the "key item" for all equality item group. (if 1=2 and 2=3 then 1=3)

the method for populate a minimal Q1 table are

CREATE TABLE Q1 ( itemK <type>, item <type> )

INSERT INTO Q1 (
itemK,
item
)
SELECT    g.itemK,
q.item
FROM    (
SELECT    group, itemK = MIN( item )
FROM    T1
GROUP
BY    group
) g
JOIN    Q1 q
ON    g.group    = q.group

result example

itemK    item
------- -------
1    1
2    2
3    3
3    4
3    5

not iterate is needed (only replace group for itemK).

For replacement relation (like 1 -> 4) create other final table Q2 { fromitemK, toitemK )

CREATE TABLE Q2( fromitemK <type>, toitemK <item> )

-- first population
INSERT INTO Q2 (
fromitemK,
toitemK
)
SELECT    DISTINCT
fromitemK    = fk.itemK,
toitemK        = tk.itemK
FROM    T2 t
JOIN    Q1 fk
ON    t.fromitem    = fk.item -- only one match
JOIN    Q2 tk
ON    t.toitem    = tk.item -- only one match

-- regenerate relations of relations
WHILE @@ROWCOUNT > 0
BEGIN
INSERT INTO Q2 (
fromitemK,
toitemK
)
SELECT    DISTINCT
fromitemK    = fk.itemK,
toitemK        = tk.itemK
FROM    Q2 t1
JOIN    Q2 t2
ON    t1.toitemK    = t2.fromitemK
JOIN    Q1 fk
ON    t1.fromitem    = fk.item -- only one match
JOIN    Q2 tk
ON    t2.toitem    = tk.item -- only one match
WHERE    NOT EXISTS (
SELECT    *
FROM    Q2
WHERE    fromitemK    = fk.itemK
AND    toitemK        = tk.itemK
)
END

this is all you need!

* Query for a key item @K

-- all @K = item
SELECT    item
FROM    Q1
WHERE    itemK = ( SELECT itemK FROM Q1 WHERE item = @K )

-- all @K -> item
SELECT    toitem
FROM    Q1
WHERE    fromitemK = ( SELECT itemK FROM Q1 WHERE item = @K )

-- all item -> @K
SELECT    fromitem
FROM    Q1
WHERE    toitemK = ( SELECT itemK FROM Q1 WHERE item = @K )

Good luck!

0

Commented:
Hi,

I don't understan what is table 2.

0

Author Commented:
Tbale 2:
Title:       | FROM Item |   To Item   |
Record:  | Item "A" | Item "B" |

Item "B" can replace Item "A"
It is not the same as  A = B because
Item "A" canNot replace Item "B"
0

Commented:
Ok, CMDAI,

what query, do you like optimize?
0

Author Commented:
It looked like this:

|-|Item "A"  |-|  = |-|  Item "B"|-|
|-|Item "C"  |-| ->  |-| Item "E"|-|
|-|Item "C"  |-| ->  |-| Item "D"|-|
|-|Item "D"  |-| ->  |-| Item "W"|-|
|-|Item "A"  |-| =  |-| Item "C"|-|
0

Author Commented:
1:Item | Group
'1'   |  'A'
'2'   |  'B'
'3'   |  'C'
'4'   |  'C'
'5'   |  'C'

2:Item | Into Item
'1' | '2'
'1' | '3'
'4' | '5'

If i want to get all the items that replace #1
I start a loop in VB and collect and Array

1,(                  ) * Initial
2,(from Table 2) *Query  1
3,(from Table 2) * Query 1
4,(from Table 1) * Query 2
5,(from Table 1) * Query 2
5,(from Table 2)  * Query 3
0

Author Commented:
Sorry typo:
1,(                  ) * Initial
2,(from Table 2) *Query  1
3,(from Table 2) * Query 1
4,(from Table 1) * Query 2
5,(from Table 1) * Query 2
6,(from Table 2)  * Query 3
Then it stops as no new numbers canbe found
0

Author Commented:
If I covert everything into table 2 format this is what i am trying to do
but i cannot predict how long the cross is going to be.
Cross.jpg
0

Commented:
Ops!

"@K -> item" and  "item -> @K" querys are "FROM Q2" instead "FROM Q1" (obviously).

The running cost are linear over initial total row count.

Good luck!

0

Author Commented:
Sorry i'm trying to recreate but i got stuck on this part

""""""""""""""""""""""""""""""""""""""
JOIN    Q1 q
ON    g.group    = q.group
""""""""""""""""""""""""""""""""""""""""

From:
INSERT INTO Q1 (
itemK,
item
)
SELECT    g.itemK,
q.item
FROM    (
SELECT    group, itemK = MIN( item )
FROM    T1
GROUP
BY    group
) g
JOIN    Q1 q
ON    g.group    = q.group
0

Commented:
Yes, group field only is in T1, you must write

JOIN T1 q
ON g.group = q.group

Sorry!
0

Author Commented:
Sorry i wans't arround yestruday,
I tried to recreate but got confused , maybe because im doing it in access
and the language structure is different.

So Q1 is
[Key Item] || [Item]
Basically it looks like the [group] has been replaced by one of the items in the [group]

itemK    item
------- -------
1    1
2    2
3    3
3    4
3    5
Will this also be Inside?
4    4
4    3

THen Q2 I'm confused
0

Commented:
Oh! Access... yes.
I suggest to you use a better database engine(like mysql, ms sql server 2005 express, ...).

Nevertheless, the only important change are the WHILE iteration and you can make this using the WHILE VB sentence.

On the other hand, my result example are good, only one itemK is needed for "linked relations". Tuples (3,4), (4,3) are equals and only one must be in table.

The Q1 table are a simple translation of your Type1 table.
For all ( itemK, item ) tuples than itemK<>item, then item must not be in Q2 (not is needed, if itemK are in Q2 then I deduce item are also in Q2).

A complete example:

Type1 relations
----------------------
1 A
2 B
3 A
4 C
5 B
6 D
7 B

Type2 relations
----------------------
3 7
5 6
4 6

Then we generate Q1 table as a simple translation of id group:

Minimum key for each Id group
--------------------------------
1 A
2 B
4 C
6 D

Q1
-----
1 1
1 3
2 2
2 5
2 7
4 4
6 6

In Q2, only itemK!

Q2 (+first insert)
--------
1 2
2 6
4 6

Q2 (+second insert)
--------
1 2
2 6
4 6
1 6

Stop!

Consequences:

1 and 3 elements are equals and equivalents.
2, 5 and 7 elements are equals and equivalents.
4 are alone.
6 are alone.

1 or 3 element can be replaced with any of 2, 5 and 7.
2, 5 or 7 element can be replaced with 6.
4 element can be replaced with 6.
1 or 3 element can be replaced with 6.

If I have not a error...

Good luck!
0

Author Commented:
Thank's for your help, sorry for long responsiveness, had wasnt arround for a long time,
Works great now ! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.