Great! Now we'd like to calculate the global average time to first order. This is a good indicator of how long a typical customer takes to make the first purchase. Take a look:
SELECT AVG(1.0 * DATEDIFF(Day, RegistrationDate, FirstOrderDate)) AS AvgDaysToFirstOrder
All we had to do was use
DATEDIFF() inside. Here we calculated the average number of days, but we can also use other time units.
Note that the
DATEDIFF() function returns integers, and in SQL Server the
AVG() function with integer arguments will return an integer. To get more precise results, you'll need to multiply the
AVG()'s arguments by
1.0 – that way the function will use floating point numbers, and it will return one.