| Author |
Topic |
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-10 : 00:47:19
|
[code]DECLARE @Tempfun1 TABLE( col1 CHAR(1), col2 INT)INSERT INTO @Tempfun1SELECT 'A', 1 UNION ALLSELECT 'A', 2 UNION ALLSELECT 'A', 3 UNION ALLSELECT 'A', 4 UNION ALLSELECT 'B', 1 UNION ALLSELECT 'B', 2 UNION ALLSELECT 'B', 3 UNION ALLSELECT 'B', 4DECLARE @Tempfun2 TABLE( col2 INT)INSERT INTO @Tempfun2SELECT 1 UNION ALLSELECT 2 UNION ALLSELECT 3[/code]How to get the followingA 1A 2A 3B 1B 2B 3B 4the condition are when col1=B, select all B but when col1=A, select matched record between 2 table<- having flu Hope can help...but advise to wait pros with confirmation... |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-10 : 00:50:09
|
can it be done without subquery?SELECT *FROM ( SELECT col1, CASE WHEN col1='A' then t2.col2 ELSE t1.col2 END col2 FROM @Tempfun1 t1 left join @Tempfun2 t2 on t1.col2=t2.col2 )aWHERE col2 IS NOT NULL Hope can help...but advise to wait pros with confirmation... |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-08-10 : 01:16:46
|
| SELECT DISTINCT T1.COL1,T1.COL2 FROM @TEMPFUN1 T1INNER JOIN @TEMPFUN2 T2 ON T1.COL2 = ( CASE WHEN T1.COL1 = 'A' THEN T2.COL2 ELSE T1.COL2 END) |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-08-10 : 01:19:12
|
try like thisSELECT distinct col1, isnull(t2.col2,t1.col2)FROM @Tempfun1 t1 left join @Tempfun2 t2 on t2.col2 = CASE WHEN col1='A' then t2.col2 ELSE t1.col2 END |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-10 : 01:49:31
|
wow thx thx ^^ Hope can help...but advise to wait pros with confirmation... |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-08-10 : 01:53:16
|
welcome |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-10 : 02:28:55
|
[code]DECLARE @Tempfun1 TABLE( col1 CHAR(1), col2 INT, col3 CHAR(3))INSERT INTO @Tempfun1SELECT 'A', 1, 'PDO' UNION ALLSELECT 'A', 2, 'GRO' UNION ALLSELECT 'A', 3, 'PDO' UNION ALLSELECT 'A', 4, 'GRO' UNION ALLSELECT 'B', 1, 'PDO' UNION ALLSELECT 'B', 2, 'GRO' UNION ALLSELECT 'B', 5, 'PDO' UNION ALLSELECT 'B', 4, 'GRO'DECLARE @Tempfun2 TABLE( col2 INT)INSERT INTO @Tempfun2SELECT 1 UNION ALLSELECT 2 UNION ALLSELECT 3[/code]how to get the followingA 1A 2A 3A 4B 1B 2B 4where the condition are PDO(join), GRO(left join) Hope can help...but advise to wait pros with confirmation... |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-08-10 : 02:44:16
|
| SELECT distinct col1, t1.col2FROM @Tempfun1 t1 join @Tempfun2 t2 on t2.col2 = t1.col2 and t1.col3 = 'pdo'union SELECT distinct col1, t1.col2FROM @Tempfun1 t1 left join @Tempfun2 t2 on t2.col2 = t1.col2 where t1.col3 = 'gro' |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-08-10 : 02:45:46
|
quote: Originally posted by waterduck wow thx thx ^^ Hope can help...but advise to wait pros with confirmation...
welcome |
 |
|
|
|