Skip to content
Advertisement

Count times date occurs between two dates in a second table

I have two tables [Charges] and [Defects] and want to produce [Desired Query Output] where the output counts the occurrances of defect when [Charges].ChargeDate is between (and including) [Defects].OpenDate and [Defects].CloseDate. For [Defects] table, a close date of NULL means it has not closed yet. Seems simple enough, but I haven’t found a good example of how to do this. Can you help??

I’m using SQL Server version 12.

[Charges]

[Defects]

[Desired Query Output]

Advertisement

Answer

You can use a correlated subquery or a lateral join:

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