Oracle行转列+排序

--1.删除临时表
drop table biz_bus_station_direct_0711;
--2.将站点数据等放入临时表
create table biz_bus_station_direct_0711 as
select ls.line_no line_no,
       bb.line_name line_name,
       t1.line_direct line_direct,
       s.station_id station_id,
       s.station_name station_name,
       t.point_num point_num_now,
       s.location_x x_now,
       s.location_y y_now,
       t1.point_num point_num_last,
       t1.point_x x_last,
       t1.point_y y_last,
       s.location_x - t1.point_x x_value,
       s.location_y - t1.point_y y_value,
       0 direction,
       0 rn
  from biz_bus_station s
  join biz_bus_line_station ls
    on ls.station_id = s.station_id
  left join biz_bus_line bb on ls.line_no=bb.line_no
  left join biz_bus_line_distance t
    on t.station_id = s.station_id
   and t.line_no = ls.line_no
  left join biz_bus_line_distance t1
    on t1.line_no = t.line_no
   and t1.point_num + 1 = t.point_num
   and t.line_direct = t1.line_direct;
/*select ls.line_no line_no,
       s.station_id station_id,
       s.station_name station_name,
       t.point_num point_num_now,
       s.location_x x_now,
       s.location_y y_now,
       t1.point_num point_num_last,
       t1.point_x x_last,
       t1.point_y y_last,
       s.location_x - t1.point_x x_value,
       s.location_y - t1.point_y y_value,
       0 direction,
       0 rn
  from biz_bus_station s
  join biz_bus_line_station ls
    on ls.station_id = s.station_id
  left join biz_bus_line_distance t
    on t.station_id = s.station_id
   and t.line_no = ls.line_no
  left join biz_bus_line_distance t1
    on t1.line_no = t.line_no
   and t1.point_num + 1 = t.point_num
   and t.line_direct = t1.line_direct;*/
 --where s.station_name=‘安徽工程大学-2‘
 --order by ls.line_no, t.point_num;
--3.新增途径站点字段
ALTER TABLE biz_bus_station_direct_0711 ADD Station_DIRECT CLOB; 

--4.创建相关索引
create index idx_bus_station_line_no on biz_bus_station_direct_0711(line_no,station_id);
--5.给行号赋值
begin
  for t2 in (select t1.line_no,t1.station_id,rank() over(partition by replace(replace(t1.station_name,‘-1‘,‘‘),‘-2‘,‘‘) order by t1.line_no,t1.station_id) rn from biz_bus_station_direct_0711 t1) loop
    update biz_bus_station_direct_0711 t
    set t.rn=t2.rn
    where t.line_no=t2.line_no
    and t.station_id=t2.station_id;
    commit;
  end loop;
end;
--6.根据行号,逐行取值,赋方向值
/*cosθ=向量a.向量b/|向量a|×|向量b|
=(x1x2+y1y2)/[√(x12+y12)*√(x22+y22)]
夹角小于180度,则cosθ大于0,表示当前计算的车辆与RN=1的车辆方向一致
*/
begin
  for t2 in (select replace(replace(t1.station_name,‘-1‘,‘‘),‘-2‘,‘‘) station_name,t1.x_value,t1.y_value,t1.rn from biz_bus_station_direct_0711 t1 where t1.rn=1) loop
    update biz_bus_station_direct_0711 t
    set t.direction=case when t.x_value!=0 and t.y_value!=0 and t2.x_value!=0 and t2.y_value!=0 then (case when (t2.x_value*t.x_value+t2.y_value*t.y_value)/(SQRT(t2.x_value*t2.x_value+t2.y_value*t2.y_value)*SQRT(t.x_value*t.x_value+t.y_value*t.y_value))>0 then 1 else -1 end) else 1 end
    where t.rn <=(select max(rn) from biz_bus_station_direct_0711 where replace(replace(station_name,‘-1‘,‘‘),‘-2‘,‘‘)=t2.station_name)
    and replace(replace(t.station_name,‘-1‘,‘‘),‘-2‘,‘‘)=t2.station_name;
    commit;
  end loop;
end;

