简体   繁体   中英

SQL - Calculated field base on calculated field?

Hi I have two columns which I concatenate A&B, this is done to figure out the customer ID. I need to then do a case statement on that calculated field. Can it be done in one go?

Column A
Column B
Column C = Column A & Column B
Column D = Case When (column C) = 001-002 then XXXX

Column D is not working. The question is if column C is a calculated field, can I then calculate off column C to give column D, in a single query?

Thanks

You should do it in this way...as calculated columns is not going to help in further calculation

Column A
Column B
Column C = Column A & Column B
Column D = Case When (Column A & Column B) = 001-002 then XXXX

I think, the logic you have used, should work, the problem that I see can be in "001-002". Are you comparing Column D with the subtracted value from 001 to 002? If not, then you should enclose this in single quotes.

And also, as per your logic written in T-SQL, you do not need to calculate Column C. So, the solution may be as:

Column A
Column B
--Column C = Column A & Column B --commented out
Column D = Case When (Column A+Column B) = '001-002' then XXXX END

You can also use ELSE condition inside CASE Expression. Also, I assume Column A and B are of String datatype (ie either CHAR/VARCHAR/NVARCHAR).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM