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
 General SQL Server Forums
 New to SQL Server Programming
 populating bunch of records.

Author  Topic 

subhaoviya
Posting Yak Master

135 Posts

Posted - 2010-07-14 : 18:27:07
Hi ,

sample Input table:

Field1 field2 field3
1 a 78
1 b 18
1 c 11
1 d 1
2 a *
2 b 1
2 c *
2 d *


reuired output:

Field1 field2 field3
1 a 78
1 b 18
1 c 11
1 d 1
2 a 78
2 b 18
2 c 11
2 d 1

based on field3 values of field1=1 needs to be populated for field3 of field1=2

thanks in advance for help

by subha

Devart
Posting Yak Master

102 Posts

Posted - 2010-07-15 : 02:18:26
Hello,

You can try this:

SELECT
t.Field1,
t.Field2,
CASE WHEN t.Field1=1
THEN t.Field3
ELSE (SELECT Field3 FROM <your_table_name> WHERE Field1=1 AND Field2=t.Field2) END AS Field3
FROM <your_table_name> t

Best regards,

Devart Team
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2010-07-15 : 04:31:26
select t.field1,t.field2,isnull(s.field3,t.field3)as field3
from table t
left join table s on s.field2 = t.field2
Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-07-15 : 05:26:32
quote:
Originally posted by bklr

select t.field1,t.field2,isnull(s.field3,t.field3)as field3
from table t
left join table s on s.field2 = t.field2



It will not give the required output.

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2010-07-15 : 05:39:02
quote:
Originally posted by vaibhavktiwari83

quote:
Originally posted by bklr

select t.field1,t.field2,isnull(s.field3,t.field3)as field3
from table t
left join table s on s.field2 = t.field2



It will not give the required output.

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER


s i forgot to add this condition
select t.field1,t.field2,isnull(s.field3,t.field3)as field3
from table t
left join table s on s.field2 = t.field2
and s.Field1=1
Go to Top of Page
   

- Advertisement -