-1

ここに私のデータがあります:

+----+------------------+--------------------+
| 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ですが、2 番目の時間は になります5th day 15:29。3つ目は2nd day 13:40

あなたのアドバイスと指導に感謝します。正しい方向に向けていただければ幸いです。

SQL または SSRS ソリューションが理想的です。

4

2 に答える 2

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])
于 2012-06-26T19:48:33.967 に答える
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
于 2012-06-26T20:13:51.450 に答える