Anyone know a better way to do these field calculations in a SQL Server View?
SELECT
TCQ.Generic,
TPM.WRITTEN_PRM / ((100 - TPM.[CO-PART]) / 100 * CASE WHEN TPM.Factor IS NULL THEN 1 ELSE TPM.[Factor] END) AS Gross100_WRITTEN_PRM,
TPM.EARNED_PRM / ((100 - TPM.[CO-PART]) / 100 * CASE WHEN TPM.Factor IS NULL THEN 1 ELSE TPM.[Factor] END) AS Gross100_EARNED_PRM,
TC.Generic = GS.Generic
(FROM table table table blah blah...)
CodePudding user response:
You can use COALESCE() or ISNULL() instead of CASE.
SELECT
TCQ.Generic,
TPM.WRITTEN_PRM / ((100 - TPM.[CO-PART]) / 100 * coalesce ( TPM.Factor, 1) ) AS Gross100_WRITTEN_PRM,
TPM.EARNED_PRM / ((100 - TPM.[CO-PART]) / 100 * coalesce ( TPM.Factor, 1)) AS Gross100_EARNED_PRM,
TC.Generic = GS.Generic
(FROM table table table blah blah...)
CodePudding user response:
You could refactor as something like the following - untested of course
select
TPM.WRITTEN_PRM / f.v as Gross100_WRITTEN_PRM,
TPM.EARNED_PRM / f.v as Gross100_EARNED_PRM,
from
tables
cross apply (values(100 - TPM.[CO-PART]/isnull(TPM.[Factor],1)*100))f(v)