Select 具有相同列的多个表

Select multiple tables with same column

我有两个 table,具有相同的列结构。 第一个 table 包含 2019 年的数据。 现在我想添加 2020 年和 2021 年以后的日期。 我怎样才能做到这一点? 联盟,但如何?

SELECT cast(cast( Replace(xy,',','.') as float) as datetime) xy
, [xy1]
, cast(xy2 as int) xy2
, [xy3]
, cast( Replace(xy4,',','.') as float) xy4
, [xy5]
, cast( Replace(xy6,',','.') as float) xy6
, cast( Replace([xy7],',','.') as float) [xy7]
, cast( Replace([xy8],',','.') as float) [xy8]
, cast( Replace([xy9],',','.') as float) [xy9]
, [xy10]
FROM 2019
WHERE xy7 not like '#%'

两个table都有这个结构。

2019
x| xy1| xy2 | xy3 etc etc etc
2020
x| xy1| xy2 | xy3 etc etc etc

你可以这样做:

SELECT cast(cast( Replace(xy,',','.') as float) as datetime) xy
, [xy1]
, cast(xy2 as int) xy2
, [xy3]
, cast( Replace(xy4,',','.') as float) xy4
, [xy5]
, cast( Replace(xy6,',','.') as float) xy6
, cast( Replace([xy7],',','.') as float) [xy7]
, cast( Replace([xy8],',','.') as float) [xy8]
, cast( Replace([xy9],',','.') as float) [xy9]
, [xy10]
FROM 2019
WHERE xy7 not like '#%'
UNION ALL
SELECT cast(cast( Replace(xy,',','.') as float) as datetime) xy
, [xy1]
, cast(xy2 as int) xy2
, [xy3]
, cast( Replace(xy4,',','.') as float) xy4
, [xy5]
, cast( Replace(xy6,',','.') as float) xy6
, cast( Replace([xy7],',','.') as float) [xy7]
, cast( Replace([xy8],',','.') as float) [xy8]
, cast( Replace([xy9],',','.') as float) [xy9]
, [xy10]
FROM 2020
WHERE xy7 not like '#%'

如果您不想重复,可以使用 UNION 而不是 UNION ALL

with cte
as
(
select * from 2019 WHERE xy7 not like '#%'
union all
select * from 2020 WHERE xy7 not like '#%'
union all
select * from 2021 WHERE xy7 not like '#%'
)
SELECT cast(cast( Replace(xy,',','.') as float) as datetime) xy
, [xy1]
, cast(xy2 as int) xy2
, [xy3]
, cast( Replace(xy4,',','.') as float) xy4
, [xy5]
, cast( Replace(xy6,',','.') as float) xy6
, cast( Replace([xy7],',','.') as float) [xy7]
, cast( Replace([xy8],',','.') as float) [xy8]
, cast( Replace([xy9],',','.') as float) [xy9]
, [xy10]
FROM cte