简体   繁体   中英

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. 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.

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..

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. 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 :)

i removed the cast, so now i get the BIT column. but now the problem is that i get all "1"'s as results. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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