实现:
表a和表b是2个不同的表,通过字段1关联,分别统计的结果是字段2和字段3(字段2和字段3是不同的字段,比如一个是物料数一个是任务数),将统计的结果分别排序,然后再按照排序的结果分别赋予顺序权重值1-6,然后根据字段1将2者合并求出最后的权重顺序。请高手帮忙啊,如何通过存储过程或sql实现???(注意:要求的结果不能直接是两者合并排序,而是各自排序后求得顺序权重值后再合并排序)
表a
字段1 字段2
a 23
b 16
c 32
d 0
e 26
f 39
表a查询结果排序:
字段1 字段2
d 0
b 16
a 23
e 26
c 32
f 39
表b
字段1 字段3
a 10
b 13
c 23
d 11
e 0
f 28
表b查询结果排序:
字段1 字段3
e 0
a 10
d 11
b 13
c 23
f 28
要求结果:
根据排序后的结果将字段1按顺序赋予权重值(权重值是1-6),从小到大:
a表字段1排序赋予权重后:
字段1 权重
d 1
b 2
a 3
e 4
c 5
f 6
B表字段1排序赋予权重后:
字段1 权重
e 1
a 2
d 3
b 4
c 5
f 6
然后将两个结果合并求和排序,按从小到大:
结果如下:
d 4
a 5
e 5
b 6
c 10
f 12
主要是为了求这个排序结果. 求指导,谢谢。。
4 个解决方案
#1
with a as
(
select 'a' col1, 23 col2 from dual union all
select 'b' col1, 16 col2 from dual union all
select 'c' col1, 32 col2 from dual union all
select 'd' col1, 0 col2 from dual union all
select 'e' col1, 26 col2 from dual union all
select 'f' col1, 39 col2 from dual
)
, b as
(
select 'a' col1, 10 col2 from dual union all
select 'b' col1, 13 col2 from dual union all
select 'c' col1, 23 col2 from dual union all
select 'd' col1, 11 col2 from dual union all
select 'e' col1, 0 col2 from dual union all
select 'f' col1, 28 col2 from dual
)
, a1 as
(
select rownum col3, col1, col2 from (select col1, col2 from a order by col2) a0
)
, b1 as
(
select rownum col3, col1, col2 from (select col1, col2 from b order by col2) b0
)
select a1.col1, a1.col3+b1.col3
from a1, b1
where a1.col1=b1.col1
order by a1.col3+b1.col3
#2
简化到一句:
with a as
(
select 'a' col1, 23 col2 from dual union all
select 'b' col1, 16 col2 from dual union all
select 'c' col1, 32 col2 from dual union all
select 'd' col1, 0 col2 from dual union all
select 'e' col1, 26 col2 from dual union all
select 'f' col1, 39 col2 from dual
)
, b as
(
select 'a' col1, 10 col2 from dual union all
select 'b' col1, 13 col2 from dual union all
select 'c' col1, 23 col2 from dual union all
select 'd' col1, 11 col2 from dual union all
select 'e' col1, 0 col2 from dual union all
select 'f' col1, 28 col2 from dual
)
select a1.col1, a1.col3 + b1.col3
from (select rownum col3, col1, col2 from (select col1, col2 from a order by col2) a0) a1
, (select rownum col3, col1, col2 from (select col1, col2 from b order by col2) b0) b1
where a1.col1=b1.col1
order by a1.col3 + b1.col3
#3
大哥厉害啊,貌似有用,我下午测测
#4
谢谢,我测试通过了。
#1
with a as
(
select 'a' col1, 23 col2 from dual union all
select 'b' col1, 16 col2 from dual union all
select 'c' col1, 32 col2 from dual union all
select 'd' col1, 0 col2 from dual union all
select 'e' col1, 26 col2 from dual union all
select 'f' col1, 39 col2 from dual
)
, b as
(
select 'a' col1, 10 col2 from dual union all
select 'b' col1, 13 col2 from dual union all
select 'c' col1, 23 col2 from dual union all
select 'd' col1, 11 col2 from dual union all
select 'e' col1, 0 col2 from dual union all
select 'f' col1, 28 col2 from dual
)
, a1 as
(
select rownum col3, col1, col2 from (select col1, col2 from a order by col2) a0
)
, b1 as
(
select rownum col3, col1, col2 from (select col1, col2 from b order by col2) b0
)
select a1.col1, a1.col3+b1.col3
from a1, b1
where a1.col1=b1.col1
order by a1.col3+b1.col3
#2
简化到一句:
with a as
(
select 'a' col1, 23 col2 from dual union all
select 'b' col1, 16 col2 from dual union all
select 'c' col1, 32 col2 from dual union all
select 'd' col1, 0 col2 from dual union all
select 'e' col1, 26 col2 from dual union all
select 'f' col1, 39 col2 from dual
)
, b as
(
select 'a' col1, 10 col2 from dual union all
select 'b' col1, 13 col2 from dual union all
select 'c' col1, 23 col2 from dual union all
select 'd' col1, 11 col2 from dual union all
select 'e' col1, 0 col2 from dual union all
select 'f' col1, 28 col2 from dual
)
select a1.col1, a1.col3 + b1.col3
from (select rownum col3, col1, col2 from (select col1, col2 from a order by col2) a0) a1
, (select rownum col3, col1, col2 from (select col1, col2 from b order by col2) b0) b1
where a1.col1=b1.col1
order by a1.col3 + b1.col3
#3
大哥厉害啊,貌似有用,我下午测测
#4
谢谢,我测试通过了。