Using Complex IF Statement in Oracle SQL

前端 未结 1 1471
失恋的感觉
失恋的感觉 2021-01-25 06:53

Hi i\'m trying to write If statement in Band Attribute but without Success T_T. what Im trying to do is have single Character A, B, C, D depending on the owner_category, Sheds a

相关标签:
1条回答
  • 2021-01-25 07:19

    Read the documentation on virtual columns:

    CREATE TABLE Rent_Band
    (
        Plot_ID NUMBER(3),
        Band VARCHAR(1) AS
           (CASE WHEN Owner_Category = 'Council' AND Sheds ='No' THEN 'A'
                WHEN Owner_Category = 'Private' AND Water_Supply ='Yes' THEN 'D'
                WHEN Owner_Category = 'Council' AND Water_Supply ='No' AND Sheds ='Yes' THEN 'B'
                WHEN Owner_Category = 'Private' AND Water_Supply ='No' AND Sheds ='No' THEN 'B'
           ELSE 'C' END),
        Rent_Charge NUMBER(4), 
        Owner_Category VARCHAR(10) CONSTRAINT Checking_Owner_Category CHECK((Owner_Category='Private') OR (Owner_Category='Council')), 
        Sheds VARCHAR(3) CHECK((Sheds='Yes') OR (Sheds='No')), 
        Water_Supply VARCHAR(3) CHECK((Water_Supply='Yes') OR (Water_Supply='No'))
    )
    

    BTW, I would STRONGLY recommend:

    • using a bit field instead of a Yes/No constraint for Sheds and Water_Supply (saves room and overhead checking constraints, and translates directly to boolean in most app languages)
    • using a lookup table for Owner_Category instead of a string constraint for basically the same reasons
    0 讨论(0)
提交回复
热议问题