Skip to content
Advertisement

How can I select multiple columns while using “when” statement

Here is the syntax I wanted to use for “loan amount” column. I am creating a data set on ssas for reporting. But I am having a problem adding “loan id” column based on the syntax since I need both “loan amount” and “loan ID” in the same data set. How can integrate “LOAN ID” while keeping the syntax below for “loan amount”?

SELECT DISTINCT
    CASE    
        WHEN [LoanAmount] <= 100000 THEN 'Less Than $100k'
        WHEN [LoanAmount] BETWEEN 100000 AND 200000 THEN '$100k to $200k'
        WHEN [LoanAmount] > 200000 THEN 'More Than $200k' 
    END [LoanAmount]
FROM
    [dbo].[dim loan detail]

Advertisement

Answer

If you only need to add LOAN_ID, you just separate each column with a comma after the SELECT and before the FROM. Also, in this case the “DISTINCT” keyword is not necessary since every LOAN_ID will be unique, and therefore distinct.

SELECT
LOAN_ID,
CASE    
WHEN [LoanAmount] <= 100000 THEN 'Less Than $100k'
WHEN [LoanAmount] between 100000 AND 200000 THEN '$100k to $200k'
WHEN [LoanAmount] > 200000 THEN 'More Than $200k' 
END [LoanAmount]
FROM [dbo].[dim loan detail]
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement