unionall用法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unionall用法相关的知识,希望对你有一定的参考价值。
参考技术A union all用法举例如下:SELECT * FROM
(SELECT top 1 left(convert(varchar(100),RECEIVE_TIME,108 ),5) receiveTime, \'0\' restoreTime,unit_code,unit_name
FROM T_FILE_RECEIVE_RECORD WHERE UNIT_CODE = \'123123\' and convert(char(100),RECEIVE_TIME,23)=\'2018-10-06\' ORDER BY RECEIVE_TIME DESC
) table1
UNION ALL
SELECT * FROM
(SELECT top 1 \'0\' receiveTime, left(convert(varchar(100),RESTORE_TIME,108 ),5) restoreTime,unit_code,unit_name
FROM T_FILE_RESTORE_RECORD WHERE UNIT_CODE = \'123123\' and convert(char(100),restore_time,23)=\'2018-10-06\' ORDER BY restore_time DESC
) table2
扩展资料
使用UNION(或者UNION ALL)语句时,如果UNION的\'两个结果集在单独排序后再拼接,则他们的ORDER BY是失效的。如果我们要进行排序有以下两种方法:将它们作为子查询再ORDER BY查询一次;在第一个结果集中不使用排序,且不用括号分隔,而在第二个结果集后使用ORDER BY。
with recursive用法
with recursive 则是一个递归的查询子句,他会把查询出来的结果再次代入到查询子句中继续查询。
with recursive d(n, fact) as (
values (1,2)
union all #合并
select n+1, (n+1)*fact from d where n < 5)
SELECT * from d;
递归过程如下:
n=1 fact=2
n=1,n<5: n=1+1=2,fact=(1+1)*2=4
n=2,n<5:n=2+1=3,fact=(2+1)*4=12
n=3,n<5:n=3+1=4,fact=(3+1)*12=48
n=4,n<5:n=4+1=5,fact=(4+1)*48=240
n=5 n>=5==stop
with recursive d(n, fact) as (
values (1,2)
union all
select n+2, (n+1)*fact from d where n < 5)
SELECT * from d;
递归过程如下:
n=1 fact=2
n=1,n<5: n=1+2=3,fact=(1+1)*2=4
n=3,n<5:n=3+2=5,fact=(3+1)*4=16
n=5 n>=5==stop
with recursive d(n, fact) as (
values (1,2)
union all
select n+2, (n+1)*fact from d where n < 5)
select sum(fact) from d;
sum(fact)=2+4+16=22
with recursive d(n, fact) as (
values (1,2)
union all
select n+2, (n+1)*fact from d where n < 5)
select sum(n) from d;
sum(n)=1+3+5=9
select * from company;
with recursive t(n) as (
values (10)
union all
select salary from company where salary < 20000
)
select * from t;
with recursive t(n) as (
values (10)
union all
select salary from company where salary < 20000
)
select sum(n) from t;
以上是关于unionall用法的主要内容,如果未能解决你的问题,请参考以下文章