简体   繁体   English

Union SQL in DB2 SQL Query更新了第一个表的顺序

[英]Union all in DB2 SQL Query updates the order of fist table

I am really stuck in UNION ALL join, below is senario 我真的被困在UNION ALL加入,下面是Senario

if i ran this query indivisually i get below result 如果我单独运行此查询,我得到低于结果

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

above both result in string so column is same type. 上面两个都导致字符串,所以列是相同的类型。 but when i join above two query with union all i get something like 但当我加入以上两个查询与联盟所有我得到的东西

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

You can see here the order of salary has been changed, i tried verious things but i am not able to understand why the order is changing in UNION ALL, does anybody know or faced this issue if yes could you please share some light on how to resolve it. 你可以在这里看到工资的顺序已经改变,我尝试了很多东西,但我无法理解为什么订单在UNION ALL中发生变化,是否有人知道或面对这个问题如果是的话请你分享一些关于如何解决它。

Thank you in advance. 先感谢您。

Regards Mona 关心莫娜

Union does not guarantee the ordering of the results. 联盟保证结果的排序。 You can, however, add in the ordering information and use that: 但是,您可以添加订购信息并使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM