Listing 2: Example of Putting Each Query in Its Own Stored Procedure CREATE PROCEDURE NumberOfUsers @BeginDateTime DATETIME, @EndDateTime DATETIME, @MachineName VARCHAR(1000) = '' AS BEGIN -- Calculate the number of users for the test. IF @MachineName = '' EXEC NumberOfUsersAll @BeginDateTime, @EndDateTime ELSE EXEC NumberOfUsersMachine @BeginDateTime, @EndDateTime, @MachineName END CREATE PROCEDURE NumberOfUsersAll @BeginDateTime DATETIME, @EndDateTime DATETIME AS BEGIN -- Calculate the number of users for the test. SELECT COUNT(DISTINCT MachineName+UserName) FROM MetricEvent WHERE EndDateTime > @BeginDateTime AND EndDateTime < @EndDateTime END CREATE PROCEDURE NumberOfUsersMachine @BeginDateTime DATETIME, @EndDateTime DATETIME, @MachineName VARCHAR(1000) = '' AS BEGIN -- Calculate the number of users for the test. SELECT COUNT(DISTINCT UserName) FROM MetricEvent WHERE EndDateTime > @BeginDateTime AND EndDateTime < @EndDateTime AND MachineName = @MachineName END