我在MY SQL服务器中创建了一个视图,我需要使用CASE
条件才能满足要求。
select
cast([SIR Per Occ] as float) * 1000000 as [SIR Per Occ],
cast([SIR Agg] as float) * 1000000 as [SIR Agg],
cast([Per Occ Limit Between] as float) * 1000000 as [Per Occ Limit Between],
cast((cast([SIR Per Occ] as float)) + (cast([Per Occ Limit Between] as float)) as float) * 1000000 as [Policy Occ Attachment],
cast((cast([SIR Agg] as float)) + (cast([Per Occ Limit Between] as float)) as float) * 1000000 as [Policy Agg Attachment]
from IT.dbo.Policy_find
但是在我的任务中,如果[Per Occ Limit Between]
是NULL
值,那么我需要假设它为零,上面的代码应该按照以下逻辑运行:
Policy Occ Attachment = [SIR Per Occ]
Policy Agg Attachment = [SIR Agg]
如何在不使用where
条件的情况下在select子句中实现此类场景?
答案 0 :(得分:2)
只需将CASE与列定义一起使用,而不是列名称(CASE MSDN here)。
EDIT(对于SQL Server):如果要删除NULL值,可以使用ISNULL(YOUR_EXPRESSION, '')
答案 1 :(得分:0)
我无法确定它是我的“SQL服务器”还是“我的SQL”服务器。
但实际上,对于这个特定的查询并不重要。
使用COALESCE
select
cast([SIR Per Occ] as float) * 1000000 as [SIR Per Occ],
cast([SIR Agg] as float) * 1000000 as [SIR Agg],
cast([Per Occ Limit Between] as float) * 1000000 as [Per Occ Limit Between],
cast((cast([SIR Per Occ] as float)) + (cast(COALESCE([Per Occ Limit Between],0) as float)) as float) * 1000000 as [Policy Occ Attachment],
cast((cast([SIR Agg] as float)) + (cast([Per Occ Limit Between] as float)) as float) * 1000000 as [Policy Agg Attachment]
from IT.dbo.Policy_find