我有一张这样的表:
name | action | action_timestamp
------------------------------------------
ABC | START_PLAY | 2017-02-02 10:00:00
PQR | START_PLAY | 2017-02-02 11:00:00
ABC | END_PLAY | 2017-02-02 10:30:00
PQR | END_PLAY | 2017-02-02 11:05:00
我希望获得所有已经开始在给定时间戳之后或之后播放的名字,并在给定时间戳之前或之前结束播放。
例如,如果(action =' START_PLAY'和action_timestamp> = 2017-02-02 10:00:00)和(action =' END_PLAY'和action_timestamp< = 2017-02-02 10:30:00)然后查询应该返回ABC
我不确定应该如何制作条件(即start_play和action_timestamp)。
答案 0 :(得分:0)
试试这个:
GraphStage
答案 1 :(得分:0)
假设每个"acorn": {
"version": "4.0.4",
"from": "acorn@>=4.0.4 <5.0.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-4.0.4.tgz"
},
确实存在name
的一行和START_PLAY
的一行,您可以使用以下内容:
END_PLAY
嵌套查询对表执行单次扫描,并为每个select name, start_play, end_play
from (
select name,
max ( case when action = 'START_PLAY' then action_timestamp end) as start_play,
max ( case when action = 'END_PLAY' then action_timestamp end) as end_play
from yourTable
group by name
)
where start_play > ...
and end_play < ...
创建一行,行上有name
和start_play
;外部部分只是对内部查询产生的行应用过滤器。
答案 2 :(得分:0)
试试这个:
select data.name from
(SELECT NAME,ACTION,ACTION_TIMESTAMP, ROW_NUMBER() OVER(PARTITION BY NAME ORDER BY ACTION) AS count FROM ST3
where (action='START_PLAY' AND ACTION_TIMESTAMP>='02-FEB-17 10.00.00')OR
(ACTION='END_PLAY' AND ACTION_TIMESTAMP<='02-FEB-17 10.30.00')) data
where data.count=2;
答案 3 :(得分:0)
像这样使用相交:
SELECT DISTINCT name FROM TBL WHERE action='START_PLAY' and action_timestamp >= '2017-02-02 10:00:00'
INTERSECT
SELECT DISTINCT name FROM TBL WHERE action='END_PLAY' and action_timestamp <= '2017-02-02 10:30:00'