这是我的数据:
+----+------------------+--------------------+
| ID | DateTime Entered | DateTime Completed |
+----+------------------+--------------------+
| 1 | 6/17/2011 10:08 | 6/18/2011 14:39 |
| 2 | 5/10/2011 9:29 | 5/15/2011 17:29 |
| 3 | 3/2/2009 9:30 | 3/3/2009 13:40 |
| 4 | 3/3/2009 11:36 | 3/4/2009 16:39 |
| 5 | 3/3/2009 11:37 | 3/4/2009 16:39 |
| 6 | 3/3/2009 11:38 | 3/4/2009 16:39 |
| 7 | 3/3/2009 11:38 | 3/4/2009 16:38 |
| 8 | 3/3/2009 11:38 | 3/5/2009 9:08 |
| 9 | 1/10/2011 9:43 | 1/11/2011 13:13 |
| 10 | 2/11/2011 12:20 | 2/12/2011 15:01 |
| 11 | 8/14/2009 10:20 | 8/17/2009 15:56 |
| 12 | 11/19/2008 9:45 | 11/20/2008 14:56 |
| 13 | 2/10/2011 13:50 | 2/11/2011 19:36 |
| 14 | 8/14/2009 10:23 | 8/17/2009 15:56 |
| 15 | 8/17/2009 9:43 | 8/18/2009 13:56 |
| 16 | 8/17/2009 9:43 | 8/19/2009 6:47 |
| 17 | 8/14/2009 10:28 | 8/17/2009 15:56 |
| 18 | 12/17/2008 10:03 | 12/18/2008 11:07 |
| 19 | 1/13/2009 9:41 | 1/14/2009 18:11 |
| 20 | 1/20/2009 9:18 | 1/21/2009 15:08 |
| 21 | 2/12/2009 10:53 | 2/13/2009 15:21 |
| 22 | 1/20/2009 9:19 | 1/21/2009 15:08 |
| 23 | 1/13/2009 9:50 | 1/15/2009 8:45 |
| 24 | 1/13/2009 9:50 | 1/14/2009 14:40 |
| 25 | 1/13/2009 9:36 | 1/14/2009 18:11 |
| 26 | 12/17/2008 10:06 | 12/18/2008 11:06 |
| 27 | 12/17/2008 10:04 | 12/18/2008 11:06 |
| 28 | 3/30/2010 9:20 | 3/31/2010 13:44 |
| 29 | 1/25/2010 9:33 | 1/26/2010 14:20 |
| 30 | 1/30/2009 11:21 | 2/2/2009 14:44 |
| 31 | 6/17/2010 9:07 | 6/18/2010 14:17 |
| 32 | 6/16/2010 10:24 | 6/17/2010 16:20 |
| 33 | 6/14/2010 9:33 | 6/15/2010 8:52 |
| 34 | 5/27/2010 9:15 | 5/28/2010 14:15 |
| 35 | 5/27/2010 10:16 | 5/28/2010 15:51 |
| 36 | 5/21/2010 9:15 | 5/23/2010 13:05 |
| 37 | 5/19/2010 10:08 | 5/20/2010 13:33 |
| 38 | 6/30/2010 11:41 | 7/1/2010 18:16 |
+----+------------------+--------------------+
这是我希望将其放入的格式。对于特定年份,我们称之为2008
:
Time Completed Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
12AM 1
1AM 2 1
2AM 1 1 1
3AM 3
4AM 342 1
. 32 34
. 42
.
NEXT DAY 12AM 2 1
NEXT DAY 1AM 1
.
.
............
DateTime Entered
为我们提供了开始日期,DateTime Completed
为我们提供了完成时间。
例如第一个。它的完成时间为14:39
,而第二个时间则为5th day 15:29
。第三个是2nd day 13:40
非常感谢你的建议和指导。如果你能指出我正确的方向,我将非常感激。
SQL或SSRS解决方案是理想的。
答案 0 :(得分:2)
您可以按小时分组持续时间:
select datediff(hour, [DateTime Entered], [DateTime Completed])
, sum(case when month([DateTime Entered] = 1 then 1 end) as Jan
, sum(case when month([DateTime Entered] = 2 then 1 end) as Feb
, sum(case when month([DateTime Entered] = 3 then 1 end) as Mar
, ...
from YourTable
group by
datediff(hour, [DateTime Entered], [DateTime Completed])
重新阅读,似乎我可以用第二种方式阅读你的问题。如果您想在结束时间分组(即7月2日23:00到7月3日01:00将计为25小时),那么您可以将开始时间投射到目前为止:
select datediff(hour, cast([DateTime Entered] as date), [DateTime Completed])
, sum(case when month([DateTime Entered] = 1 then 1 end) as Jan
, ...
from YourTable
group by
datediff(hour, cast([DateTime Entered] as date), [DateTime Completed])
答案 1 :(得分:2)
编辑包括排序:
create table tblTest (ID int , Entered datetime, Completed datetime)
set dateformat mdy
insert into tblTest values (1,'6/17/2011 10:08','6/18/2011 14:39')
insert into tblTest values ( 2 ,'5/10/2011 9:29','5/15/2011 17:29')
insert into tblTest values ( 3 ,'3/2/2009 9:30','3/3/2009 13:40')
insert into tblTest values ( 4 ,'3/3/2009 11:36','3/4/2009 16:39')
insert into tblTest values ( 5 ,'3/3/2009 11:37','3/4/2009 16:39')
insert into tblTest values ( 6 ,'3/3/2009 11:38','3/4/2009 16:39')
insert into tblTest values ( 7 ,'3/3/2009 11:38','3/4/2009 16:38')
insert into tblTest values ( 8 ,'3/3/2009 11:38','3/5/2009 9:08')
insert into tblTest values ( 9 ,'1/10/2011 9:43','1/11/2011 13:13')
insert into tblTest values ( 10 ,'2/11/2011 12:20','2/12/2011 15:01')
insert into tblTest values ( 11 ,'8/14/2009 10:20','8/17/2009 15:56')
insert into tblTest values ( 12 ,'11/19/2008 9:45','11/20/2008 14:56')
insert into tblTest values ( 13 ,'2/10/2011 13:50','2/11/2011 19:36')
insert into tblTest values ( 14 ,'8/14/2009 10:23','8/17/2009 15:56')
insert into tblTest values ( 15 ,'8/17/2009 9:43','8/18/2009 13:56')
insert into tblTest values ( 16 ,'8/17/2009 9:43','8/19/2009 6:47')
insert into tblTest values ( 17 ,'8/14/2009 10:28','8/17/2009 15:56')
insert into tblTest values ( 18 ,'12/17/2008 10:03','12/18/2008 11:07')
insert into tblTest values ( 19 ,'1/13/2009 9:41','1/14/2009 18:11')
insert into tblTest values ( 20 ,'1/20/2009 9:18','1/21/2009 15:08')
insert into tblTest values ( 21 ,'2/12/2009 10:53','2/13/2009 15:21')
insert into tblTest values ( 22 ,'1/20/2009 9:19','1/21/2009 15:08')
insert into tblTest values ( 23 ,'1/13/2009 9:50','1/15/2009 8:45')
insert into tblTest values ( 24 ,'1/13/2009 9:50','1/14/2009 14:40')
insert into tblTest values ( 25 ,'1/13/2009 9:36','1/14/2009 18:11')
insert into tblTest values ( 26 ,'12/17/2008 10:06','12/18/2008 11:06')
insert into tblTest values ( 27 ,'12/17/2008 10:04','12/18/2008 11:06')
insert into tblTest values ( 28 ,'3/30/2010 9:20','3/31/2010 13:44')
insert into tblTest values ( 29 ,'1/25/2010 9:33','1/26/2010 14:20')
insert into tblTest values ( 30 ,'1/30/2009 11:21','2/2/2009 14:44')
insert into tblTest values ( 31 ,'6/17/2010 9:07','6/18/2010 14:17')
insert into tblTest values ( 32 ,'6/16/2010 10:24','6/17/2010 16:20')
insert into tblTest values ( 33 ,'6/14/2010 9:33','6/15/2010 8:52')
insert into tblTest values ( 34 ,'5/27/2010 9:15','5/28/2010 14:15')
insert into tblTest values ( 35 ,'5/27/2010 10:16','5/28/2010 15:51')
insert into tblTest values ( 36 ,'5/21/2010 9:15','5/23/2010 13:05')
insert into tblTest values ( 37 ,'5/19/2010 10:08','5/20/2010 13:33')
insert into tblTest values ( 38 ,'6/30/2010 11:41','7/1/2010 18:16')
insert into tblTest values ( 39 ,'6/30/2010 11:41','6/30/2010 18:16')
insert into tblTest values ( 40 ,'1/10/2011 9:43','1/11/2011 13:13')
select
datediff(d, CAST(entered as DATE), CAST(completed as DATE)) AS Sort_Days,
DATEPART(hour, completed) AS Sort_Hours,
CASE datediff(d, CAST(entered as DATE), CAST(completed as DATE))
WHEN 0 THEN '' WHEN 1 THEN 'NEXT DAY ' WHEN 2 THEN '2ND DAY ' WHEN 3 THEN '3RD DAY ' ELSE CONVERT(varchar(10), datediff(d, CAST(entered as DATE), CAST(completed as DATE))) + 'TH DAY '
END
+ CONVERT(varchar(10), DATEPART(hour, completed) - CASE WHEN DATEPART(hour, completed) > 12 THEN 12 ELSE 0 END )
+ CASE WHEN DATEPART(hour, completed) < 12 THEN 'AM' ELSE 'PM' END AS TimeCompleted,
case when datepart(month,entered) = 1 then 1 end as Jan,
case when datepart(month,entered) = 2 then 1 end as Feb,
case when datepart(month,entered) = 3 then 1 end as Mar,
case when datepart(month,entered) = 4 then 1 end as Apr,
case when datepart(month,entered) = 5 then 1 end as May,
case when datepart(month,entered) = 6 then 1 end as Jun,
case when datepart(month,entered) = 7 then 1 end as Jul,
case when datepart(month,entered) = 8 then 1 end as Aug,
case when datepart(month,entered) = 9 then 1 end as Sep,
case when datepart(month,entered) = 10 then 1 end as Oct,
case when datepart(month,entered) = 11 then 1 end as Nov,
case when datepart(month,entered) = 12 then 1 end as Dec
into #tblTest
from tblTest
select TimeCompleted, sum(Jan), sum(Feb), sum(Mar), sum(Apr), sum(May), sum(Jun), sum(Jul), sum(Aug), sum(Sep), sum(Oct), sum(Nov), sum(Dec)
from #tblTest
group by TimeCompleted, Sort_Days, Sort_Hours
order by Sort_Days, Sort_Hours