我正在尝试编写一个脚本,该脚本将返回一个独特的文档 - 医生 - 患者三联体的最新值。我需要脚本的行为类似于group by语句,除了group by一次只能处理一列。我需要仅针对最新的独特三元组的日期和状态信息。请告诉我你需要从我这里得到什么帮助。这是当前非常简单的声明:
SELECT
TransmissionSend.CreateTimestamp,
TransmissionSendItem.Status,
TransmissionSendItem.PhysicianId,
TransmissionSendItem.DocumentIdDisplay,
Utility.SqlFunctions_NdnListToAccountList(TransmissionSendItem.NdocNum) AS AccountNum
FROM
Interface_SFAX.TransmissionSend,
Interface_SFAX.TransmissionSendItem
WHERE
TransmissionSend.ID = TransmissionSendItem.childsub --I don't know exactly what this does, I did not write this script. It must stay here though for the exact results.
ORDER BY TransmissionSend.CreateTimestamp DESC -- In the end, each latest result of the unique triplet will be ordered from most recent to oldest in return
我的问题是,如何才能将结果限制为每个医生ID,文档ID和帐号组合的最新状态?
答案 0 :(得分:4)
首先使用文档化的GROUP BY documentid选择MAX(日期),然后通过第一个选择结果从表中选择所有数据,例如使用内部联接。
SELECT table.additionalData, J.id, J.date
FROM table
INNER JOIN (SELECT id, MAX(date) AS date
FROM table GROUP BY id) AS J
ON J.id = table.id
AND J.date /* this is the max date */ = table.date