SQL - SELECT子查询AS BIT值用于EXIST检查

时间:2014-10-09 14:49:30

标签: sql reporting-services shift timetable

我有问题。

我正试图获取一个BIT值来检查一个人是否在昨晚10点到午夜之间进入了大楼。当我单独运行子查询代码时,它会给我我需要的结果。当我使用SSRS2008时,我需要将所有结果放在同一个存储过程中。

所以问题是,它给了我一些正确的位值,对于明显错误的那些,它给出了错误,对于明显为真的那些,它给出了真实。但对于那些中间的人(白班,谁在23岁离开),它给结果有点随机..

有没有人有线索?

SELECT DISTINCT TOP 200 
   Events.LoggedTime, 
   PTUsers.Name, 
   PTDoors.PTDoorsID, 
   PTUsers.AccessLevel, 
   CAST(CASE 
           WHEN EXISTS (SELECT Events.LoggedTime 
                        FROM Events 
                        INNER JOIN PTUsers AS PTUsers_1 ON Events.GlobalIndex1 = PTUsers.GlobalRecord 
                        INNER JOIN PTDoors AS PTDoors_1 ON Events.RecordIndex2 + 1 = PTDoors.Address
                        WHERE (DATEPART(day, Events.LoggedTime) = DATEPART(day, GETDATE() - 1)) 
                          AND (DATEPART(hour, Events.LoggedTime) IN (22, 23)) 
                          AND (PTDoors_1.PTDoorsID = 14)) THEN 1 ELSE 0 END AS BIT) AS Night
FROM         
    Events 
INNER JOIN
    PTUsers ON Events.GlobalIndex1 = PTUsers.GlobalRecord 
INNER JOIN
    PTDoors ON Events.RecordIndex2 + 1 = PTDoors.Address
WHERE     
    (PTUsers.Panel = 0) 
    AND (PTDoors.Panel = 0) 
    AND (PTDoors.PTDoorsID = 14) 
    AND (DATEPART(day, Events.LoggedTime) = DATEPART(day, GETDATE()) - 1) 
    AND (PTUsers.AccessLevel IN (3))
ORDER BY 
    Events.LoggedTime DESC

2 个答案:

答案 0 :(得分:0)

我认为你不需要CAST,因为你明确地将Night定义为BIT通过将结果设置为EXISTS(),这有点。我删除了不正确的查询。

我看到了你的问题。您没有在子查询中为连接约束使用正确的表别名。

应该是:

INNER JOIN PTUsers AS PTUsers_1 ON Events.GlobalIndex1 = PTUsers_1.GlobalRecord 
INNER JOIN PTDoors AS PTDoors_1 ON Events.RecordIndex2 + 1 = PTDoors_1.Address

另外,请检查并确保在联接中使用正确的值。我会更改以下内容进行测试。

FROM Events Events_2
INNER JOIN PTUsers AS PTUsers_1 ON Events_2.GlobalIndex1 = PTUsers_1.GlobalRecord 
INNER JOIN PTDoors AS PTDoors_1 ON Events_2.RecordIndex2 + 1 = PTDoors_1.Address

答案 1 :(得分:0)

@lrd我做了你建议的更正, 谢谢你指出表别名:)

我删除了演员,所以现在我得到了BIT专栏。但现在的问题是,我得到了所有" 1"" s作为结果。 令我感到困惑的是,子查询的工作原理应该是对它自己的查询。它可以追溯到一天,并在给定的时间范围内显示该门上的条目。

现在我试图比较同一个人的信息,这意味着 - 我在列表中看到一个人,昨天早上6点到达,检查那个人是否也在前一天到达22和22之间。午夜并返回一个值来显示。

SELECT DISTINCT TOP 200 Events.LoggedTime, PTUsers.Name, PTDoors.PTDoorsID, PTUsers.AccessLevel, CASE WHEN EXISTS
                          (SELECT     Events.LoggedTime
                            FROM          Events INNER JOIN
                                                   PTUsers AS PTUsers_1 ON Events.GlobalIndex1 = PTUsers_1.GlobalRecord INNER JOIN
                                                   PTDoors AS PTDoors_1 ON Events.RecordIndex2 + 1 = PTDoors_1.Address
                            WHERE      (DATEPART(day, Events.LoggedTime) = DATEPART(day, GETDATE() - 1)) AND (DATEPART(hour, Events.LoggedTime) IN (22, 23)) AND 
                                                   (PTDoors_1.PTDoorsID = 14)) THEN 1 ELSE 0 END AS BIT
FROM         Events INNER JOIN
                      PTUsers ON Events.GlobalIndex1 = PTUsers.GlobalRecord INNER JOIN
                      PTDoors ON Events.RecordIndex2 + 1 = PTDoors.Address
WHERE     (PTUsers.Panel = 0) AND (PTDoors.Panel = 0) AND (PTDoors.PTDoorsID = 14) AND (DATEPART(day, Events.LoggedTime) = DATEPART(day, GETDATE()) 
                      - 1) AND (PTUsers.AccessLevel IN (3))
ORDER BY Events.LoggedTime DESC