0
MachineID   Active_Inactive   Time
    A              0        10.10 am
    A              0        10.11 am
    A              1        10.12 am
    A              0        10.13 am
    A              0        10.14 am
    A              0        10.15 am
    A              1        10.16 am
    A              1        10.17 am
    A              1        10.18 am

ここで、上記の表から、マシンAが 2 分間にアクティブだった回数と非アクティブだった回数がわかるような出力を見つけたいと思います。したがって、集計は 2 分間のスティントごとに行う必要があります。同様に、Aは 10.10 から 10.11 まで 2 回非アクティブで、0 回アクティブでした。出力テーブルを表現する最良の方法は何ですか

スロットは5つ

10.10-10.11(1), 10.12-10.13(2) and so on...

出力は次のようになります。

Slots   Active A    Inactive A
  1       0            2
  2       1            1
  3       0            2
  4       1            1
  5       2            0
4

2 に答える 2

1

時間が日付型であると仮定すると、これが私がすることです。これはオラクルにあることに注意してください。しかし、それはあまり変わらないはずです。

CREATE TABLE temp (
  Machine nvarchar2 (10),
  Active number,
  dt date
);

INSERT INTO temp VALUES ('A', 0, to_date('10.10 am', 'hh.mi am')); 
INSERT INTO temp VALUES ('A', 0, to_date('10.11 am', 'hh.mi am')); 
INSERT INTO temp VALUES ('A', 1, to_date('10.12 am', 'hh.mi am'));
INSERT INTO temp VALUES ('A', 0, to_date('10.13 am', 'hh.mi am')); 
INSERT INTO temp VALUES ('A', 0, to_date('10.14 am', 'hh.mi am')); 
INSERT INTO temp VALUES ('A', 0, to_date('10.15 am', 'hh.mi am')); 
INSERT INTO temp VALUES ('A', 1, to_date('10.16 am', 'hh.mi am')); 
INSERT INTO temp VALUES ('A', 1, to_date('10.17 am', 'hh.mi am')); 
INSERT INTO temp VALUES ('A', 1, to_date('10.18 am', 'hh.mi am')); 

Select
  Machine, 
  Active, 
  to_char(dt, 'hh') || '.' || to_char(floor(to_char(DT, 'mi') /2) * 2) || '-' || to_char(dt, 'hh') || '.' || to_char(floor(to_char(DT, 'mi') /2) * 2 + 1) timeGroup
from temp
group by Machine, Active, to_char(dt, 'hh') || '.' || to_char(floor(to_char(DT, 'mi') /2) * 2) || '-' || to_char(dt, 'hh') || '.' || to_char(floor(to_char(DT, 'mi') /2) * 2 + 1)
;
于 2013-07-09T07:57:27.647 に答える
0

変換、文字列、および日付関数を使用してグループ化を作成できます

SELECT machine_id, active_inactive,CONVERT(VARCHAR(13),time,21)+ ':'+RIGHT ('00'+FLOOR(CAST(DATEPART(minute,time)/2) *2 AS VARCHAR(2)),2), COUNT(*)
FROM yourtable
GROUP BY machine_id, active_inactive, CONVERT(VARCHAR(13),time,21)+ ':'+RIGHT ('00'+FLOOR(CAST(DATEPART(minute,time)/2) *2 AS VARCHAR(2)),2)
于 2013-07-09T08:02:12.297 に答える