注册 登录
编程论坛 SQL Server论坛

SQL两表合并(两表列相同)

z345725862 发布于 2023-06-07 21:25, 686 次点击
select a.* from
    (select distinct htno,c_value,row_number()over(partition by htno order by samply_no desc) rn from BGTAMODLL3.T_ODS_TQMTJ04 where rec_creator='100' and c_value>0 and c_value<20) a
where a.rn='1'

union all

select * from
    (select distinct htno,c_value as dc_c,row_number()over(partition by htno order by sample_time desc) rn from BGTAMODLL3.T_ODS_TMMSM35  where htno not in (select distinct htno from a) and c_value>0 and c_value<1)
where rn='1'
order by htno

问题出在第二段程序 htno not in (select distinct htno from a) 这个地方,这个去掉程序可以实现,主要是第二个程序的htno和第一段是相关的,这种情况要怎么处理才能让两个表并在一起。
1 回复
#2
yiyanxiyin2023-07-06 13:21
htno not in (select distinct htno from a) 替换成下面的语句:
 htno not in (select htno from BGTAMODLL3.T_ODS_TQMTJ04 where rec_creator='100' and c_value>0 and c_value<20)

[此贴子已经被作者于2023-7-6 13:22编辑过]

1