0

这是表结构(相关位):

基本表结构

我正在尝试查找与 clientID 上的声明和声明日期(实际上在 8 小时内)相匹配的 contactLog 条目。这是我想出的:

SELECT
D.ClientID
D.DeclarationID   
CL.ContactDescription,
CL.ContactDate,

FROM Declarations D

INNER JOIN  (Contacts C
         INNER JOIN (PartialContacts PC
             INNER JOIN ContactLogs CL
                ON PC.ContactPartID = CL.ContactPartID )
            on PC.ContactID = C.ContactID
     )  
    ON C.ClientID = D.ClientID AND DATEDIFF(hour, D.DeclarationDate, CL.ContactDate) < 8

但是,连接无法正常运行,因为我得到了 CL.ContactDate 的不匹配日期,很明显它只是返回 CL 中该患者 ID 的每一行。我想...我不太确定出了什么问题。

在此处输入图像描述

4

2 回答 2

0

感谢达米安:

我没有指定 datediff 的下限,所以匹配没有正常工作。显然,连接很好:

SELECT
D.ClientID
D.DeclarationID   
CL.ContactDescription,
CL.ContactDate,

FROM Declarations D

INNER JOIN  (Contacts C
         INNER JOIN (PartialContacts PC
             INNER JOIN ContactLogs CL
                ON PC.ContactPartID = CL.ContactPartID )
            on PC.ContactID = C.ContactID
     )  
    ON C.ClientID = D.ClientID AND DATEDIFF(hour, D.DeclarationDate, CL.ContactDate) < 8 DATEDIFF(hour, D.DeclarationDate, CL.ContactDate) > 0.1
于 2012-04-25T08:19:26.330 回答
0
SELECT
D.ClientID
D.DeclarationID   
CL.ContactDescription,
CL.ContactDate
FROM Declarations D
INNER JOIN Clients CL ON D.ClientID=CL.ClientID
INNER JOIN  Contacts CO ON CL.ClientID=CO.ClientID
INNER JOIN PartialContact PC ON PC.ContactID=CO.ContactID
INNER JOIN ContactLog CL ON CL.ContactPartID=PC.ContactPartID
WHERE DATEDIFF(hour, D.DeclarationDate, CL.ContactDate) BETWEEN 0 AND 8
于 2012-04-25T08:20:13.947 回答