Skip to content
Advertisement

MS SQL Join Two Tables with ‘IN’ Faces Problem

I have two tables for my system.

Table 1: Book

enter image description here

Table 2: BookStatus

enter image description here

Now I want to find out the latest status of books that had been lent from 2018/11/05 to 201/11/10 and how many times they have been lent during this period.

The result I expect is like: enter image description here

My SQL script is:

After I execute the sql query, it just keeps running and doesn’t stop. Is there any problem with my SQL query script? How to revise my SQL script to have the result I expect?

Advertisement

Answer

Find out all the books that were lent during the period and the count using a sub query. Use CROSS APPLY to fetch the latest status:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement