我有一个查询将值插入表中,并使用CASE来确定每行的值。我想知道在它结束时是否有可能从计算列中排除null或零?
CASE
WHEN Value = 0 THEN Value2
WHEN Value2 < Value1 THEN Value3
WHEN Value2 > Value1 THEN Value4
END AS Final_Value
这会产生几十万行,其中几千行现在为零或为零。我可以在同一段代码中排除这些吗?
由于
答案 0 :(得分:1)
使用where
子句:
select
...
, case
when value = 0
then value2
when value2 < value1
then value3
when value2 > value1
then value4
end as final_value
from ...
where case
when value = 0 then value2
when value2 < value1 then value3
when value2 > value1 then value4
end <> 0
答案 1 :(得分:0)
在查询结束时可以避免0或null值,如
SELECT
ACCGRPID,
LOCID,
USERID4,
LOBNAME,
REINSTYPE,
INCEPTDATE,
EXPIREDATE,
Latitude,
Longitude,
CountryRMSCode,
StreetAddress,
CityName,
ACCGRPNUM,
ACCGRPNAME,
CASE
WHEN Value = 0 THEN Value2
WHEN Value2 < Value1 THEN Value3
WHEN Value2 > Value1 THEN Value4
END AS Final_Value
FROM <table_name>
WHERE Final_Value <> 0
答案 2 :(得分:0)
;WITH CTE AS (
select
ACCGRPID,
LOCID,
USERID4,
LOBNAME,
REINSTYPE,
INCEPTDATE,
EXPIREDATE,
Latitude,
Longitude,
CountryRMSCode,
StreetAddress,
CityName,
ACCGRPNUM,
ACCGRPNAME,
CASE
WHEN Value = 0 THEN Value2
WHEN Value2 < Value1 THEN Value3
WHEN Value2 > Value1 THEN Value4
END AS Final_Value
)
select * from CTE
where Final_Value <> 0
and Final_Value is not null
答案 3 :(得分:0)
如果无法在where子句中进行过滤,那么在某些情况下我会使用一个简单的解决方案,并使用子选择。
示例:
select X.* from (
SELECT
ACCGRPID,
LOCID,
USERID4,
LOBNAME,
REINSTYPE,
INCEPTDATE,
EXPIREDATE,
Latitude,
Longitude,
CountryRMSCode,
StreetAddress,
CityName,
ACCGRPNUM,
ACCGRPNAME,
CASE
WHEN Value = 0 THEN Value2
WHEN Value2 < Value1 THEN Value3
WHEN Value2 > Value1 THEN Value4
END AS Final_Value
from <table_name>) X
where X.Final_Value != 0 and X.Final_Value is not null
答案 4 :(得分:0)
检查案例,以便它不会导致0或NULL
例如:
CASE
WHEN Value = 0 THEN Value2
WHEN Value2 <= Value1 THEN Value3
WHEN Value2 > Value1 THEN Value4
ELSE Value2
END AS Final_Value
当然,这个假定Value2,Value3和Value4是非NULL而不是0。