Search code examples
c#sqllinqlinq-to-entities

How to get records that not have specific records in another table


Please consider these Tables:

User:

UserId      Name       LastName
--------------------------------
User1       Name1        LName1
User2       Name2        LName2
User3       Name3        LName3
User4       Name4        LName4

and Users Login Logs:

UserId       LogDate      Status
--------------------------------
User1       2022-01-01      1 <--For Successful Login
User1       2022-01-02      0 <--For UnSuccessful Login
User2       2022-01-02      1
User4       2022-02-02      1
User4       2022-03-02      0
User4       2022-05-02      1
User4       2022-05-10      0
User5       2022-05-10      0

No I want to find those users that don't login in specific duration (for example 1 past month). In another point of view I don't want the users that have at least one Successful login log in specific duration.

I wrote some queries but I couldn't check exclude records that have at least one Successful login log. This is a SQL version of the Linq query:

select * 
from Users aa left join Login_Log bb on aa.UserId = bb.UserId
where (bb.LogDate >= DATEADD(month, -1, getdate()) and bb.LogDate <= getdate()) and bb.Status is null

Solution

  • The LogedInUsers CTE have users which have at least one successfull login. Then the final result is user which are not in CTE:

    WITH LogedInUsers
    AS (SELECT DISTINCT bb.UserId
        FROM Login_Log bb
        WHERE bb.LogDate >= DATEADD(MONTH, -1, GETDATE())
              AND bb.LogDate <= GETDATE()
              AND bb.Status = 1
       )
    SELECT aa.*
    FROM Users aa
        LEFT JOIN LogedInUsers liu ON aa.UserId = liu.UserId
        WHERE liu.UserId IS NULL;