Sql server 如果对第一个表的查询未返回任何记录,则从第二个表返回记录

Sql server 如果对第一个表的查询未返回任何记录,则从第二个表返回记录,sql-server,Sql Server,目标是从表address\u master address\u master中返回id\u num和county的值,如果未使用以下地址历史查询返回id\u num和county值,则每个id都有一条记录: 选择不同的a.id\u num,a.county 来自地址(历史a) 内部联接选择id\u num,MINarchive\u job\u tim作为MaxEnrollDate 从地址到历史 按id\u num b分组 在a.id\u num=b.id\u num和a.archive\u jo

目标是从表address\u master address\u master中返回id\u num和county的值,如果未使用以下地址历史查询返回id\u num和county值,则每个id都有一条记录:

选择不同的a.id\u num,a.county 来自地址(历史a) 内部联接选择id\u num,MINarchive\u job\u tim作为MaxEnrollDate 从地址到历史 按id\u num b分组 在a.id\u num=b.id\u num和a.archive\u job\u tim=b.MaxEnrollDate上 其中addr_cde='*LHP'

样本数据:

表地址\u历史记录包含历史更改,每个id可以有多个记录

id_num | county | archive_job_tim -------|--------|---------------- 123 |012 |10/17/2001 10:48:38 123 |NULL |10/17/2001 09:50:02 123 |042 |11/17/2003 08:22:01 134 |NULL |12/10/2005 02:14:23 145 |534 |9/27/1996 00:00:00 表地址\u master仅包含每个id的最新记录

id_num | county | archive_job_tim -------|--------|----------------- 123 |563 |12/22/2015 10:29:01 134 |734 |2/23/2005 07:21:15 145 |943 |10/22/1996 06:24:13 168 |012 |6/5/2017 08:01:22 197 |NULL |7/1/2017 10:16:02 查询结果应为:

id_num | county -------|-------- 123 |012 (because it is the earliest record with a county for this id in address_history) 134 |734 (because the only record(s) in address_history has no county, returns record from address_master) 145 |534 (because it is the earliest record with a county for this id in address_history) 168 |012 (because no record exists in address_history for this id) 197 |NULL (because no record exists in address_history for this id)
感谢您的帮助。谢谢。

我想您的查询如下:

Select top (1) with ties * from
(
    Select *,'am' as Note from address_master
    union all
    Select *,'ah' as Note from address_history
) a where (a.Note = 'ah' and a.county is not null) or a.Note = 'am'
Order by Row_Number() over(partition by id_num order by archive_job_tim)
+--------+--------+-------------------------+------+
| id_num | county |     archive_job_tim     | Note |
+--------+--------+-------------------------+------+
|    123 | 12     | 2001-10-17 10:48:38.000 | ah   |
|    134 | 734    | 2005-02-23 07:21:15.000 | am   |
|    145 | 534    | 1996-09-27 00:00:00.000 | ah   |
|    168 | 12     | 2017-06-05 08:01:22.000 | am   |
|    197 | NULL   | 2017-07-01 10:16:02.000 | am   |
+--------+--------+-------------------------+------+
输出如下:

Select top (1) with ties * from
(
    Select *,'am' as Note from address_master
    union all
    Select *,'ah' as Note from address_history
) a where (a.Note = 'ah' and a.county is not null) or a.Note = 'am'
Order by Row_Number() over(partition by id_num order by archive_job_tim)
+--------+--------+-------------------------+------+
| id_num | county |     archive_job_tim     | Note |
+--------+--------+-------------------------+------+
|    123 | 12     | 2001-10-17 10:48:38.000 | ah   |
|    134 | 734    | 2005-02-23 07:21:15.000 | am   |
|    145 | 534    | 1996-09-27 00:00:00.000 | ah   |
|    168 | 12     | 2017-06-05 08:01:22.000 | am   |
|    197 | NULL   | 2017-07-01 10:16:02.000 | am   |
+--------+--------+-------------------------+------+

谢谢你,Kandasamy先生,但是当我尝试在Excel连接中的命令文本中使用它时,我遇到了一个错误,我通常在Excel连接中插入SQL以生成连接的表。它显示:[Microsoft][SQL Server Native Client 10.0][SQL Server]关键字“from”附近的语法不正确。更新的查询在Overhank you中缺少一个括号。查询现在正在生成结果,但是,它只返回来自address_master的行,并且应该生成address_历史记录中的许多id号的记录。您能提供示例输入和输出数据吗?谢谢。对我在上面添加了示例输入和输出数据。我试着按照SOF编辑帮助中所示的表格格式发布它,但它看起来不像一张表格…我道歉。