--7.根据站点给各个线路赋值途径站点(行转列+排序)
begin
  for t2 in (
    select A.line_no,A.line_direct,max(KEY) Station_DIRECT from (
select t.line_no,t.line_direct,
WMSYS.WM_CONCAT(t.station_name) OVER(PARTITION BY t.line_no,t.line_direct ORDER BY t.line_direct,t.point_num) KEY,
row_number() over(PARTITION BY t.line_no,t.line_direct ORDER BY t.line_direct,t.point_num) rs
from (
select t1.line_no,t1.line_direct,t1.point_num,t2.station_name from BIZ_BUS_LINE_DISTANCE t1
join biz_bus_station t2 on t1.station_id=t2.station_id
order by t1.line_direct,t1.point_num) t
) A
group by A.line_no,A.line_direct
    ) loop
    update biz_bus_station_direct_0711 bbs
    set bbs.station_direct=t2.station_direct
    where bbs.line_no=t2.line_no and bbs.line_direct=t2.line_direct;
    commit;
  end loop;
end;

存储过程:

create or replace procedure usp_bus_station_direct_update is
/*
  --名称:公交站点方向同步
  --功能:将各个公交站点的途径公交车辆相同方向置1,不同方向置-1
  --作者:赛太岁
  --编写时间:2016-07-12
*/

begin
--删除中间表数据
delete from biz_bus_station_direct;

--将公交站点等数据全量插入中间表
insert into biz_bus_station_direct
select ls.line_no line_no,
       bb.line_name line_name,
       t1.line_direct line_direct,
       s.station_id station_id,
       s.station_name station_name,
       t.point_num point_num_now,
       s.location_x x_now,
       s.location_y y_now,
       t1.point_num point_num_last,
       t1.point_x x_last,
       t1.point_y y_last,
       s.location_x - t1.point_x x_value,
       s.location_y - t1.point_y y_value,
       0 direction,
       0 rn,
       ‘‘ STATION_DIRECT,
       sysdate create_time
  from biz_bus_station s
  join biz_bus_line_station ls
    on ls.station_id = s.station_id
  left join biz_bus_line bb on ls.line_no=bb.line_no
  left join biz_bus_line_distance t
    on t.station_id = s.station_id
   and t.line_no = ls.line_no
  left join biz_bus_line_distance t1
    on t1.line_no = t.line_no
   and t1.point_num + 1 = t.point_num
   and t.line_direct = t1.line_direct;

--给行号赋值
  for t2 in (select t1.line_no,t1.station_id,rank() over(partition by replace(replace(t1.station_name,‘-1‘,‘‘),‘-2‘,‘‘) order by t1.line_no,t1.station_id) rn from biz_bus_station_direct t1) loop
    update biz_bus_station_direct t
    set t.rn=t2.rn
    where t.line_no=t2.line_no
    and t.station_id=t2.station_id;
  end loop;

--根据行号,逐行取值,赋方向值
/*cosθ=向量a.向量b/|向量a|×|向量b|
=(x1x2+y1y2)/[√(x12+y12)*√(x22+y22)]
夹角小于180度,则cosθ大于0,表示当前计算的车辆与RN=1的车辆方向一致
*/

  for t2 in (select replace(replace(t1.station_name,‘-1‘,‘‘),‘-2‘,‘‘) station_name,t1.x_value,t1.y_value,t1.rn from biz_bus_station_direct t1 where t1.rn=1) loop
    update biz_bus_station_direct t
    set t.direction=case when t.x_value!=0 and t.y_value!=0 and t2.x_value!=0 and t2.y_value!=0 then (case when (t2.x_value*t.x_value+t2.y_value*t.y_value)/(SQRT(t2.x_value*t2.x_value+t2.y_value*t2.y_value)*SQRT(t.x_value*t.x_value+t.y_value*t.y_value))>0 then 1 else -1 end) else 1 end
    where t.rn <=(select max(rn) from biz_bus_station_direct where replace(replace(station_name,‘-1‘,‘‘),‘-2‘,‘‘)=t2.station_name)
    and replace(replace(t.station_name,‘-1‘,‘‘),‘-2‘,‘‘)=t2.station_name;
  end loop;

