Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 compare rows between 2 diffrent tables and update

Author  Topic 

kneekill
Yak Posting Veteran

76 Posts

Posted - 2009-09-04 : 13:19:22
[code]
Hi All,

I need your help in accomplishing below task. I have two tables

Table A

+=====+=====+=======+
| ID |Code |exCode |
+=====+=====+=======+
| | | |
| 1 | A | AAA |
+-----+-----+-------+
| 1 | B | B |
| | | |
+-----+-----+-------+
| 2 | A | AAA |
| | | |
+-----+-----+-------+
| 3 | A | AAA |
| | | |
+-----+-----+-------+

Table B

+=====+=====+=======+
| ID |Name | Code |
+=====+=====+=======+
| | | |
| 1 |Tom | A |
'-----+-----+-------+
| 1 |Bill | A |
| | | |
'-----+-----+-------+
| | | |
| 1 |Tom | B |
'-----+-----+-------+
| 1 |Bill | B |
| | | |
'-----+-----+-------+
| 2 |Chad | AAA |
| | | |
'-----+-----+-------+
| 3 | A | A |
| | | |
'-----+-----+-------+

1) For each ID in Table A, I need to check if "exCode" = "AAA" has an entry in Table B "Code" column for the "same ID"

example : exCode "AAA" Exits for ID = 2 in Table A and Code column of Table B


+=====+=====+=======+
| ID |Code |exCode |
+=====+=====+=======+
| 2 | A | AAA |
| | | |
+-----+-----+-------+

+=====+=====+=======+
| ID |Name | Code |
+=====+=====+=======+
| 2 |Chad | AAA |
| | | |
'-----+-----+-------+


2) If it does not exist then I need to insert a new row in Table B

example: exCode "AAA" Exits for ID = 1 in Table A and Code column of Table B doesn’t not contain "AAA" in code column

+=====+=====+=======+
| ID |Code |exCode |
+=====+=====+=======+
| | | |
| 1 | A | AAA |
+-----+-----+-------+
| 1 | B | B |
| | | |
+-----+-----+-------+

+=====+=====+=======+
| ID |Name | Code |
+=====+=====+=======+
| | | |
| 1 |Tom | A |
'-----+-----+-------+
| 1 |Bill | A |
| | | |
'-----+-----+-------+
| | | |
| 1 |Tom | B |
'-----+-----+-------+
| 1 |Bill | B |
| | | |
'-----+-----+-------+

After Rows Insertion i need to get below data for ID = 1 in table B

+=====+=====+=======+
| ID |Name | Code |
+=====+=====+=======+
| | | |
| 1 |Tom | A |
'-----+-----+-------+
| 1 |Bill | A |
| | | |
'-----+-----+-------+
| | | |
| 1 |Tom | B |
'-----+-----+-------+
| 1 |Bill | B |
| | | |
'-----+-----+-------+
| | | |
| 1 |Tom | AAA |
'-----+-----+-------+
| 1 |Bill | AAA |
| | | |
'-----+-----+-------+

3) for the above Table A and Table B, after the row additions my final Table B should as below

Final Table B

+=====+=====+=======+
| ID |Name | Code |
+=====+=====+=======+
| | | |
| 1 |Tom | A |
'-----+-----+-------+
| 1 |Bill | A |
| | | |
'-----+-----+-------+
| | | |
| 1 |Tom | B |
'-----+-----+-------+
| 1 |Bill | B |
| | | |
'---+-----+-------+
| | | |
| 1 |Tom | AAA |
'-----+-----+-------+
| 1 |Bill | AAA |
| | | |
'-----+-----+-------+
| 2 |Chad | AAA |
| | | |
'-----+-----+-------+
| 3 | A | A |
| | | |
'-----+-----+-------+
| 3 | A | AAA |
| | | |
'-----+-----+-------+

Looking forward to your help/solution. Thanks in advance



[/code]

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2009-09-04 : 15:19:49
Looks like homework.

If you actually did all that formatting yourself you would have saved yourself a lot of time by putting it into a usable format, like

Create table #TableA
( ID int not null,
Code varchar(10) null, ..... etc..

An infinite universe is the ultimate cartesian product.
Go to Top of Page

kneekill
Yak Posting Veteran

76 Posts

Posted - 2009-09-04 : 15:48:57
cat_jesus,

I did the formatting using ASCII. please find below some sample code.When i noticed the board isn't supporting HTML... I used ASCII, it hardly took couple of minutes

i am pretty bad with my SQL skills as i hardly work on it and I don't have an approach to solve the problem. I am trying to learn when ever i get a chance to do. I posted my problem here so that i could get help and also learn how the members approach and solve the issue.




use Text::ASCIITable;
$t = Text::ASCIITable->new({ headingText => 'Table A' });

$t->setCols('Id','Name','Code');
$t->addRow(1,'Tom','A');
$t->addRow(1,'Bill','A');
$t->addRowLine();

print $t;





quote:
Originally posted by cat_jesus

Looks like homework.

If you actually did all that formatting yourself you would have saved yourself a lot of time by putting it into a usable format, like

Create table #TableA
( ID int not null,
Code varchar(10) null, ..... etc..

An infinite universe is the ultimate cartesian product.

Go to Top of Page

kneekill
Yak Posting Veteran

76 Posts

Posted - 2009-09-07 : 06:28:33
Below is the syntax and data for the above 2 tables

 

CREATE TABLE #TableA(
ID int,
Code varchar(10),
exCode varchar(10)
)


INSERT INTO #TableA (ID, Code,exCode)
SELECT 1 ,'A','AAA'
UNION ALL
SELECT 1 ,'B','BBB'
UNION ALL
SELECT 2 ,'A','AAA'
UNION ALL
SELECT 3 ,'A','AAA'

-- select * from #TableA
-- DROP Table #TableA

CREATE TABLE #TableB(
ID int,
Name varchar(10),
Code varchar(10)
)


INSERT INTO #TableB (ID,Name,Code)
SELECT 1 ,'Tom','A'
UNION ALL
SELECT 1 ,'Bill','A'
UNION ALL
SELECT 2 ,'Chad','AAA'
UNION ALL
SELECT 3 ,'A','A'


-- select * from #TableB
-- DROP Table #TableB


Go to Top of Page

kneekill
Yak Posting Veteran

76 Posts

Posted - 2009-09-07 : 06:31:32
This is the query i wrote to insert the extra rows. problem with the query is it is inserting rows into Table B for an ID which already has an entry for "Code" = AAA



Insert Into #TableB

select distinct b.ID,b.Name,'AAA' as Code from #TableB b
,(select * from #TableA where (exCode = 'AAA') ) a
where a.ID=b.ID order by b.id asc

Go to Top of Page

kneekill
Yak Posting Veteran

76 Posts

Posted - 2009-09-07 : 08:31:32
this is what i have come up with and looks like its working fine

Insert Into #TableB

select distinct b.ID,b.Name,'AAA' as Code from #TableB b
,((select * from #TableA where ID not in (select distinct ID from #TableB where code = 'AAA' ) ) ) a
where a.ID=b.ID and a.code <> 'AAA' order by b.id asc
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-09-07 : 14:39:58
[code]Insert INTO #TableB
SELECT t.*,'AAA'
FROM (SELECT DISTINCT b.ID,b.Name
FROM TableB b
INNER JOIN TableA a
ON a.ID=b.ID
AND a.exCode='AAA') t
LEFT JOIN TableB tb
ON tb.ID=t.ID
AND tb.exCode='AAA'
WHERE tb.ID IS NULL
[/code]
Go to Top of Page
   

- Advertisement -