1

When I select the table

Select 
    [Premium Amount], [Policy Number], 
    [Transaction Type Code], Insured, * 
from 
    DW.Table
where 
    [Policy Number] in ('1111111111', '2222222222')

these are the results.

Premium Amount  Policy Number   Transaction Type Code      Insured
-12798.0000     1111111111       Cancellation              ABC Group
27000.0000      1111111111       Renewal Business          ABC Group 
-8452.7700      2222222222       Cancellation              DEF Group
10221.0000      2222222222       Renewal Business          DEF Group

What I want to happen is to create two new columns, [Original Policy Premium] and [Transaction Premium].

[Original Policy Premium] is a column that contains when the Transaction Type Code = Renewal Business and it will result the corresponding Premium Amount = 27000.0000 for Policy Number 111111111. Same applies to Policy Number 22222222.

Make it look like this.

Original Policy Premium  Transaction Premium  Policy Number   
27000.0000                -12798.0000          1111111111
10221.0000                -8452.7700           2222222222

Select 
    case 
       when [Transaction Type Code] = 'Renewal Business' 
         then [Premium Amount] 
         else '' 
    end as [Original Policy Premium],
    case 
      when [Transaction Type Code] = 'Cancellation' 
        then [Premium Amount] 
        else '' 
    end as [Transaction Premium],
    [Policy Number], 
    [Transaction Code] 
from 
    DW.Table
where 
    [Policy Number] in ('111111111', '22222222')

But I get this error after running.

Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.

But when I replace END as '' with END AS NULL

Original Policy Premium  Transaction Premium   Policy Number   
    27000.0000                NULL             1111111111
    NULL                     -12798.0000       1111111111
    10221.0000                NULL             2222222222
    NULL                     -8452.7700        2222222222
2
  • 1
    Change to else null Commented Jul 7, 2015 at 21:33
  • Thank you. your answer is correct. But please look at my edits. Commented Jul 7, 2015 at 21:39

1 Answer 1

1

Your case paths return different types. They must be of same type. Change to:

select max(case when [Transaction Type Code] = 'Renewal Business'
                then [Premium Amount] 
                else null end) as [Original Policy Premium],
       max(case when [Transaction Type Code] = 'Cancellation'
                then [Premium Amount] 
                else null end) as [Transaction Premium],
       [Policy Number]
from table
where [Policy Number] in ('111111111', '22222222')
group by [Policy Number]
Sign up to request clarification or add additional context in comments.

3 Comments

SUPERB! But how does the MAX function and Group by help do the trick? Using max will only get the highest number, but there's no other number higher than 27000.0000 or 10221.0000? These numbers are unique. So how does it help to fix it?
It is called conditional aggregation and standart trick. For example for 1111111111 you have one cancellation and one renewal. Look at cancellation case: it will produce set - -12798.0000 and null. Max will pick -12798.0000 . Same for renewal. Same for 2222222222.
Now I get it. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.