我想编写如下查询:
select top 1 * from table_A
Join table B ---some more table is possible
where conditon_1
order by create_date
union all
select top 1 * from table B
---some more table is possible
where conditon_2
order by create_date
因此,我需要根据不同的条件从两个选择查询中获取最新记录,然后进行全部合并。
答案 0 :(得分:1)
您不能直接这样做。将CTE
用于第2个查询
with
cte1 as
(
select top 1 * from table_A
Join table B ---some more table is possible
where conditon_1
order by create_date
),
cte2 as
(
select top 1 * from table B
---some more table is possible
where conditon_2
order by create_date
)
select * from cte1
union all
select * from cte2
答案 1 :(得分:0)
您可以按照以下说明重新编写查询
SELECT * FROM
(select top 1 * from table_A
Join table B ---some more table is possible
where conditon_1
order by create_date) as t1
UNION ALL
SELECT * FROM
(select top 1 * from table B
---some more table is possible
where conditon_2
order by create_date) as t2