## How to create scalar function of SQL Server in order to define nPNA?

The Japanese Society for Dialysis Therapy (JSDT) recommends PCR as an indicator of protein intake. Otherwise K/DOQQI recommends nPNA. If you calculate Kt/V with Daugirdas’ method, you can also define nPNA. $\displaystyle \mathrm{nPNA} = \frac{C_0}{36.3 + 5.48\times\mathrm{Kt/V} + 53.5/\mathrm{Kt/V}} + 0.168 \\ = \frac{\mathrm{preBUN}}{36.3 + 5.48\times\mathrm{Kt/V} + 53.5 / \mathrm{Kt/V}} + 0.168 \cdots(4)$

Execute the following procedure.

CREATE FUNCTION Function_nPNA
(		@preBUN		DEC(4, 1)
,	@postBUN	DEC(4, 1)
,	@preWeight	DEC(4, 1)
,	@postWeight	DEC(4, 1)
,	@DialysisDuration	int
)
RETURNS DEC(3, 2)
AS
BEGIN
DECLARE @nPNA	DEC(3, 2)
SELECT	@nPNA = @preBUN
/	(36.3 + 5.48 * (dbo.Function_KtV(@preBUN, @postBUN, @preWeight, @postWeight, @DialysisDuration))
+			53.5 / (dbo.Function_KtV(@preBUN, @postBUN, @preWeight, @postWeight, @DialysisDuration)))
+	0.168
RETURN	@nPNA
END


Reference: Simplified nutritional screening tools for patients on maintenance hemodialysis

## SQL Serverのスカラー値関数としてnPNAを定義するには

たんぱく質摂取量の指標として日本透析医学会では PCR を採用しています．一方 K/DOQQI では nPNA を採用しています．Daugirdas の方法で Kt/V を計算すると nPNA も計算できます． $\displaystyle \mathrm{nPNA} = \frac{C_0}{36.3 + 5.48\times\mathrm{Kt/V} + 53.5/\mathrm{Kt/V}} + 0.168 \\ = \frac{\mathrm{preBUN}}{36.3 + 5.48\times\mathrm{Kt/V} + 53.5 / \mathrm{Kt/V}} + 0.168 \cdots(4)$

下記プロシージャを実行して関数を作成します．

CREATE FUNCTION Function_nPNA
(		@preBUN		DEC(4, 1)
,	@postBUN	DEC(4, 1)
,	@preWeight	DEC(4, 1)
,	@postWeight	DEC(4, 1)
,	@DialysisDuration	int
)
RETURNS DEC(3, 2)
AS
BEGIN
DECLARE @nPNA	DEC(3, 2)
SELECT	@nPNA = @preBUN
/	(36.3 + 5.48 * (dbo.Function_KtV(@preBUN, @postBUN, @preWeight, @postWeight, @DialysisDuration))
+			53.5 / (dbo.Function_KtV(@preBUN, @postBUN, @preWeight, @postWeight, @DialysisDuration)))
+	0.168
RETURN	@nPNA
END


## How to define Kt/V, an indicator of the efficiency of dialysis, as scalar function of SQL Server?

In Japan, Shinzato’s fomula for calculating Kt/V, an indicator of efficiency of dialysis, is recommended by JSDT. Since integral equation is used to solve Shinzato’s method, you couldn’t solve algebraically. In K/DOQQI, it is usual to solve Kt/V with Daugirdas’ method. Shinzato has described that Daugirdas’ Kt/V is similar to Shinzato’s Kt/V. $\displaystyle \mathrm{Kt/V} = - LN( R - 0.08 \times t ) + \left[ 4 - \left( 3.5 \times R \right) \right] \times\frac{\mathrm{UF}}{\mathrm{W}}\\ = - LN \left( \frac{\mathrm{postBUN}}{\mathrm{preBUN}} - 0.008 \times t \right) + \left[ 4 - \left( 3.5 \times \frac{\mathrm{postBUN}}{\mathrm{preBUN}} \right) \right] \times \frac{\mathrm{preWeight} - \mathrm{postWeight}}{\mathrm{postWeight}} \cdots(1)$ $\displaystyle \mathrm{Gw} = \mathrm{G}\cdot\mathrm{Tw} = \mathrm{Kd}\int_{0}^{Td}C_1dt + \mathrm{Kd}\int_{0}^{Td}C_2dt + \mathrm{Kd}\int_{0}^{Td}C_3dt \cdots(2)$ $\displaystyle \mathrm{Ce} = \mathrm{Cs} Exp\left( - \frac{\mathrm{Kt}}{\mathrm{V}} \right) + \frac{\mathrm{G}}{\mathrm{K}}\left[ 1 - Exp\left( - \frac{\mathrm{Kt}}{\mathrm{V}} \right) \right] \cdots(3)$

