【发布时间】:2019-12-16 01:56:15
【问题描述】:
主表
将显示所有可用 DetailsTable 的记录,Ops_Stat=A
的记录除外+---------+----------+-------+---------+---------+---------------------+-------------------------------------------------------+
| Row_Num | Ops_Stat | RunID | Cust_ID | Name | Date | |
+---------+----------+-------+---------+---------+---------------------+-------------------------------------------------------+
| 1 | U | A123 | AAA1111 | Hulk | 2019-09-01 01.05.01 | < No need capture this row in expected result |
| 2 | U | B456 | AAA1111 | Hulk | 2019-07-01 01.04.11 | |
| 3 | U | C789 | AAA1111 | IronMan | 2019-05-01 01.03.01 | |
| 4 | A | D123 | AAA1111 | Spidey | 2019-01-01 01.02.01 | < Need capture this row and show in expected result |
| 1 | A | U489 | BBB2222 | Marvel | 2019-10-10 02.02.02 | < no need get this if not match of Cust_ID in Details |
| 2 | A | Y789 | BBB2222 | Marvel | 2019-09-10 02.02.02 | < no need get this if not match of Cust_ID in Details |
+---------+----------+-------+---------+---------+------------+----------------------------------------------------------------+
详情
-如果字段(名称、原因和更多列)有更新,则只会在此表上显示
-只需要捕获Status='Comp'
的记录+---------+-------+---------+---------+--------+---------------------+--------+---------------------------+
| Row_Num | RunID | Cust_ID | Name | Reason | Date | Status | |
+---------+-------+---------+---------+--------+---------------------+--------+---------------------------+
| 1 | A123 | AAA1111 | Hulk | | 2019-09-01 01.05.40 | Pend | << Ignore status = 'Pend' |
| 2 | B456 | AAA1111 | Hulk | A | 2019-07-01 01.04.20 | Comp | |
| 3 | C789 | AAA1111 | IronMan | A | 2019-05-01 01.03.50 | Comp | |
+---------+-------+---------+---------+--------+---------------------+--------+---------------------------+
预期结果
+----------+---------+---------+---------+--------+-------------------+--------+--------------------------------------+
| Ops_Stat | RunID | Cust_ID | Name | Reason | Date | Status | |
+----------+---------+---------+---------+--------+-------------------+--------+--------------------------------------+
| U | B456 | AAA1111 | Hulk | A | 20190701 01.04.20 | Comp | << take every field in Details table |
| U | C789 | AAA1111 | IronMan | A | 20190501 01.03.50 | Comp | << take every field in Details table |
| A | D123 | AAA1111 | Spidey | | 20190101 01.02.01 | | << take every field in Master table |
+----------+---------+---------+---------+--------+-------------------+--------+--------------------------------------+
- Cust_ID 是连接 Master 和 Details 表的关键
- 在预期结果中,需要包含 Ops_Stat = A 的记录;以及是否在 Master 和 Details 表中匹配 Cust_ID
- 在预期结果中,如果Details表中没有Cust_ID匹配,则无需获取Ops_Stat=A。
- 在预期结果中,需要排除Details table status = Pend的记录
- 请注意日期时间。当 Ops_stat=A 时,取 MasterTable 中的 datetime,但当 Ops_stat 不等于 A 时,取 DetailsTable 中的 datetime
尝试过:
select *
from Master m
left join Details d on m.Cust_ID = d.Cust_ID
where d.Status = 'Comp'
根据上面的尝试,我的发现如下
- Master 表中的所有记录(无论状态 = pend 还是 com,明细表中没有匹配键,明细表中的状态都变为“Comp”而不是 Pend + Comp
您可以在这里尝试您的代码:SQL Fiddle
【问题讨论】:
-
您是否已经为此编写了任何查询?你在这方面面临什么问题?你能在这里分享那个查询吗?
-
您的问题到底是什么?您已经列出了一系列要求,但没有提出任何要求。 你的问题是什么?你尝试过什么,为什么没有成功?另外,你是什么意思你“处理10mils”?什么是密耳?
-
@ChetanRanpariya 为我的尝试更新如上。
-
执行
OUTER JOIN,然后在不处理NULL值的情况下从WHERE中的连接表中引用a 列,将其转换为隐式INNER JOIN -
@Larnu 对不起,我不明白你的意思:(
标签: sql sql-server join sql-server-2012 subquery