Skip to content
Advertisement

counting totals after left join and requiring 0 for a NULL variable – SQL Server

I am using SQL Server Management Studio 2012 and I am running the following query:

I would like the total number of appointments for these 3 individuals for now. Then, I will include everyone in Table 1. Table 1 gives me the ID (one row per individual), Table 2 gives me all appointments across different days per individual.

The results I get are:

But I would like:

Can you please advise?

Advertisement

Answer

Move the WHERE conditions on the second table to the ON clause:

Note that the conditions on the first table remain in the WHERE clause. Also, IN is simpler than a bunch of OR conditions.

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