I think the code of Listing 5 and Listing 6 is mixed.
Example of using a Subquery in a Function Call
To demonstrate using a subquery in a function call, suppose that you have the requirement to display the number of days between the OrderDate and the maximum OrderDate for each Sales.SalesOrderHeader records.
The code in Listing 6 meets this requirement.
SELECT count(*), OrderDate
GROUP BY OrderDate
HAVING count(*) >
WHERE OrderDate = '2006-05-01 00:00:00.000');
Listing 6: Subquery in function call
The code in Listing 6 has two different subqueries. Both subqueries return the max OrderDate in the Sales.SalesOrderHeader table.
But the first subquery is used to pass a date to the second parameter of the DATEDIFF function.
Please correct Listing 6