当我选择表格时
Select
[Premium Amount], [Policy Number],
[Transaction Type Code], Insured, *
from
DW.Table
where
[Policy Number] in ('1111111111', '2222222222')
这些是结果。
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
我想要发生的是创建两个新列,[Original Policy Premium]和[Transaction Premium]。
[原始保单保费]是一个列,其中包含交易类型代码=续订业务时,它将导致相应的保费金额= 27000.0000,保单号为111111111 。同样适用于政策号22222222.
让它看起来像这样。
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')
但是我跑完后得到了这个错误。
Msg 8114,Level 16,State 5,Line 1
将数据类型varchar转换为数字时出错。
但是当我用“END AS NULL”替换END作为''时
Original Policy Premium Transaction Premium Policy Number
27000.0000 NULL 1111111111
NULL -12798.0000 1111111111
10221.0000 NULL 2222222222
NULL -8452.7700 2222222222
答案 0 :(得分:1)
您的案例路径返回不同的类型。它们必须是同一类型。改为:
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]