0

私のテーブルは次のようになります。

表1:注:このテーブルは実際には非常に大きく、より多くの列(20ish)と行(数百万)があります

 |  Time      |  tmp   |  productionID  |
 |  10:00:00  |   2.2  |    5           |
 |  10:00:05  |   5.2  |    5           |
 |  10:00:11  |   7.4  |    5           |
 |  ......    |   3.2  |    5           |
 |  10:10:02  |   4.5  |    5           |

注:Timeはvarcharなので、次のようなことをする必要があると思います。

 CONVERT(VARCHAR(8), DATEADD(mi, 10, time), 114)

私がする必要があるのは:

 select time, tmp 
 from mytable
 where productionid = somevalue 
 and time = first_time_stamp associated to some productionID(ie. 10:00:00 table above)
     time = 10 minutes after the first_time_stamp with some productionID
     time = 20 minutes after the first_time_stamp with some productionID
     ...25, 30, 40, 60, 120, 180 minutes 

これが理にかなっていることを願っています。これを行う正しい方法がわかりません。つまり、次のプロセスを考えました。-最初のタイムスタンプを選択します(いくつかのproductionIDを使用)-その時間に10分を追加し、-20分を追加します。次に、ピボットテーブルを使用し、結合を使用してテーブル1にリンクする必要があります。より簡単な方法になります。

専門知識をよろしくお願いします。

期待されるサンプル出力:

    |  Time    | tmp
    | 10:00:00 | 2.2 
    | 10:10:02 | 4.5
    | 10:20:54 | 2.3 
    | 10:30:22 | 5.3
4

2 に答える 2

1

オンザフライで間隔テーブルを作成し、各ProductionIDの開始時刻と相互結合すると、同じカテゴリに分類されるMyTableからレコードを抽出し、最初のレコードのみを取得することを選択できます。

; with timeSlots (startSlot, endSlot) as (
  select 0, 10
  union all
  select 10, 20
  union all
  select 25, 30
  union all
  select 30, 40
  union all
  select 40, 60
  union all
  select 60, 120
  union all
  select 120, 180
),
startTimes (ProductionID, minTime) as (
  select ProductionID, min([Time])
    from MyTable
   group by ProductionID
),
groupedTime (ProductionID, [Time], [Tmp], groupOrder) as (
  select myTable.ProductionID,
         myTable.Time,
         myTable.Tmp,
         row_number () over (partition by myTable.productionid, timeSlots.startSlot
                           order by mytable.Time) groupOrder
    from startTimes
   cross join timeslots
   inner join myTable
      on startTimes.ProductionID = myTable.ProductionID
     and convert(varchar(8), dateadd(minute, timeSlots.startSlot, convert(datetime, startTimes.MinTime, 114)), 114) <= mytable.Time
     and convert(varchar(8), dateadd(minute, timeSlots.endSlot, convert(datetime, startTimes.MinTime, 114)), 114) > myTable.Time
)
select ProductionID, [Time], [Tmp]
  from groupedTime
 where groupOrder = 1

ここにSQLフィドル

于 2012-07-30T20:54:00.670 に答える
0

これは、時間内に欠落しているシーケンスがない場合に機能します。つまり、10分の増分ごとに時間値がある場合、これは機能します。

create table times([Time] time,tmp float,productionID int)
INSERT INTO times
VALUES('10:10:00',2.2,5),
      ('10:00:05',5.2,5),         
      ('10:00:11',7.4,5),
      ('10:00:18',3.2,5),         
      ('10:10:02',4.5,5),
      ('10:20:22',5.3,5)

select * from times

Declare @min_time time
select @min_time = MIN(time) from times

;WITH times1 as (select row_number() over (order by (select 0)) as id,time, tmp from times where productionID = 5)
,times2 as(
select id,time,tmp from times1 where time=@min_time 
union all
select t1.id,t1.time,t1.tmp from times2 t2 inner join times1 t1 on cast(t1.time as varchar(5))=cast(DATEADD(mi,10,t2.Time) as varchar(5)) --where t1.id-1=t2.id
)

,result as (select MAX(time) as time from times2
group by CAST(time as varchar(5)))

select distinct t2.Time,t2.tmp from times2 t2,Result r where t2.Time =r.time order by 1
于 2012-07-31T12:57:02.887 に答える