我真的陷入了UNION ALL联盟,下面是Senario
如果我单独运行此查询,我会得到以下结果
1) Select salary as result from employee
where empno = '111628548' and seqno = 4
order by seqno Desc
result
------
$7000
$3000
2) Select descofemp as result from empdetail
where empno = '111628548' and seqno = 4
order by seqno Desc
result
------
very good employee
good employee
上面的都会产生字符串,所以列是相同的类型。 但当我加入上面两个查询与联盟时,我得到类似
的东西With s1 as (Select salary from employee
where empno = '111628548' and seqno = 4
order by seqno Desc),
s2 as (Select descofemp from empdetail
where empno = '111628548' and seqno = 4
order by seqno Desc
)
select * from s1
Union ALL
select * from s2
Result
------
$3000
$7000
very good employee
good employee
你可以在这里看到工资的顺序已经改变了,我尝试了很多东西,但是我无法理解为什么订单在UNION ALL中发生变化,是否有人知道或面对这个问题如果是的话请你分享一些亮点如何解决它。
提前谢谢。
此致 莫纳
答案 0 :(得分:3)
Union 不保证结果的排序。但是,您可以添加订购信息并使用:
With s1 as (Select salary as col, row_number() over (order by seqno desc) as seqnum
from employee
where empno = '111628548' and seqno = 4
),
s2 as (Select descofemp as col , row_number() over (order by seqno desc) as seqnum
from empdetail
where empno = '111628548' and seqno = 4
)
select col
from (select * from s1
Union ALL
select * from s2
) t
order by seqnum