通过查询手动输入数据到sql数据库

时间:2016-01-07 07:53:36

标签: sql sql-server

我有这个问题:

select 
    a.secCode, b.[Haircut (%)], c.[Absolute IDR (Miliar)] 
from 
    openquery(PDC_MYAPPS, 'select secCode from mii.secReq') a 
left join 
    (select * from RC_Saham) b on a.secCode = b.kode 
left join 
    (select * from Absolute_IDR) c on b.kode = c.[Kode Saham]
where 
    b.[Haircut (%)] is not null

union

select 
    a.code_Ic, a. PERC_HRCT, b.[Absolute IDR (Miliar)]
from 
    openquery(MANTARAY, 'select b.code_ic, a.PERC_HRCT 
                         from cminstruments a, instrument_codes b
                         where a.ID_CMI_CAPCO = b.INS_ID_INS_CAPCO') a   
left join 
    (select * from Absolute_IDR) b on a.code_Ic = b.[Kode Saham]
where 
    code_Ic in (select a.secCode 
                from openquery(PDC_MYAPPS, 'select secCode 
                                            from mii.secReq') a 
                                            left join (select * from RC_Saham) b on a.secCode = b.kode 
                                            left join (select * from Absolute_IDR) c on b.kode = c.[Kode Saham]
                                            where b.[Haircut (%)] is null)  

我得到了结果:

 KODE    |HAIRCUT|  Absolutes IDR 
 MLIA     94.00         13                 
 SIMA     100.00        20             
 CTRA     20.00         300
 NRCA-W   100.00        NULL
 VINS     60.00         30   
 NRCA-W   100.00        NULL

我想直接在上面的查询中使用基于发夹值的数字填充空值。

让我们说haircut = 100然后absolutes IDR = 20

如何在SQL查询中创建它?

1 个答案:

答案 0 :(得分:1)

您可以使用ISNULL()CASE

select 
    a.secCode, b.[Haircut (%)],
    ISNULL(c.[Absolute IDR (Miliar)], CASE b.[Haircut (%)]
        WHEN 100 then 20
        WHEN 80 THEN 40
        ELSE 0 END )

有关详细信息,请参阅MSDN on CASE