?
Solved

sql update (better version of Q_22755932.html)

Posted on 2007-08-11
4
Medium Priority
?
186 Views
Last Modified: 2013-11-05
sorry for the confusion (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_22755932.html).
i will try to explain one more time:

table 1 = t1 has columns c1, c2, c3, c4, and c5
table 2 = t2 has columns c1, c2, c3
table 3 = t3 has columns c1, c2

t1.c1 corresponds to t2.c1 that is t1.c1 is populated with ABC1,ABC2,ABC3..etc. while t2.c1 is populated with ABC1, ABC1, ABC2, ABC2, ABC3 , ABC3. there are to rows/entries in t2 for every entry in t1.

column c3 of table2 is populated with XYZ1, XYZ4, XYZ1 ,XYZ5, XYZ1, XYZ10. column c2 of table3 is populated with XZY1,XYZ2,ZYZ3, ... XYZN (all avaialble enetries)

i need to updated table2.c2 with table3.c1 in case when

table1.c4 = 'Val1' and table1.c5 = 'val5'

and table1.c1 = table2.c1
and table2.c3 =table3.c2
0
Comment
Question by:jgordin
  • 2
  • 2
4 Comments
 
LVL 58

Accepted Solution

by:
amit_g earned 2000 total points
ID: 19677491
Execute

select t1.*, '|', t2.*, '|', t3.*
from table2 t2
inner join table1 t1 on t1.c1 = t2.c1
inner join table3 t3 on t2.c2 = t3.c2
where t1.c4 = 'Val4' and t1.c5 = 'val5'

does this line up the columns properly so as in each row t2.c2 can be updated with t3.c1. If so your update statement is

update table2
set c2 = t3.c1
from table2 t2
inner join table1 t1 on t1.c1 = t2.c1
inner join table3 t3 on t2.c2 = t3.c2
where t1.c4 = 'Val4' and t1.c5 = 'val5'
0
 

Author Comment

by:jgordin
ID: 19683776
when i run:
select count(*)
from table2 t2
inner join table1 t1 on t1.c1 = t2.c1
where t1.c4 = 'Val4' and t1.c5 = 'val5'

i get the number N which is 2*the number of rows in tabe t1.

If i run

select count(*)
from table2 t2
inner join table1 t1 on t1.c1 = t2.c1
inner join table3 t3 on t2.c2 = t3.c2
where t1.c4 = 'Val4' and t1.c5 = 'val5'

I get the number N >>>>> then number of rows in table1.

I dont think that makes sense I would expect to see the same number of rows as in the first query.

Anyone has any ideas?

Thanks.
0
 
LVL 58

Expert Comment

by:amit_g
ID: 19686519
There must be more than one item in table 3 for the join condition t2.c2 = t3.c2 and that is why the more rows must be resulting from the join.
0
 

Author Comment

by:jgordin
ID: 19686971
i figure that out. thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question