2015-07-03 83 views
0

我想從SQL Server 2008 R2中的單個表中提取...我有兩列。增量式SQL與標籤

IDate[Datetime] and ID[Numeric] 

我想構建一個MSSQL查詢,將返回以下內容。

檢索標準

-IDate MM\DD\YYYY 
-ID #### 

返回

-HourIncrement 00:00 - 00:30 
-RecordsForHour 3 
-CumulativeRecords 3 
-ID 

-HourIncrement 00:30 - 01:00 
-RecordsForHour 1 
-CumulativeRecords 4 
-ID 

的樣本數據

enter image description here

預期結果

enter image description here

+1

什麼是與你有困難的部分? –

+0

所以你每天會有48組?假設每個增量都發生了什麼? –

+0

或者您是否需要全部48個,並且稀疏條目指示RecordsForHour = 0(並且不會RecordsForInterval是更好的名稱....) – Stan

回答

0

這將提供期望的結果,而無需使用CTE(公用表表達式),其可以是更容易理解,並且可以具有更好的性能。

定義樣本的測試數據:

Declare @test Table 
(
    IDate  Datetime 
    ,ID   Int 
) 

Insert Into @test(IDate,ID) Values 
('2015-06-23 00:11:22.387',1000) 
,('2015-06-23 00:15:52.192',1000) 
,('2015-06-23 00:19:29.344',1000) 
,('2015-06-23 00:39:20.660',1000) 

,('2015-06-23 01:21:22.387',1000) 
,('2015-06-23 01:25:52.192',1000) 
,('2015-06-23 01:32:29.344',1000) 
,('2015-06-23 01:39:20.660',1000) 

,('2015-06-23 23:21:22.387',1000) 
,('2015-06-23 23:25:52.192',1000) 
,('2015-06-23 23:32:29.344',1000) 
,('2015-06-23 23:39:20.660',1000) 

,('2015-06-24 00:11:22.387',1000) 
,('2015-06-24 00:15:52.192',1000) 
,('2015-06-24 00:19:29.344',1000) 
,('2015-06-24 00:39:20.660',1000) 

查詢:

select * 
from 
(
    select Convert(varchar, DayPeriod, 101) as DayPeriod, 
    HourIncrement, 
    count(*) as RecordForHour, 
    max(rownum) as CumulativeRecords, 
    min(ID) as ID 
    from 
    (
     select *, 
      Row_Number() over (order by IDate asc) as rownum,  
      DATEADD(dd, 0, DATEDIFF(dd, 0, IDate)) as DayPeriod, 
      case when DATEPART(mi, IDate) between 0 and 30 
      then dateHour + ':00 - ' + dateHour + ':30' 
      else dateHour + ':30 - ' + dateNextHour + ':00' 
      end as HourIncrement 
     from 
      (
       select *, 
       right('00' + Convert(varchar(4), DATEPART(hh, IDate)), 2) as dateHour, 
       right('00' + Convert(varchar(4), DATEPART(hh, DateAdd(hh, 1, IDate))), 2) as dateNextHour 
       from @test 
      ) t1 
    ) t2 
    group by DayPeriod, HourIncrement 
) t3 
order by CumulativeRecords asc 

結果

enter image description here

0

這可能對您有所幫助: -

Set Nocount On; 

Declare @MinTime Time 

Select @MinTime = '00:00' 

Declare @test Table 
(
    IDate  Datetime 
    ,ID   Int 
) 

Declare @resultTable Table 
(
    RowId    Int Identity(1,1) Primary Key 
    ,RecordsPerHour  Int 
    ,ID     Int 
    ,MinTime   Time 
    ,MaxTime   Time 
) 

Insert Into @test(IDate,ID) Values 
('2015-06-23 00:11:22.387',1000) 
,('2015-06-23 00:15:52.192',1000) 
,('2015-06-23 00:19:29.344',1000) 
,('2015-06-23 00:39:20.660',1000) 

;With timeCte As 
(
    Select @MinTime As minTime 
      ,Dateadd(Mi, 30, @MinTime) As maxTime 

    Union All 

    Select Dateadd(mi, 30, minTime) As minTime 
      ,Case When t.maxTime = Cast('23:00' As Time) Then Dateadd(mi, 29, maxTime) Else Dateadd(mi, 30, maxTime) End As maxTime 
    From timeCte As t 
    Where t.maxTime < Cast('23:59' As Time) 
) 


Insert Into @resultTable(RecordsPerHour,ID,MinTime,MaxTime) 
Select Count(1) As RecordsForHour 
     ,t.ID 
     ,tc.minTime 
     ,tc.maxTime 
From timeCte As tc With (Nolock) 
     Join @test As t On Cast(t.IDate As Time) >= tc.minTime 
      And Cast(t.IDate As Time) <= tc.maxTime 
Group By t.ID 
     ,tc.minTime 
     ,tc.maxTime 

Select Cast(t.minTime As Varchar(5)) + ' - ' + Cast(t.maxTime As Varchar(5)) As Time 
     ,t.RecordsPerHour 
     ,t.RecordsPerHour + Isnull(t1.RecordsPerHour,0) As CumulativeRecords 
     ,t.ID 
From @resultTable As t 
     Left Join @resultTable As t1 On t.ID = t1.ID And t.RowId = (t1.RowId + 1) 
Order By t.ID 
     ,t.MinTime 
     ,t.MaxTime 

,如果你使用的是MSSQL Server 2012中或更高版本,那麼你可以使用滯後()函數,而不是左連接計算CumulativeRecords。上述查詢的

輸出: -

enter image description here

+0

錯誤,timeCte不是定義的系統類型 – DataCrypt

+0

您可能有複製粘貼錯誤,I也檢查了MSSQL 2008和2012年,沒有任何問題的工作。 CTE總是比任何內部/子查詢都快。在這裏,您的問題也不清楚重複整天的時間,或者只顯示數據中的時間段。 –

+0

即使0個記錄,我也想顯示當天的所有時段 – DataCrypt