查询一个表,把对应的行转列怎么写?

-一个大坑 2017-08-04 02:17:06

select t1.deriveno, t1.carno, t1.begintime, t2.endtime
from (select deriveno, carno, actualtime begintime
FROM (select a.deriveno,
a.carno,
a.stagecode,
to_char(a.actualtime, 'yyyy-mm-dd hh24:mi:ss') as actualtime
from app_carstatus a, app_send b
where a.deriveno = b.deriveno
and b.status in ('0', '10')
and b.deriveno not in (select deriveno from app_ridecost))
where stagecode = '3') t1
left join (select deriveno, actualtime endtime
from (select a.deriveno,
a.carno,
a.stagecode,
to_char(a.actualtime, 'yyyy-mm-dd hh24:mi:ss') as actualtime
from app_carstatus a, app_send b
where a.deriveno = b.deriveno
and b.status in ('0', '10')
and b.deriveno not in
(select deriveno from app_ridecost))
WHERE STAGECODE = '4') t2
on t1.deriveno = t2.deriveno

取3的查询,然后把对应的4行转列。但是我现在还有6.7也要行转列
...全文
211 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
-一个大坑 2017-08-07
  • 打赏
  • 举报
回复
引用 1 楼 wmxcn2000 的回复:
楼主提供一些测试数据,和预期结果,适当的做一些说明。

原表
stagecode=3 出发时间,stagecode=4到达时间(单程只有3.4,没有6.7)
stagecode=6回来时间,stagecode=7回来到达时间

现在效果
这是上面语句查的,写的只是单程的情况。

预期结果
原表中有6.7就取6.7的值给begintime1, endtime1,没有就为空
S_Pony 2017-08-07
  • 打赏
  • 举报
回复
用 case when 最简单 当在什么时候执行什么事
mayanzs 2017-08-07
  • 打赏
  • 举报
回复
select * from yourtable pivot ( min(actualtime) as time for (stagecode) in (3 as begin,4 as end,6 as begin1,7 as end1)) order by deriveno;
卖水果的net 2017-08-04
  • 打赏
  • 举报
回复
楼主提供一些测试数据,和预期结果,适当的做一些说明。

679

社区成员

发帖
与我相关
我的任务
社区描述
智能路由器通常具有独立的操作系统,包括OpenWRT、eCos、VxWorks等,可以由用户自行安装各种应用,实现网络和设备的智能化管理。
linuxpython 技术论坛(原bbs)
社区管理员
  • 智能路由器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