Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 根据身高/体重表计算BMI_Sql_Sql Server - Fatal编程技术网

Sql 根据身高/体重表计算BMI

Sql 根据身高/体重表计算BMI,sql,sql-server,Sql,Sql Server,澄清 在仔细阅读你的答案和你对这个问题的解释后,我有以下补充 我需要生成整个BMI历史记录,而不是单个值。 如果可能,两个表中的每个值都需要与另一个表中的相关值配对。 简单问题 给定PatientEight中的一个条目,计算BMI体重指数,该指数包含所有条目,这些条目的输入日期在当前PatientEight输入日期和前一个PatientEight输入日期之间。这是正确的,除非PatientWight中的EntryDates大于PatientWight中的任何EntryDates。在这种情况下,使

澄清

在仔细阅读你的答案和你对这个问题的解释后,我有以下补充

我需要生成整个BMI历史记录,而不是单个值。 如果可能,两个表中的每个值都需要与另一个表中的相关值配对。 简单问题

给定PatientEight中的一个条目,计算BMI体重指数,该指数包含所有条目,这些条目的输入日期在当前PatientEight输入日期和前一个PatientEight输入日期之间。这是正确的,除非PatientWight中的EntryDates大于PatientWight中的任何EntryDates。在这种情况下,使用最新的PatientTheRight条目计算BMI

对于PatientWight中的每个条目,使用PatientWight中所有相应的值计算BMI体重指数

一些逻辑:


PatientLight的EntryDate是类似于以下内容的内容应该可以实现未经测试的技巧

SELECT P.PaitenId
,      W.EntryDate
,      P.Inches
,      W.Pounds
FROM (
  SELECT p.PatientId
  ,      p.EntryDate AS EntryDate
  ,      MIN(p2.EntryDate) as NextEntryDate
  FROM PatientHeight p
  LEFT JOIN PatientHeight p2
  ON p.PatientID = p2.PatientID
  AND p2.EntryDate > p.EntryDate
  GROUP BY p.PatientId
  , p.EntryDate
) P
JOIN PaitentWeight W
ON P.PatientId = W.PatientId
AND W.EntryDate BETWEEN P.EntryDate AND P.NextEntryDate
像这样的

select
      curr.personid, curr.entrydate, wgt.entrydate WeightDate,
      dbo.CalcBMI(curr.Inches, wgt.Pounds) as BMI
from
     (Select top 1 * from PatientHeight
      where personid= @personid
      order by entrydate desc) curr
outer apply
     (select top 1 * from PatientHeight
      where personid= curr.personid
        and entrydate < curr.entrydate
      order by entrydate desc) prev
join
      PatientWeight wgt
  on (wgt.entrydate > prev.entrydate or prev.entrydate is null)
      and wgt.personid = curr.personid
我对这个问题的解读表明,只需要显示当前数据,当前数据是

PatientWight中的所有条目,其EntryDate介于当前PatientWight EntryDate和上一个PatientWight EntryDate之间


您假设重量比重量多heights@Richard又名赛博奇维,我自己也注意到了这一点,正在调查@基督,这是一个伟大的开始@Richard又名cyberkiwi是真的,但我的假设是基于问题中显示的表格。我认为这一假设在现实生活中也适用,因为体重的波动远大于身高。有点离题:为什么要使用两张桌子?这是一个家庭作业问题吗?@moonman239-这是一个要求。最初的设计由一张桌子组成,但用例显示,随着人们倾向于停止生长,体重将比身高更频繁地被测量。从那里决定两张桌子更好。即使我们使用一个表[weight | height],我们仍然需要解决相同的问题并插值任何空高度。此外,当我们需要区分生成的和用户输入的值时,我们不能自动填充这些值,特别是当在多个权重项的中间添加/删除高度时。 9783 | 01/01/2011 | 75in | 178lbs 9783 | 01/01/2010 | 75in | 175lbs 9783 | 12/01/2010 | 75in | 174lbs 9783 | 11/01/2010 | 75in | 178lbs 9783 | 01/01/2009 | 74in | 174lbs 9783 | 12/01/2009 | 74in | 174lbs 9783 | 11/01/2009 | 74in | 178lbs
Insert Into @PatientWeightRet
    Select 
        *
    From
    (
        Select
            TransactionID, 
            EncounterID, 
            EntryDate,
            ISNULL(CONVERT(NUMERIC(18,2),dbo.fnBmi(Inches, Pounds)), -1) AS BMI
        From
        (
            Select Distinct
                W.TransactionID,
                W.PatientID, 
                W.EntryDate,
                W.EncounterID,
                W.Pounds,
                ( -- For Every Weight
                    Select Top 1 --Get the first Entry
                        H.Inches
                    From
                        @PatientHeight AS H -- From Patient Height 
                    Where 
                        H.EntryDate <=  W.EntryDate-- Who's Date is less than or equal to the Weight Date
                        AND W.EntryDate >  -- and the Weight Date is greater than (the previous height date)
                        (
                            ISNULL
                            (
                                (
                                    Select Top 1 -- the first 
                                        EntryDate -- date
                                    From
                                        @PatientHeight -- from patientHeight
                                    Where
                                        EntryDate < H.EntryDate -- who's entry date is less than the current height date
                                    Order BY EntryDate Desc, TransactionID DESC
                                )
                            , '01/01/1800') -- if we're at the bottom, return really old date
                        )
                    Order By H.EntryDate Desc, H.TransactionID DESC
                ) AS Inches
            From
                PatientWeight AS W
            Where 
                PatientID = @PatientID 
                AND Active = 1
        ) tmp
    ) tmp2
    Where
        BMI != -1
    Order By EntryDate DESC, TransactionID DESC
SELECT W.PersonID,
       W.EntryDate,
       (
           SELECT TOP 1 H.Inches
               FROM PatientHeight AS H
               WHERE W.PersonID = H.PersonId
                   AND H.EntryDate <= W.EntryDate
               ORDER BY H.EntryDate DESC
       ) AS Inches
       W.Pounds
    FROM PatientWeight AS W
SELECT P.PaitenId
,      W.EntryDate
,      P.Inches
,      W.Pounds
FROM (
  SELECT p.PatientId
  ,      p.EntryDate AS EntryDate
  ,      MIN(p2.EntryDate) as NextEntryDate
  FROM PatientHeight p
  LEFT JOIN PatientHeight p2
  ON p.PatientID = p2.PatientID
  AND p2.EntryDate > p.EntryDate
  GROUP BY p.PatientId
  , p.EntryDate
) P
JOIN PaitentWeight W
ON P.PatientId = W.PatientId
AND W.EntryDate BETWEEN P.EntryDate AND P.NextEntryDate
SELECT
  w.PersonID,
  w.EntryDate,
  Inches = MIN(h.Inches)
  w.Pounds
FROM PatientWeight w
  LEFT JOIN PatientHeight h
    ON w.PersonID = h.PersonID AND w.EntryDate >= h.EntryDate
select
      curr.personid, curr.entrydate, wgt.entrydate WeightDate,
      dbo.CalcBMI(curr.Inches, wgt.Pounds) as BMI
from
     (Select top 1 * from PatientHeight
      where personid= @personid
      order by entrydate desc) curr
outer apply
     (select top 1 * from PatientHeight
      where personid= curr.personid
        and entrydate < curr.entrydate
      order by entrydate desc) prev
join
      PatientWeight wgt
  on (wgt.entrydate > prev.entrydate or prev.entrydate is null)
      and wgt.personid = curr.personid