如何从15日到15日获取数据

时间:2013-09-03 23:37:18

标签: sql-server sql-server-2008 stored-procedures

我有一个用户输入时间表,如下所示:

表名是时间计费:

字段:

 TimeBillingId  int
 UserId         int
 StartTime      datetime
 StopTime       datetime
 ElapsedTime    time(7)
 NormalTime     time(7)
 OverTime       time(7)

现在我想要一份报告,其中我想计算所有用户的总时间。与Total ElapsedTime一样,按月计算正常时间和总加班时间。

但我的月份是15日至15日。所以我想结果应该是这样的:

User    Month        TotalTime         NormalTime       OverTime
 1     March-April   120:58:00         100:58:00        20:00:00
 2     March-April    97:40:23          97:40:23        00:00:00
 1     April-May      15:00:00          14:30:00        00:30:00
 2     April-May      89:30:00          80:15:00        09:15:00

我正在使用此查询使其按月运行:

select MONTH(tym.StopTIme) as Month, YEAR(tym.StopTime) as Year, U.UserId,
ISNULL((select cast(sum(datediff(second,0,t.ElapsedTime))/3600 as varchar(20)) + ':' + right('0' + cast(sum(datediff(second,0,t.ElapsedTime))/60%60 as varchar(20)),2) + ':' + right('0' + cast(sum(datediff(second,0,t.ElapsedTime))%60 as varchar(20)),2) 
from TimeBilling t where MONTH(t.StopTime) = MONTH(tym.StopTime) and YEAR(t.StopTime) = YEAR(tym.StopTime) and t.UserId = u.UserId),'00:00:00') as TotalTime,

ISNULL((select cast(sum(datediff(second,0,t.NormalTime))/3600 as varchar(20)) + ':' + right('0' + cast(sum(datediff(second,0,t.NormalTime))/60%60 as varchar(20)),2) + ':' + right('0' + cast(sum(datediff(second,0,t.NormalTime))%60 as varchar(20)),2) 
from TimeBilling t where MONTH(t.StopTime) = MONTH(tym.StopTime) and YEAR(t.StopTime) = YEAR(tym.StopTime) and t.UserId = u.UserId),'00:00:00') as NormalTime,

ISNULL((select cast(sum(datediff(second,0,t.OverTime))/3600 as varchar(20)) + ':' + right('0' + cast(sum(datediff(second,0,t.OverTime))/60%60 as varchar(20)),2) + ':' + right('0' + cast(sum(datediff(second,0,t.OverTime))%60 as varchar(20)),2) 
from TimeBilling t where MONTH(t.StopTime) = MONTH(tym.StopTime) and YEAR(t.StopTime) = YEAR(tym.StopTime) and t.UserId = u.UserId),'00:00:00') as OverTime 
from TimeBilling tym, Users u
where tym.UserId = u.UserId

我按月到1月30日/ 31日获得了总时间。但我希望从15日到15日。但我不知道如何在SQL查询或存储过程中获取它。

有人可以帮我吗?

3 个答案:

答案 0 :(得分:1)

最简单的方法就是从日期和组中减去14天。我假设你已经知道如何聚合时间:

select UserId,
       datename(month, min(starttime - 14)),
from timebilling tb
group by UserID, year(starttime - 14), month(starttime - 14)

这将产生序列中的第一个月。如果你想要两个月:

select UserId,
       datename(month, min(starttime - 14)) + '-' + datename(month, dateadd(month, 1, min(starttime - 14)))
from timebilling tb
group by UserID, year(starttime - 14), month(starttime - 14)

答案 1 :(得分:0)

最简单的方法是使用日历表:

http://web.archive.org/web/20070611150639/http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html

可以使用DatePart和DateName构建替代方法,如下所示:

DECLARE @dt DATE = '20130411'
--GETDATE() 

;
WITH    c AS ( SELECT   enddate = DATEADD(MONTH,
                                          CASE WHEN DAY(@dt) >= 16 THEN 1
                                               ELSE 0
                                          END,
                                          DATEADD(MONTH,
                                                  DATEDIFF(MONTH, 0, @dt), 0))
             )
    SELECT  DATENAME(MONTH, DATEADD(MONTH, -1, enddate)) + '-'
            + DATENAME(MONTH, enddate)
    FROM    c

答案 2 :(得分:0)

SELECT UserID, Month, SUM(...) AS TotalTime, SUM(...) AS TotalNormalTime....
FROM
(
SELECT
T.*, 
CASE WHEN DAY(StartTime) > 15 THEN DateName(month, StartTime) + '-' + DATENAME(month, DATEADD(month, 1, StartTime))
ELSE DATENAME(month, DATEADD(month, -1, StartTime)) + '-' +  DateName(month, StartTime)
END AS Month
FROM TimeBilling T
) TX
GROUP BY UserID, Month