1. 程式人生 > >Oracle行轉列+排序

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)/[√(x1²+y1²)*√(x2²+y2²)] 夾角小於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;