## 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

## 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

## How to create scalar function of SQL Server in order to calculate GNRI?

Malnutrition in elder people increases the risk of death. Geriatric Nutrition Risk Index (GNRI) is the tool to detect malnutrition easily in hemodialysis patients, too. The definition is as following; $\displaystyle \mathrm{GNRI} = 14.89\times\mathrm{Albumin (g/dL)} + 41.7\times\frac{\mathrm{Body Weight}}{\mathrm{Ideal Body Weight}}$

where ideal body weight is given by multiplying 22 the square of height. But body weight should be replaced with ideal body weight if body weight is greater than ideal body weight. Then the second term is equal to 1.

Table is defined as following procedure. It is based on the survey list of the Japanese Society for Dialysis Therapy in 2013.

CREATE TABLE dbo.T_JSDT(
ID nchar(8) NOT NULL,
DATE_Survey date NOT NULL,
Diabetes nchar(1) NOT NULL,
Myocardial_Infarction nchar(1) NOT NULL,
Cerebral_Hemorrhage nchar(1) NOT NULL,
Cerebral_Infarction nchar(1) NOT NULL,
Amputation nchar(1) NOT NULL,
Femoral_Fracture nchar(1) NOT NULL,
EPS nchar(1) NOT NULL,
Hypertensive_Agents nchar(1) NOT NULL,
Smoke nchar(1) NOT NULL,
Therapy_Mode nchar(10) NOT NULL,
Combination_PD nchar(1) NOT NULL,
History_PD nchar(1) NOT NULL,
Transplantation_COUNT nchar(1) NOT NULL,
DIalysis_COUNT int NULL,
Dialysis_Duration int NULL,
QB int NULL,
Height decimal(4, 1) NOT NULL,
preWeight decimal(4, 1) NOT NULL,
postWeight decimal(4, 1) NULL,
preBUN decimal(4, 1) NOT NULL,
postBUN decimal(4, 1) NULL,
preCre decimal(5, 2) NOT NULL,
postCre decimal(5, 2) NULL,
Albumin decimal(3, 1) NOT NULL,
CRP decimal(4, 2) NOT NULL,
Ca decimal(3, 1) NOT NULL,
IP decimal(3, 1) NOT NULL,
Hemoglobin decimal(3, 1) NOT NULL,
TIBC decimal(3, 0) NULL,
Fe decimal(3, 0) NULL,
Ferritin decimal(5, 1) NULL,
TCHO decimal(3, 0) NOT NULL,
HDLC decimal(3, 0) NOT NULL,
PTH_mode nchar(1) NOT NULL,
PTH decimal(4, 0) NULL,
HbA1c decimal(3, 1) NULL,
GA decimal(3, 1) NULL,
SBP decimal(3, 0) NOT NULL,
DBP decimal(3, 0) NOT NULL,
HR decimal(3, 0) NOT NULL,
KtV_JSDT decimal(3, 2) NULL,
nPCR_JSDT decimal(3, 2) NULL,
[%CRE] decimal(4, 1) NULL,
CONSTRAINT PK_T_JSDT PRIMARY KEY (ID, DATE_Survey)

We need height, body weight and albumin in the table. Execute following procedure to create function.

CREATE FUNCTION Function_GNRI
(@Albumin dec(3, 1), @Height dec(4, 1), @Weight dec(4, 1))
RETURNS DEC(5, 2)
AS
BEGIN
DECLARE @GNRI DEC(5,2)
SELECT @GNRI = 14.89 * @Albumin + 41.7 * CASE WHEN @Weight > ( 22 * POWER(@Height/100, 2)) THEN ( 22 * POWER(@Height/100, 2)) ELSE @Weight END / ( 22 * POWER(@Height/100, 2))
RETURN @GNRI
END


Execute following query to calculate GNRI.

WITH	CTE	AS
(SELECT	J.ID		AS ID
,	J.DATE_Survey	AS DATE_Survey
,	J.Albumin	AS ALB
,	J.Height	AS Height
,	CASE WHEN J.postWeight IS NULL THEN J.preWeight ELSE J.postWeight END	AS Weight
FROM	dbo.T_JSDT AS J
),	CTE_GNRI AS
(SELECT	CTE.ID		AS ID
,	CTE.DATE_Survey	AS DATE_Survey
,	dbo.Function_GNRI(CTE.ALB, CTE.Height, CTE.Weight)	AS GNRI
FROM	CTE)
SELECT	*	FROM	CTE_GNRI;