繁体   English   中英

有什么方法可以根据与ID相关的“更改历史”表来识别某个时间段内是否有任何列值发生变化?

[英]Are there any way to identify whether any column value changed during a certain time period base on a "change history" table which related with an ID?

下面有两个数据集示例,第一个表包含与工单相关的所有数据,第二个表包含每个工单的更改历史记录。

历史表

+---------+--------+---------+-----------+-----------+
| WORK_ID |  TIME  |  FIELD  | OLD_VALUE | NEW_VALUE |
+---------+--------+---------+-----------+-----------+
| A2      | 09.02  | SALES   | 150       | 250       |
| A1      | 09.00  | STATUS  | CLOSED    | OPEN      |
| A1      | 08.55  | OWNER   | LISA      | DEBBY     |
| A2      | 08.54  | STATUS  | CLOSED    | OPEN      |
| A2      | 08.50  | STATUS  | OPEN      | CLOSED    |
| A1      | 08.45  | SALES   | 300       | 500       |
| A1      | 08.45  | STATUS  | OPEN      | CLOSED    |
| A2      | 08.40  | OWNER   | ROB       | ANDY      |
+---------+--------+---------+-----------+-----------+

工单表

+---------+---------+--------+-------+
| WORK_ID |  STATUS | OWNER  | SALES | 
+---------+---------+--------+-------+
| A1      | OPEN    | DEBBY  | 500   |
| A2      | OPEN    | ANDY   | 250   |
+---------+---------+--------+-------+

这两个表都通过 WORK_ID 列相互关联。 我的目标是确定在工单状态更改为“关闭”之后和更改为“打开”之前是否更改了任何字段。

我已经尝试使用 CASE 和 WHEN 以及this的解决方案,但我仍然无法仅隔离状态更改为 CLOSED(字段 STATUS 新值已关闭)到状态更改为 OPEN(字段 STATUS new值是开放的)。

我期待 output 表看起来像这样,

+---------+---------+--------+-------+-------------------+
| WORK_ID |  STATUS | OWNER  | SALES | CHANGED_ON_CLOSED |
+---------+---------+--------+-------+-------------------+
| A1      | OPEN    | DEBBY  | 500   | TRUE              |
| A2      | OPEN    | ANDY   | 250   | FALSE             |
+---------+---------+--------+-------+-------------------+

有人愿意帮我看一个 SQL 查询吗?

要合并这些表格,请 pivot 第一张表格。

如果您只需要查看关闭语句期间发生的情况:

With tbl as (
Select"A2" WORK_ID,"9,02" TIME,"SALES" FIELD,"150" OLD_VALUE,"250"NEW_VALUE
Union all Select"A1","9","STATUS","CLOSED","OPEN",
Union all Select"A1","8,55","OWNER","LISA","DEBBY",
Union all Select"A2","8,54","STATUS","CLOSED","OPEN",
Union all Select"A2","8,5","STATUS","OPEN","CLOSED",
Union all Select"A1","8,45","SALES","300","500",
Union all Select"A1","8,45","STATUS","OPEN","CLOSED",
Union all Select"A2","8,4","OWNER","ROB","ANDY",
)

SELECT
*,
last_value(if(Field="STATUS",new_value,null) ignore nulls) over win as status,
sum(if(Field="STATUS",1,0)) over win as status_cycle,

from tbl
qualify status="CLOSED" and Field!="STATUS"
window win as  (partition by WORK_ID order by time rows between unbounded preceding and current row)

order by 1,2

但我猜你需要在第一个表上使用 pivot 才能将它与第二个表连接起来。

With tbl as (
Select"A2" WORK_ID,"9,02" TIME,"SALES" FIELD,"150" OLD_VALUE,"250"NEW_VALUE
Union all Select"A1","9","STATUS","CLOSED","OPEN",
Union all Select"A1","8,55","OWNER","LISA","DEBBY",
Union all Select"A2","8,54","STATUS","CLOSED","OPEN",
Union all Select"A2","8,5","STATUS","OPEN","CLOSED",
Union all Select"A1","8,45","SALES","300","500",
Union all Select"A1","8,45","STATUS","OPEN","CLOSED",
Union all Select"A2","8,4","OWNER","ROB","ANDY",
),
helper as (
Select Time,WORK_ID,Owner,Status,Sales from tbl
pivot (any_value (new_value) for field in ("OWNER","STATUS","SALES") )
),

list as (
SELECT WORK_ID,time,
any_value(Owner) Owner,
any_value(Status) Status,
any_value(Sales) Sales,

from helper
group by 1,2)

Select 
WORK_ID,time,
last_value(Owner ignore nulls) over win as owner,
last_value(Status ignore nulls) over win as Status,
last_value(Sales ignore nulls) over win as Sales,
 from list
window win as  (partition by WORK_ID order by time rows between unbounded preceding and current row)
order by 2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM