Database 从同一列中提取不同的查询,结果显示在不同的列中
我的状态如下表所示:Database 从同一列中提取不同的查询,结果显示在不同的列中,database,postgresql,join,union,Database,Postgresql,Join,Union,我的状态如下表所示: section robotid date active area1| 11 | 2014-08-09 02:40:09 | 1 area1| 11 | 2014-08-09 02:55:50 | 0 area3| 01 | 2014-08-09 03:40:09 | 1 area3| 11 | 2014-08-10 00:1
section robotid date active
area1| 11 | 2014-08-09 02:40:09 | 1
area1| 11 | 2014-08-09 02:55:50 | 0
area3| 01 | 2014-08-09 03:40:09 | 1
area3| 11 | 2014-08-10 00:15:50 | 1
area1| 11 | 2014-08-10 02:40:09 | 1
area1| 11 | 2014-08-12 00:15:50 | 0
对于特定部分和robot id,我想使用两个单独的列提取robot处于活动状态和非活动状态的日期。例如,区域1和
robotid=11,我应该得到这个结果:
robot_ON | robot_OFF
2014-08-09 02:40:09 | 2014-08-09 02:55:50
2014-08-10 02:40:09 | 2014-08-12 00:15:50
但结果显示在一列中。有两列的想法是在以后计算机器人每次开始工作时处于活动状态时的时差。尝试将机器人自身加入状态,类似这样的事情(我没有尝试过)
非常感谢,我尝试了它,它是工作除了“on.date”和“off.date”不被接受,我用robotOn.date和robotOff.date代替。
select date from robot_state where (active=1 and section ='area1' and robotid=11)
union
select date from robot_state where (active=0 and section ='area1' and robotid=11);
select on.date, off.date from (
select date
from robot_state
where active=1 and section ='area1' and robotid=11
) as On
inner join (
select date
from robot_state
where active=0 and section ='area1' and robotid=11
) as off
on off.date = (select min(date)
from robot_state
where active=0 and section ='area1' and robotid=11
and date > on.date)