Execute the procedure as following;

CREATE FUNCTION Function_KtV
(		@preBUN DEC(4, 1)
,	@postBUN DEC(4, 1)
,	@preWeight	DEC(4, 1)
,	@postWeight DEC(4, 1)
,	@DialysisDuration	int
)
RETURNS DEC(3,2)
AS
BEGIN
DECLARE	@KtV DEC(3, 2)
SELECT	@KtV = - LOG(@postBUN / @preBUN - 0.008 * @DialysisDuration / 60)
+ (4 - (3.5 * @postBUN / @preBUN))
* ((@preWeight - @postWeight) / @postWeight)
RETURN	@KtV
END
GO


References: JSDT 29 (12): 1511-1516, 1996

Second Generation Logarithmic Estimates of Single-Pool Variable Volume

## SQL Serverのスカラー値関数としてKt/Vを定義するには

透析患者の透析効率を計算するには日本透析医学会の推奨する新里式があります．原著論文を見ると積分方程式を解く必要があり，一般的な数学の知識では歯が立ちません．ここでは Daugirdas による Kt/V をデータベース内で計算する方法を述べます．新里自身も Daugirdas による Kt/V と新里式による Kt/V とはかなり一致していると述べており，ほぼ代用可能ではないかと思われます．(1) が Daugirdas の方法で (2) と (3) を解くと新里式になります． $\displaystyle \mathrm{Kt/V} = - LN( R - 0.008 \times t ) + \left[ 4 - \left( 3.5 \times R \right) \right] \times\frac{\mathrm{UF}}{\mathrm{W}}\\ = - LN \left( \frac{\mathrm{postBUN}}{\mathrm{preBUN}} - 0.008 \times t \right) + \left[ 4 - \left( 3.5 \times \frac{\mathrm{postBUN}}{\mathrm{preBUN}} \right) \right] \times \frac{\mathrm{preWeight} - \mathrm{postWeight}}{\mathrm{postWeight}} \cdots(1)$ $\displaystyle \mathrm{Gw} = \mathrm{G}\cdot\mathrm{Tw} = \mathrm{Kd}\int_{0}^{Td}C_1dt + \mathrm{Kd}\int_{0}^{Td}C_2dt + \mathrm{Kd}\int_{0}^{Td}C_3dt \cdots(2)$ $\displaystyle \mathrm{Ce} = \mathrm{Cs} Exp\left( - \frac{\mathrm{Kt}}{\mathrm{V}} \right) + \frac{\mathrm{G}}{\mathrm{K}}\left[ 1 - Exp\left( - \frac{\mathrm{Kt}}{\mathrm{V}} \right) \right] \cdots(3)$

下記プロシージャを実行して関数を作成します．

CREATE FUNCTION Function_KtV
(		@preBUN DEC(4, 1)
,	@postBUN DEC(4, 1)
,	@preWeight	DEC(4, 1)
,	@postWeight DEC(4, 1)
,	@DialysisDuration	int
)
RETURNS DEC(3,2)
AS
BEGIN
DECLARE	@KtV DEC(3, 2)
SELECT	@KtV = - LOG(@postBUN / @preBUN - 0.008 * @DialysisDuration / 60)
+ (4 - (3.5 * @postBUN / @preBUN))
* ((@preWeight - @postWeight) / @postWeight)
RETURN	@KtV
END
GO


Second Generation Logarithmic Estimates of Single-Pool Variable Volume