SQL: Add a new column based on CASE expression and looking up values from another table

丶灬走出姿态 提交于 2019-12-12 04:03:24

问题


I am trying to add a new column called Multiplier to an existing table called Trades. The row values of this column will depend on another column on the Trades table called Type. If the Type is anything other than "Equity", "Corp" or "Option, then the value needs to be looked up from another table called ContractSize. Lastly, I want the data type of the Multiplier column to be decimal (7,3). The code I had was:

ALTER TABLE Portfolio.Trades
ADD Multiplier decimal(7,3) AS
(
CASE 
 WHEN Type = 'Equity' Then 1
 WHEN Type = 'Corp' Then 0.1
 WHEN Type = 'Option' Then 100
ELSE
 (SELECT ContractSize FROM Portfolio.ContractSize CS
 JOIN Portfolio.Trades T
 ON T.Identifier = CS.ContractSize)
)

I am getting two errors on this code:

Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'AS'.
Msg 102, Level 15, State 1, Line 12 Incorrect syntax near ')'.

Some guidance is much appreciated.


回答1:


Do this in two steps:

ALTER TABLE Portfolio.Trades ADD Multiplier decimal(7,3);

UPDATE T
    SET Multiplier = (CASE WHEN T.Type = 'Equity' Then 1
                           WHEN T.Type = 'Corp' Then 0.1
                           WHEN T.Type = 'Option' Then 100
                           ELSE (SELECT CS.ContractSize
                                 FROM Portfolio.ContractSize CS
                                 WHERE T.Identifier = CS.ContractSize)
                       END)
    FROM Portfolio.Trades T;

I am guessing that the ON/correlation condition is incorrect. If it is correct, you can just do:

UPDATE T
    SET Multiplier = (CASE WHEN T.Type = 'Equity' Then 1
                           WHEN T.Type = 'Corp' Then 0.1
                           WHEN T.Type = 'Option' Then 100
                           ELSE T.Identifier)
                       END)
    FROM Portfolio.Trades T;

Or even just add this as a computed column.




回答2:


Why add column to the table? You can create a view alternatively.

CREATE VIEW myView AS
    SELECT *, -- your real column list here
        CAST(CASE
         WHEN Type = 'Equity' Then 1
         WHEN Type = 'Corp' Then 0.1
         WHEN Type = 'Option' Then 100
         ELSE
           (SELECT ContractSize 
            FROM Portfolio.ContractSize CS
            WHERE T.Identifier = CS.ContractSize)
         END AS decimal(7,3)) AS  Multiplier
    FROM
    Portfolio.Trades T;



回答3:


Every CASE statement must have a END keyword at the end of the condition. In this case it should be like this.

ALTER TABLE Portfolio.Trades
ADD Multiplier decimal(7,3) AS
(
CASE 
 WHEN Type = 'Equity' Then 1
 WHEN Type = 'Corp' Then 0.1
 WHEN Type = 'Option' Then 100
ELSE
 (SELECT ContractSize FROM Portfolio.ContractSize CS
 JOIN Portfolio.Trades T
 ON T.Identifier = CS.ContractSize)END
)


来源:https://stackoverflow.com/questions/44976581/sql-add-a-new-column-based-on-case-expression-and-looking-up-values-from-anothe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!