Question

I have to get the sub total of time spent in SQL any ideas?

This is just a simple example of my query.

`SELECT 
   A.INDX, B.CLIENTNAME, C.PROJECTNAME, D.NAME, 
   CONVERT(CHAR(10), A.CDATE, 101) [DATE],
   CONVERT(TIME(0), A.START_TIME, 108) [START],
   CONVERT(TIME(0), A.END_TIME, 108) [END],
   CONVERT(TIME(0), (A.END_TIME - A.START_TIME ), 108) [HOURS_WORKED]
FROM 
   LOGSHEET A, CLIENTS B, PROJECTS C, DEVELOPERS D
WHERE B.CLIENTNO = 1
  AND C.PROJECTID = 11
  AND D.USERID = 1`

The total should be under hours worked

Dropped my logsheet table

`BEGIN CREATE TABLE [dbo].[LOGSHEET](
[INDX] [int] IDENTITY(1,1) NOT NULL,
[CLIENTNO] [int] NULL,
[PROJECTID] [int] NULL,
[USERID] [int] NULL,
[CDATE] [datetime] NULL,
[START_TIME] [datetime] NULL,
[END_TIME] [datetime] NULL) ON [PRIMARY]END GO`

basically what I Need

name | start time | End time | Hours_worked |

AJ......| 07:00:00 | 07:15:00 | 00:15:00 |

AJ .....| 07:00:00 | 07:15:00 | 00:15:00 |

            Total:   |00:30:00|
Was it helpful?

Solution 2

Ok well I figured out an solution to my problem by just adding a field "MINUTES" and running the query from c#

if (e.CommandName == "Stop") {
    int row = int.Parse(e.CommandArgument.ToString());
    string indx = GridView1.Rows[row].Cells[1].Text; 
    string s = null;
    s = "UPDATE LOGSHEET ";
    s += "SET MINUTES = (DATEPART(HOUR,TIME_SPENT)*60)+(DATEPART(MINUTE,TIME_SPENT)) ";
    s += "WHERE INDX = @p0 ";


    String[] pr = new String[1];
    pr[0] = indx; 

    cQ.execSql(s, pr);

  }
}

witch gives me the minutes as int and then just run

protected string GetTotal() {
  string userid = Session["id"].ToString();
  string s = null;


  s = "SELECT SUM(MINUTES) ";
  s += "FROM LOGSHEET ";
  s += "WHERE USERID = @p0 ";

  String[] pr = new String[1];
  pr[0] = userid;

  return cQ.GenQuery(s, pr);
}

followed by

label5.text = GetTotal();

and KABOOM done sum of all time spent in minutes problem solved

this is a C# program but I needed the SQL queries to solve it

THANKS to the people that Helped!!

OTHER TIPS

DECLARE @start_time DATETIME, @end_time DATETIME;

SELECT @start_time = '20120621 22:05', @end_time = '20120622 06:47';

SELECT CONVERT(TIME(0), DATEADD(MINUTE, DATEDIFF(MINUTE, @start_time, @end_time), 0));

Result:

08:42:00

So if you had data in a table, it would be quite similar:

SELECT CONVERT(TIME(0), DATEADD(MINUTE, 
  DATEDIFF(MINUTE, A.start_time, A.end_time), 0)) AS [HOURS_WORKED]
FROM ...

You should stay away from things like adding and subtracting date/time types, and use the built-in functions like DATEADD. This shorthand works with datetime/smalldatetime but can be problematic with the new types.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top