SELECT
SaleDate, Product, SalePrice,
ISNULL(
( SELECT SUM(SalePrice) FROM SalesHistory sh1 WHERE Sh1.Product = sh.Product AND Sh1.SaleID < sh.SaleID
),0) AS RunningTotal
FROM SalesHistory sh
ORDER BY Product, SaleID在报表中引入的连续的销售总计查询即是相关子查询。对于表中的每种产品,相关子查询重述了其结果集,并且在将结果返回到结果集之前,为每一种售出产品的SalePrice进行加和操作。