我必须得到SQL中花费的所有时间的总计吗?
这只是我查询的一个简单示例。
`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`
总数不应超过工作小时数
删除了我的记录表
`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`
基本上我需要什么
名称|开始时间|结束时间| 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|
答案 0 :(得分:0)
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));
结果:
08:42:00
因此,如果您在表中有数据,那将非常相似:
SELECT CONVERT(TIME(0), DATEADD(MINUTE,
DATEDIFF(MINUTE, A.start_time, A.end_time), 0)) AS [HOURS_WORKED]
FROM ...
您应远离添加和减去日期/时间类型等内容,并使用DATEADD
等内置函数。这个简写适用于datetime / smalldatetime,但对于新类型可能会有问题。
答案 1 :(得分:0)
好吧,我通过添加一个字段“MINUTES”并运行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);
}
}
女巫给我作为int的分钟,然后运行
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);
}
接着是
label5.text = GetTotal();
和KABOOM完成了所有花在分钟上的时间总和问题解决了
这是一个C#程序,但我需要SQL查询来解决它
感谢帮助的人们!!