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;

Reference: Simplified nutritional screening tools for patients on maintenance hemodialysis

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

 高齢者における低栄養は死亡のリスクを高めることが知られています.Geriatric Nutrition Risk Index (GNRI) は低栄養を簡易に検出できるツールです.単に高齢者だけではなく,維持透析患者においても有用です.その定義は以下です.

\displaystyle \mathrm{GNRI} = 14.89\times\mathrm{Albumin (g/dL)} + 41.7\times\frac{\mathrm{Body Weight}}{\mathrm{Ideal Body Weight}}

ここで理想体重 (kg) は身長 (m) の二乗に 22 を乗じて得られます.ただし実体重が理想体重を上回る場合には,実体重を理想体重に置き換えます.つまり第 2 項の分数は 1 に等しくなります.

 データベースのテーブル定義は以下の通りであるとします.日本透析医学会統計調査票の 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)

 上記テーブルの中で必要な項目は身長,体重,アルブミン値です.下記プロシージャを実行して関数を作成します.実体重が理想体重を上回る場合には実体重を理想体重に置き換えるという条件は CASE 式の中で評価します.

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

 下記のクエリを実行して GNRI を求めます.CASE 式の中身は透析後体重が空欄の場合は透析前体重で代用するという意味です.

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;

参照:維持透析患者のための簡易栄養スクリーニングツール