--根据站点给各个线路赋值途径站点

  for t2 in (
    select A.line_no,A.line_direct,max(KEY) Station_DIRECT from (
select t.line_no,t.line_direct,
WMSYS.WM_CONCAT(t.station_name) OVER(PARTITION BY t.line_no,t.line_direct ORDER BY t.line_direct,t.point_num) KEY,
row_number() over(PARTITION BY t.line_no,t.line_direct ORDER BY t.line_direct,t.point_num) rs
from (
select t1.line_no,t1.line_direct,t1.point_num,t2.station_name from BIZ_BUS_LINE_DISTANCE t1
join biz_bus_station t2 on t1.station_id=t2.station_id
order by t1.line_direct,t1.point_num) t
) A
group by A.line_no,A.line_direct
    ) loop
    update biz_bus_station_direct bbs
    set bbs.station_direct=t2.station_direct
    where bbs.line_no=t2.line_no and bbs.line_direct=t2.line_direct;
  end loop;

  commit;

--异常处理
exception
  when others then
    rollback;
end usp_bus_station_direct_update;
时间: 2024-11-05 20:49:20

Oracle行转列+排序的相关文章

oracle行转列

针对oracle数据查询的数据,行转列 1.wm_concat函数: 例一: select c1,c2,wm_concat(c3) from T where.... group by c1,c2 查询结果自动用","分割 例二: select c1,c2,wm_concat(c3)over(partition by ..order by..) from T where.... 其中,partition用来分组, order用来排序 2.sys_connect_by_path函数: 这个

oracle 行转列 分析函数

oracle 行转列 首先看一下源数据: 方法一:WM_CONCAT group by 这个方法没有问题. SELECT CODE_TS, WMSYS.WM_CONCAT(S_NUM + 1 || ':' || ELEMENT) ELEMENT FROM T_MERCH_ELEMENT where code_ts='020745' group by CODE_TS; 得到的结果: 上面大家可能会发现序号没有按顺序排列下来.如果没有要求,就这样就可以了.如果要排序看方法二. 方法二:WM_CONC

C语言之基本算法42—矩阵转置及按行按列排序

//矩阵转置 按行按列排序 /* ================================================================== 题目:输入m*n矩阵,按行升序排列输出. 输入: 4 3 5 6 2 9 8 1 2 8 7 1 2 3 8 输出: 2 3 4 5 6 1 2 8 8 9 1 2 3 7 8 ================================================================== */ #includ

Oracle行转列、列转行的Sql语句总结(转)

多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_userselect id||username str from app_user 字符串转多列 实际上就是拆分字符串的问题,可以使用 substr.instr.regexp_substr函数方式 字符串转多行 使用union all函数等方式 wm_concat函数 首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以",&quo

oracle 行转列、列转行

最近做数据处理,经常遇到需要行转列.列转行的场景,记录个非常简单实用的oracle  列转行.行转的列方法 1.行转列,基础数据如下 做行转列处理 处理SQL select user_name,max(date_201501) as date_201501,max(date_201502),max(date_201503),max(date_201504) from (select t.user_name,case when t.acct_date = '201501' then t.flow

python 按二维数组的某行或列排序 (numpy lexsort)

lexsort支持对数组按指定行或列的顺序排序:是间接排序,lexsort不修改原数组,返回索引. 默认按最后一行元素有小到大排序, 返回最后一行元素排序后索引所在位置. 设数组a, 返回的索引ind, a可以是1维或2维数组,ind返回的是一维数组 对于一维数组, a[ind]就是排序后的数组. 对于二维数组下面会详细举例. import numpy as np >>> a array([[ 2,  7,  4,  2], [35,  9,  1,  5], [22, 12,  3, 

Oracle 行转列、列转行 的Sql语句总结

参考文章:http://blog.csdn.net/tianlesoftware/article/details/4704858 多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_user select id||username str from app_user 字符串转多列 实际上就是拆分字符串的问题,可以使用 substr.instr.regexp_substr函数方式 字符串转多行 使用union

ORACLE 行转列的通用过程

--测试数据create table rowtocol_test asselect 2009 year,1 month,'部门1' dept,50000 expenditure from dualunion all select 2009,2,'部门1',20000 from dualunion all select 2009,2,'部门1',30000 from dualunion all select 2010,1,'部门1',35000 from dualunion all select

Oracle行转列操作

有时候我们在展示表中数据的时候,需要将行转为列来显示,如以下形式: 原表结构展示如下:---------------------------产品名称    销售额     季度---------------------------奶酪          50     第一季度奶酪          60     第二季度啤酒          50     第二季度啤酒          80     第四季度--------------------------- 现在需要将上面的原表结构转换为