Search code examples
sqlsql-server-2008t-sqlsubtotal

how to get sub total of time in sql


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|

Solution

  • 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!!