﻿{"id":5824,"date":"2014-07-18T06:05:26","date_gmt":"2014-07-17T21:05:26","guid":{"rendered":"http:\/\/fujiitoshiki.com\/improvesociety\/?p=5824"},"modified":"2017-04-27T16:53:45","modified_gmt":"2017-04-27T07:53:45","slug":"how-to-create-scalar-function-of-sql-server-in-order-to-calculate-gnri","status":"publish","type":"post","link":"https:\/\/www.fujiitoshiki.com\/improvesociety\/?p=5824","title":{"rendered":"How to create scalar function of SQL Server in order to calculate GNRI?"},"content":{"rendered":"<div class=\"theContentWrap-ccc\"><p>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; <\/p>\n<p><img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cdisplaystyle+%5Cmathrm%7BGNRI%7D+%3D+14.89%5Ctimes%5Cmathrm%7BAlbumin+%28g%2FdL%29%7D+%2B+41.7%5Ctimes%5Cfrac%7B%5Cmathrm%7BBody+Weight%7D%7D%7B%5Cmathrm%7BIdeal+Body+Weight%7D%7D&#038;bg=T&#038;fg=000000&#038;s=0' alt='\\displaystyle \\mathrm{GNRI} = 14.89\\times\\mathrm{Albumin (g\/dL)} + 41.7\\times\\frac{\\mathrm{Body Weight}}{\\mathrm{Ideal Body Weight}}' title='\\displaystyle \\mathrm{GNRI} = 14.89\\times\\mathrm{Albumin (g\/dL)} + 41.7\\times\\frac{\\mathrm{Body Weight}}{\\mathrm{Ideal Body Weight}}' class='latex' \/><\/p>\n<p>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. <\/p>\n<p>Table is defined as following procedure. It is based on the survey list of the Japanese Society for Dialysis Therapy in 2013. <\/p>\n<pre class=\"lang:tsql decode:true \" >\r\nCREATE TABLE dbo.T_JSDT(\r\n\tID nchar(8) NOT NULL,\r\n\tDATE_Survey date NOT NULL,\r\n\tDiabetes nchar(1) NOT NULL,\r\n\tMyocardial_Infarction nchar(1) NOT NULL,\r\n\tCerebral_Hemorrhage nchar(1) NOT NULL,\r\n\tCerebral_Infarction nchar(1) NOT NULL,\r\n\tAmputation nchar(1) NOT NULL,\r\n\tFemoral_Fracture nchar(1) NOT NULL,\r\n\tEPS nchar(1) NOT NULL,\r\n\tHypertensive_Agents nchar(1) NOT NULL,\r\n\tSmoke nchar(1) NOT NULL,\r\n\tTherapy_Mode nchar(10) NOT NULL,\r\n\tCombination_PD nchar(1) NOT NULL,\r\n\tHistory_PD nchar(1) NOT NULL,\r\n\tTransplantation_COUNT nchar(1) NOT NULL,\r\n\tDIalysis_COUNT int NULL,\r\n\tDialysis_Duration int NULL,\r\n\tQB int NULL,\r\n\tHeight decimal(4, 1) NOT NULL,\r\n\tpreWeight decimal(4, 1) NOT NULL,\r\n\tpostWeight decimal(4, 1) NULL,\r\n\tpreBUN decimal(4, 1) NOT NULL,\r\n\tpostBUN decimal(4, 1) NULL,\r\n\tpreCre decimal(5, 2) NOT NULL,\r\n\tpostCre decimal(5, 2) NULL,\r\n\tAlbumin decimal(3, 1) NOT NULL,\r\n\tCRP decimal(4, 2) NOT NULL,\r\n\tCa decimal(3, 1) NOT NULL,\r\n\tIP decimal(3, 1) NOT NULL,\r\n\tHemoglobin decimal(3, 1) NOT NULL,\r\n\tTIBC decimal(3, 0) NULL,\r\n\tFe decimal(3, 0) NULL,\r\n\tFerritin decimal(5, 1) NULL,\r\n\tTCHO decimal(3, 0) NOT NULL,\r\n\tHDLC decimal(3, 0) NOT NULL,\r\n\tPTH_mode nchar(1) NOT NULL,\r\n\tPTH decimal(4, 0) NULL,\r\n\tHbA1c decimal(3, 1) NULL,\r\n\tGA decimal(3, 1) NULL,\r\n\tSBP decimal(3, 0) NOT NULL,\r\n\tDBP decimal(3, 0) NOT NULL,\r\n\tHR decimal(3, 0) NOT NULL,\r\n\tKtV_JSDT decimal(3, 2) NULL,\r\n\tnPCR_JSDT decimal(3, 2) NULL,\r\n\t[%CRE] decimal(4, 1) NULL,\r\n CONSTRAINT PK_T_JSDT PRIMARY KEY (ID, DATE_Survey)<\/pre>\n<p>We need height, body weight and albumin in the table. Execute following procedure to create function. <\/p>\n<pre class=\"lang:tsql decode:true \" >\r\nCREATE FUNCTION Function_GNRI \r\n(@Albumin dec(3, 1), @Height dec(4, 1), @Weight dec(4, 1))\r\nRETURNS DEC(5, 2)\r\nAS\r\nBEGIN\r\n\tDECLARE @GNRI DEC(5,2)\r\n\tSELECT @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))\r\n\tRETURN @GNRI\r\nEND\r\n<\/pre>\n<p>Execute following query to calculate GNRI. <\/p>\n<pre class=\"lang:tsql decode:true \" >\r\nWITH\tCTE\tAS\r\n(SELECT\tJ.ID\t\tAS ID\r\n\t,\tJ.DATE_Survey\tAS DATE_Survey\r\n\t,\tJ.Albumin\tAS ALB\r\n\t,\tJ.Height\tAS Height\r\n\t,\tCASE WHEN J.postWeight IS NULL THEN J.preWeight ELSE J.postWeight END\tAS Weight\r\n   FROM\tdbo.T_JSDT AS J\r\n),\tCTE_GNRI AS\r\n(SELECT\tCTE.ID\t\tAS ID\r\n\t,\tCTE.DATE_Survey\tAS DATE_Survey\r\n\t,\tdbo.Function_GNRI(CTE.ALB, CTE.Height, CTE.Weight)\tAS GNRI\r\nFROM\tCTE)\r\nSELECT\t*\tFROM\tCTE_GNRI;\r\n<\/pre>\n<p>Reference: <a href=\"\/\/fujiitoshiki.com\/improvesociety\/?p=97\" target=\"_blank\">Simplified nutritional screening tools for patients on maintenance hemodialysis<\/a><\/p>\n<p><iframe style=\"width:120px;height:240px;\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" frameborder=\"0\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&#038;OneJS=1&#038;Operation=GetAdHtml&#038;MarketPlace=US&#038;source=ss&#038;ref=ss_til&#038;ad_type=product_link&#038;tracking_id=improsocie-20&#038;marketplace=amazon&#038;region=US&#038;placement=0123820227&#038;asins=0123820227&#038;linkId=LXJBROSN54NZGFQG&#038;show_border=true&#038;link_opens_in_new_window=true\"><br \/>\n<\/iframe><iframe style=\"width:120px;height:240px;\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" frameborder=\"0\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&#038;OneJS=1&#038;Operation=GetAdHtml&#038;MarketPlace=US&#038;source=ss&#038;ref=ss_til&#038;ad_type=product_link&#038;tracking_id=improsocie-20&#038;marketplace=amazon&#038;region=US&#038;placement=B00JI50L0M&#038;asins=B00JI50L0M&#038;linkId=TBXDLJXWCL2FZME3&#038;show_border=true&#038;link_opens_in_new_window=true\"><br \/>\n<\/iframe><\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Malnutrition in elder people increases the risk of death. Geriatric Nutrition Risk Index (GNRI) is the tool to &hellip; <a href=\"https:\/\/www.fujiitoshiki.com\/improvesociety\/?p=5824\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;How to create scalar function of SQL Server in order to calculate GNRI?&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":6024,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1,11,4],"tags":[145,137,136,128],"class_list":["post-5824","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-database","category-healthcare","category-nutrition","tag-gnri","tag-scalar-function","tag-sql-server","tag-the-japanese-society-for-dialysis-therapy"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=\/wp\/v2\/posts\/5824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5824"}],"version-history":[{"count":5,"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=\/wp\/v2\/posts\/5824\/revisions"}],"predecessor-version":[{"id":7778,"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=\/wp\/v2\/posts\/5824\/revisions\/7778"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=\/wp\/v2\/media\/6024"}],"wp:attachment":[{"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}