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.
| 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 Bexample: 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. |
 |
|
|
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 minutesi 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.
|
 |
|
|
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 ALLSELECT 1 ,'B','BBB'UNION ALLSELECT 2 ,'A','AAA'UNION ALLSELECT 3 ,'A','AAA'-- select * from #TableA -- DROP Table #TableACREATE TABLE #TableB(ID int,Name varchar(10), Code varchar(10) )INSERT INTO #TableB (ID,Name,Code)SELECT 1 ,'Tom','A' UNION ALLSELECT 1 ,'Bill','A'UNION ALLSELECT 2 ,'Chad','AAA'UNION ALLSELECT 3 ,'A','A'-- select * from #TableB-- DROP Table #TableB |
 |
|
|
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" = AAAInsert Into #TableBselect distinct b.ID,b.Name,'AAA' as Code from #TableB b,(select * from #TableA where (exCode = 'AAA') ) awhere a.ID=b.ID order by b.id asc |
 |
|
|
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 fineInsert Into #TableBselect 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' ) ) ) awhere a.ID=b.ID and a.code <> 'AAA' order by b.id asc |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-07 : 14:39:58
|
| [code]Insert INTO #TableBSELECT 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') tLEFT JOIN TableB tbON tb.ID=t.IDAND tb.exCode='AAA'WHERE tb.ID IS NULL[/code] |
 |
|
|
|
|
|
|
|