简体   繁体   English

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

[英]SQL - SELECT subquery AS BIT value for EXIST check

I have a problem. 我有个问题。

I'm trying to get a BIT value to check whether a person has entered the building last night between 10pm to midnight. 我正在尝试获取BIT值,以检查昨晚晚上10点至午夜之间是否有人进入建筑物。 When I run the subquery code by itself, it gives me the results I need. 当我自己运行子查询代码时,它将为我提供所需的结果。 As I'm working with SSRS2008 I need for all the results to be in the same stored procedure. 当我使用SSRS2008时,我需要所有结果都放在同一存储过程中。

So the problem is, it gives me the bit values somewhat right, for the ones that are obviously false, it gives false, for the ones that are obviously true, it gives true. 所以问题是,它给了我一些正确的位值,对于那些显然是假的,它给出了假,对于那些显然是真的,它给出了真。 But for the ones in the middle (the day shift, who leave at 23) it gives the results somewhat random.. 但是对于中间的那些人(白昼轮班,他们在23点离开),其结果有些随机。

Does anyone have a clue? 有人有线索吗?

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

I don't think you need a CAST because you are explicitly defining Night as a BIT By setting the result to EXISTS(), which is a bit. 我认为您不需要CAST,因为您通过将结果设置为EXISTS()来将Night明确定义为BIT,这有点。 I removed the query that was incorrect. 我删除了不正确的查询。

I see your problem. 我明白你的问题。 You are not using the correct table alias for your join constraint in your subquery. 您没有为子查询中的联接约束使用正确的表别名。

It should be: 它应该是:

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

Also,check and make sure you are using the correct values in your join. 另外,检查并确保在联接中使用正确的值。 I would change the following for a test. 我将更改以下内容进行测试。

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

@lrd i did the corrections you suggested, thanks for pointing out the table aliases :) @lrd我做了您建议的更正,感谢您指出表别名:)

i removed the cast, so now i get the BIT column. 我删除了演员表,所以现在我得到了BIT栏。 but now the problem is that i get all "1"'s as results. 但现在的问题是,我得到的结果全为“ 1”。 What baffles me is the that subquery works as it should as a query on it's own. 让我感到困惑的是,子查询本身就可以作为查询工作。 it goes back a day, and displays the entries on that door in the given timeframe. 它可以追溯到一天,并在给定的时间范围内显示该门上的条目。

now i'm trying to compare that information for the same person, meaning - i see a person in the list, arriving yesterday at 6am, check if that person also arrived the day before that between 22 & midnight and return a bit value to display that. 现在,我正在尝试比较同一个人的信息,这意味着-我看到列表中有一个人,该人昨天早上6点到达,请检查该人是否也在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM