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
 conditional colums select (join)

Author  Topic 

dainova
Starting Member

31 Posts

Posted - 2010-01-13 : 02:33:57
Hi, all, bit stuck with small task (for Pros, of course), will appreciate you help. I have:
Table X1:
MainX ___ FlagX
M440 ..... NULLl
M450 ..... 1
M777 ..... NULL


Table D1:
Sub ___ Main___ AmT
L14 .....M440 ... 22220.00
L15 ..... M450 ... 333.00
L17 ..... M777 ... 6578.00


I need to select like this:
L14.....M440....22220
M450....M450....333 <=== here is replace
L17.....M777....6578

i.e: IF Main M450 has X1.Flag = 1 ,
..... then select D1.Main, D1.Main, D1.AmT
.. else
..... select D1.Sub, D1.Main, D1.AmT.

I bit new to SQL, I composed something scary with nested, but I feel should be smarter option, I’m on SQL 2000. Somebody directed me to Coalesce, but I’m not sure.
Will appreciate if you push me to the right direction.

Tx to all

Dai

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-13 : 02:39:47
[code]select case when X1.FlagX = 1 then D1.Main else D1.Sub end AS Sub,
D1.Main,
D1.Amt
from X1
join D1
on D1.Main=X1.MainX
[/code]
Go to Top of Page
   

- Advertisement -