0

我有大约 52,000 条记录的 tblDump。它是具有相应给药时间的 ID 列表。相关变量为SubjectNumber, RECDATE, RECTIME, FoodType。每位患者每天服药两次,因此有 aMin(RECTIME)Max(RECTIME)per RECDATE。为了显示这个,我到目前为止有这个:

 SELECT tblDump.SubjectNumber, tblDump.RECDATE, 
        Min(RECTIME) As MornDose, Max(RECTIME) As EveDose
 FROM tblDump
 GROUP BY tblDump.SubjectNumber, tblDump.RECDATE
 ORDER BY tblDump.SubjectNumber, tblDump.RECDATE Asc;

对于变量FoodType,有三种可能性:1、2 或 3。我需要每天打印Min(RECTIME)与 1相关联且与 3 相关联的主题。Max(RECTIME)

我试图做出HAVING这样的陈述Foodtype = 1forMin(RecTIME)Foodtype = 3for Max(RECTIME),但我无法让语言学正确。

解决:

好的,我确信有一种更有效的方法可以做到这一点,但这很有效。希望这将有助于某人或某人可以发布更有效的方法。我使用了 3 个查询,一个用于最小剂量时间,一个用于最大剂量时间,一个用于将它们连接在一起。

qryMin:

SELECT a.SubjectNumber, a.RECDATE, First(a.colm AS MornDose), First(m.FoodType)
FROM (SELECT SubjectNumber, RECDATE, MIN(RECTIME) as colm 
      FROM tblDump GROUP BY SubjectNumber, RECDATE)  AS a 
INNER JOIN tblDump AS m 
        ON (a.colm=m.RECTIME) 
           AND (a.RECDATE = m.RECDATE) 
           AND (a.SubjectNumber = m.SubjectNumber)
GROUP BY a.SubjectNumber, a.RECDATE
ORDER BY a.SubjectNumber, a.RECDATE;

qryMax:

SELECT a.SubjectNumber, a.RECDATE, First(a.colm) AS MornDose, First(m.FoodType)
FROM (SELECT SubjectNumber, RECDATE, MAX(RECTIME) AS colm 
      FROM tblDump GROUP BY SubjectNumber, RECDATE)  AS a 
INNER JOIN tblDump AS m 
        ON (a.SubjectNumber = m.SubjectNumber) 
           AND (a.RECDATE = m.RECDATE) 
           AND (a.colm = m.RECTIME)
GROUP BY a.SubjectNumber, a.RECDATE
ORDER BY a.SubjectNumber, a.RECDATE;

qryCombo:

SELECT qryMin.*, qryMax.*
FROM qryMin
INNER JOIN qryMax 
        ON qryMin.SubjectNumber = qryMax.SubjectNumber
WHERE qryMin.RECDATE = qryMax.RECDATE 
      AND qryMin.FoodType = 1 
      AND qryMax.FoodType = 3;
4

0 回答 0