LISTING 6: Query That Uses Solution 1's UDF and Views SELECT starttime, endtime, dbo.fn_fmtelapsed(DATEDIFF(second, starttime, endtime)) AS elapsed, MAX(NOx) AS max_NOx FROM VStartEnd JOIN Samples ON sample_ts BETWEEN starttime AND endtime GROUP BY starttime, endtime