Listing 1: Analyzing a T-SQL Query from the Inside Out DECLARE @customerid int SET @customerid=3 [BEGIN CALLOUT A] SELECT e.cname, f.value AS value -- Get the value for each cname and max tid. FROM [BEGIN CALLOUT B] (SELECT d.cname, max(d.tid) AS tid -- Get the max tid for every cid FROM [BEGIN CALLOUT C] (SELECT a.cname, b.pid, max(c.tid) AS tid -- Get the max tid for every cid and pid. FROM dbo.cats AS a INNER JOIN dbo.prods AS b ON (a.cid=b.cid) LEFT OUTER JOIN dbo.trans AS c ON (b.pid=c.pid AND c.custid = @customerid) GROUP BY a.cname, b.pid) AS d [END CALLOUT C] GROUP BY d.cname) AS e [END CALLOUT B] LEFT OUTER JOIN dbo.trans AS f ON (f.tid=e.tid) [END CALLOUT A]