加入3tables; select 来自第三 table 行,其中包含最新日期的特定字符串
Join 3 tables; select from third table row which contains specific string by latest date
我有 3 个 table,其中我需要从第 3 个 table 搜索这个特定字符串 '%sell back%' 并获取 latest 条目来自 table,3 个 table 如下:
Table#1:客户
|---------------|---------------|
| Customer ID | CustomerName |
|---------------|---------------|
| 1234 | Johnathan |
---------------------------------
Table#2:问题
|---------------|---------------|----------------------|---------------|
| Problem ID | CustomerID | Problem Description | Date Reported |
|---------------|---------------|----------------------|---------------|
| 3203494 | 1234 | Needs Appointment | 2019-08-01 |
------------------------------------------------------------------------
| 3178766 | 1234 | Sell Back Customer | 2019-08-12 |
------------------------------------------------------------------------
Table#3:问题事件
|---------------|---------------|----------------------|---------------|
|ProblemEventID | Problem ID | Event Reason | Event Date |
|---------------|---------------|----------------------|---------------|
| 1926144 | 3178766 | Reported | 2019-08-12 |
------------------------------------------------------------------------
| 2022750 | 3178766 | sell back | 2019-08-13 |
------------------------------------------------------------------------
| 2022751 | 3178766 | Accepted as sell back| 2019-08-26 |
------------------------------------------------------------------------
| 2022899 | 3178766 | Finalized | 2019-08-31 |
------------------------------------------------------------------------
我查询的结果如下:
|---------------|---------------|------------|-----------------------|------------|
| Customer ID | CustomerName | Problem Id | Event Reason | Event Date |
|---------------|---------------|------------|-----------------------|------------|
| 1234 | Johnathan | 3178766 | Accepted as sell back | 2019-08-26 |
---------------------------------------------------------------------|------------|
提前感谢您的帮助。
如果您想要整个 table 的最新 "sell back" 活动,那么您可以加入、排序和限制:
select top (1)
c.customer_id,
c.customer_name
p.problem_id,
pe.event_reason,
pe.event_date
from customer c
inner join problem p on p.customer_id = c.customer_id
inner join problem_event pe on pe.problem_id = p.problem_id
where pe.event_reason like '%sell back%'
order by pe.event_date
还有其他方法可以理解您的问题。假设您想要每个客户 的最新 "sell back" 事件 ,那么这是一个 greatest-n-per-group 的问题。您可以使用 row_number()
:
select *
from (
select
c.customer_id,
c.customer_name
p.problem_id,
pe.event_reason,
pe.event_date,
row_number() over(partition by c.customer_id order by pe.event_date desc) rn
from customer c
inner join problem p on p.customer_id = c.customer_id
inner join problem_event pe on pe.problem_id = p.problem_id
where pe.event_reason like '%sell back%'
) t
where rn = 1
order by c.customer_id
每个问题 返回最新 "sell back" 事件的练习 ,这是对您的问题的最后可能解释,留给 reader!
我有 3 个 table,其中我需要从第 3 个 table 搜索这个特定字符串 '%sell back%' 并获取 latest 条目来自 table,3 个 table 如下:
Table#1:客户
|---------------|---------------|
| Customer ID | CustomerName |
|---------------|---------------|
| 1234 | Johnathan |
---------------------------------
Table#2:问题
|---------------|---------------|----------------------|---------------|
| Problem ID | CustomerID | Problem Description | Date Reported |
|---------------|---------------|----------------------|---------------|
| 3203494 | 1234 | Needs Appointment | 2019-08-01 |
------------------------------------------------------------------------
| 3178766 | 1234 | Sell Back Customer | 2019-08-12 |
------------------------------------------------------------------------
Table#3:问题事件
|---------------|---------------|----------------------|---------------|
|ProblemEventID | Problem ID | Event Reason | Event Date |
|---------------|---------------|----------------------|---------------|
| 1926144 | 3178766 | Reported | 2019-08-12 |
------------------------------------------------------------------------
| 2022750 | 3178766 | sell back | 2019-08-13 |
------------------------------------------------------------------------
| 2022751 | 3178766 | Accepted as sell back| 2019-08-26 |
------------------------------------------------------------------------
| 2022899 | 3178766 | Finalized | 2019-08-31 |
------------------------------------------------------------------------
我查询的结果如下:
|---------------|---------------|------------|-----------------------|------------|
| Customer ID | CustomerName | Problem Id | Event Reason | Event Date |
|---------------|---------------|------------|-----------------------|------------|
| 1234 | Johnathan | 3178766 | Accepted as sell back | 2019-08-26 |
---------------------------------------------------------------------|------------|
提前感谢您的帮助。
如果您想要整个 table 的最新 "sell back" 活动,那么您可以加入、排序和限制:
select top (1)
c.customer_id,
c.customer_name
p.problem_id,
pe.event_reason,
pe.event_date
from customer c
inner join problem p on p.customer_id = c.customer_id
inner join problem_event pe on pe.problem_id = p.problem_id
where pe.event_reason like '%sell back%'
order by pe.event_date
还有其他方法可以理解您的问题。假设您想要每个客户 的最新 "sell back" 事件 ,那么这是一个 greatest-n-per-group 的问题。您可以使用 row_number()
:
select *
from (
select
c.customer_id,
c.customer_name
p.problem_id,
pe.event_reason,
pe.event_date,
row_number() over(partition by c.customer_id order by pe.event_date desc) rn
from customer c
inner join problem p on p.customer_id = c.customer_id
inner join problem_event pe on pe.problem_id = p.problem_id
where pe.event_reason like '%sell back%'
) t
where rn = 1
order by c.customer_id
每个问题 返回最新 "sell back" 事件的练习 ,这是对您的问题的最后可能解释,留给 reader!