SQL中的大小写表达问题

时间:2018-11-23 06:49:55

标签: sql mariadb

我正在使用IF语句并使用case语句来做到这一点。当我这样做时,我会得到一个错误。

基本上,如果安全类型为'Currency Future。',我想将其输出为'NetDetail',否则为'Detail'

SQL语句如下:

错误看起来像这样:

<e>Query: SELECT `Desk`, `VT_id`, `BloombergID`,`Id_Type`,`Position` ,`Price`, `positions_desk`.`Multiplier`,`CCY`,`business_date`,`APAC_E...

Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CASE 
     WHEN (`APAC_Exposures_Map`.`Security_Type`) THEN 'NetDetail'
                        ' at line 3

4 个答案:

答案 0 :(得分:1)

您需要指定条件

SELECT `Desk`, `VT_id`, `BloombergID`,`Id_Type`,`Position` ,`Price`, `positions_desk`.`Multiplier`,`CCY`,`business_date`,`APAC_Exposures_Map`.`Equity_Index`,`APAC_Exposures_Map`.`Security_Type`,`APAC_Exposures_Map`.`Leverage`, IF(Id_Type !='FWD',COALESCE(`APAC_Exposures_Map`.`Expiry`, `CBBC_Static_Data`.`Expiration_date`), STR_TO_DATE(SUBSTRING(`VT_id`, 7, 6),'%y%m%d') ) AS `Expiration_date`, `CBBC_Static_Data`.`Strike`
,
CASE WHEN `APAC_Exposures_Map`.`Security_Type`='YOUR Condition' THEN 'NetDetail'
                         ELSE 'Detail' 
END AS `Record_Type` FROM `risk_source`.`positions_desk`
LEFT JOIN (`risk`.`APAC_Exposures_Map`) ON`positions_desk`.`BloombergID`=UPPER(`APAC_Exposures_Map`.`BBG_Ticker`)
LEFT JOIN (`risk_source`.`CBBC_Static_Data`) ON LEFT(`positions_desk`.`BloombergID`,5) =`CBBC_Static_Data`.`CBBC_Name`
WHERE business_date= ? AND (Id_Type='FUT') AND Desk NOT IN ('HKCOI')

答案 1 :(得分:1)

您错过陈述条件时的情况   case when APAC_Exposures_MapSecurity_Type = //此处需要条件值,此选择将​​在表名之前

 SELECT `Desk`, `VT_id`, `BloombergID`,`Id_Type`,`Position` ,`Price`, 
`positions_desk`.`Multiplier`,`CCY`,`business_date`,
`APAC_Exposures_Map`.`Equity_Index`,`APAC_Exposures_Map`.`Security_Type`,
`APAC_Exposures_Map`.`Leverage`,
IF(Id_Type !='FWD',COALESCE(`APAC_Exposures_Map`.`Expiry`, `CBBC_Static_Data`.`Expiration_date`), 
STR_TO_DATE(SUBSTRING(`VT_id`, 7, 6),'%y%m%d') ) AS `Expiration_date`,
`CBBC_Static_Data`.`Strike`,
(CASE WHEN (`APAC_Exposures_Map`.`Security_Type`='need value') THEN 'NetDetail' ELSE 'Detail' 
END) AS `Record_Type`
    FROM `risk_source`.`positions_desk`    
    LEFT JOIN (`risk`.`APAC_Exposures_Map`) ON`positions_desk`.`BloombergID`=UPPER(`APAC_Exposures_Map`.`BBG_Ticker`)
    LEFT JOIN (`risk_source`.`CBBC_Static_Data`) ON LEFT(`positions_desk`.`BloombergID`,5) =`CBBC_Static_Data`.`CBBC_Name`
    WHERE business_date= ? AND (Id_Type='FUT') AND Desk NOT IN ('HKCOI')

答案 2 :(得分:0)

您忘记了一些东西:

(CASE 
     WHEN (`APAC_Exposures_Map`.`Security_Type` =  'Currency Future') THEN 'NetDetail'
                         ELSE 'Detail' 
END) AS `Record_Type`

条件应为:

`APAC_Exposures_Map`.`Security_Type` =  'Currency Future'

答案 3 :(得分:0)

可以测试吗?

SELECT `Desk`, `VT_id`, `BloombergID`,`Id_Type`,`Position` ,`Price`, `positions_desk`.`Multiplier`,`CCY`,`business_date`,`APAC_Exposures_Map`.`Equity_Index`,`APAC_Exposures_Map`.`Security_Type`,`APAC_Exposures_Map`.`Leverage`, CASE WHEN(Id_Type !='FWD' THEN COALESCE(`APAC_Exposures_Map`.`Expiry`, `CBBC_Static_Data`.`Expiration_date`), STR_TO_DATE(SUBSTRING(`VT_id`, 7, 6),'%y%m%d') ) ELSE GETDATE() AS `Expiration_date`, `CBBC_Static_Data`.`Strike`
FROM `risk_source`.`positions_desk`,
(CASE 
     WHEN (`APAC_Exposures_Map`.`Security_Type`) THEN 'NetDetail'
                         ELSE 'Detail' 
END) AS `Record_Type`
LEFT JOIN (`risk`.`APAC_Exposures_Map`) ON`positions_desk`.`BloombergID`=UPPER(`APAC_Exposures_Map`.`BBG_Ticker`)
LEFT JOIN (`risk_source`.`CBBC_Static_Data`) ON LEFT(`positions_desk`.`BloombergID`,5) =`CBBC_Static_Data`.`CBBC_Name`
WHERE business_date= ? AND (Id_Type='FUT') AND Desk NOT IN ('HKCOI')