title: SQL Query to distance traveled in a day based on timestamp and lat long
body: <p>I have a table with Date and latitude, longitude values in each row. I want a sql query to calculate the distance travailed in a day.</p>
  
  <p>Say for date <code>2013-03-01</code> I want the total distance traveled, </p>
  
  <pre><code>ID      DATE                  LAT     LONG    V_ID
  ---------------------------------------------------
  123     2013-03-01 06:05:24   45.544  86.544   1
  124     2013-03-01 06:15:17   45.676  86.676   1 
  125     2013-03-01 06:25:24   46.544  86.544   2
  126     2013-03-01 06:38:14   46.651  86.651   2
  127     2013-03-02 07:12:04   46.876  86.876   1 
  128     2013-03-02 10:38:14   46.871  86.871   1
  129     2013-03-02 10:56:14   46.871  86.671   2
  130     2013-03-02 15:28:02   46.243  86.871   2
  </code></pre>
  
  <p>To calculate the distance what I wrote a sql function :</p>
  
  <pre><code>CREATE FUNCTION [dbo].[fnCalcDistanceKM](@lat1 FLOAT, @lat2 FLOAT, @lon1 FLOAT, @lon2 FLOAT)
   RETURNS FLOAT 
  AS
  BEGIN
  
  RETURN ACOS(SIN(PI()*@lat1/180.0)*SIN(PI()*@lat2/180.0)+COS(PI()*@lat1/180.0)*COS(PI()*@lat2/180.0)*COS(PI()*@lon2/180.0-PI()*@lon1/180.0))*6371
  END
  </code></pre>
  
  <p>but I want the total distance traveled in a day and for day <code>2013-03-01</code> I have top <code>four row</code> and I want total distance traveled in <code>2013-03-01</code></p>
  
  <p>and similar to this date <code>2013-03-02</code> has last four row , how do I calculate distance for these rows.</p>