简体   繁体   English

一个查询,显示表中列的更改

[英]A query which shows changes of a column from a table

i have table which include my product table's logs like that: 我有表格,包括我的产品表的日志:

process_time             product_id  product_type_id
04.07.2009 14:08:43      5           4
05.07.2009 15:08:43      5           4
06.07.2009 16:08:43      5           6
07.07.2009 16:08:43      5           6
08.07.2009 17:08:43      5           4
08.07.2009 18:08:43      5           4

I want to write a query which shows the changes of product_type_id. 我想写一个查询,显示product_type_id的变化。 For the example above, the result of my query should be like that: 对于上面的示例,我的查询结果应该是这样的:

process_time             product_id  product_type_id
04.07.2009 14:08:43      5           4
06.07.2009 16:08:43      5           6
08.07.2009 17:08:43      5           4

How can I write this query? 我该怎么写这个查询?

Like this: 像这样:

select * from
(select process_time, product_id, product_type_id
,lag(product_type_id) over (partition by product_id order by process_time) as prevrow
,lead(product_type_id) over (partition by product_id order by process_time) as nextrow
from products )
where nextrow <> product_type_id or nextrow is null;

For all who like to see how this works: 对于所有喜欢看它是如何工作的人:

create table products (process_time  timestamp, product_id number, product_type_id number);

insert into products values (to_date('2009-07-04 14:08:43','YYYY-MM-DD hh24:mi:ss'),5,4);
insert into products values (to_date('2009-07-05 15:08:43','YYYY-MM-DD hh24:mi:ss'),5,4);
insert into products values (to_date('2009-07-06 16:08:43','YYYY-MM-DD hh24:mi:ss'),5,6);
insert into products values (to_date('2009-07-07 16:08:43','YYYY-MM-DD hh24:mi:ss'),5,6);
insert into products values (to_date('2009-07-08 17:08:43','YYYY-MM-DD hh24:mi:ss'),5,4);
insert into products values (to_date('2009-07-08 18:08:43','YYYY-MM-DD hh24:mi:ss'),5,4);

commit;

select process_time, product_id, product_type_id
,lag(product_type_id) over (partition by product_id order by process_time) as prevrow
,lead(product_type_id) over (partition by product_id order by process_time) as nextrow
from products 
order by process_time;

select * from
(select process_time, product_id, product_type_id
,lag(product_type_id) over (partition by product_id order by process_time) as prevrow
,lead(product_type_id) over (partition by product_id order by process_time) as nextrow
from products )
where nextrow <> product_type_id or nextrow is null;

commit;

drop table products;

Executed we get: 执行我们得到:

Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
Commit complete.

PROCESS_TIME                    PRODUCT_ID PRODUCT_TYPE_ID    PREVROW    NEXTROW
------------------------------- ---------- --------------- ---------- ----------
04-JUL-09 02.08.43.000000 PM             5               4                     4
05-JUL-09 03.08.43.000000 PM             5               4          4          6
06-JUL-09 04.08.43.000000 PM             5               6          4          6
07-JUL-09 04.08.43.000000 PM             5               6          6          4
08-JUL-09 05.08.43.000000 PM             5               4          6          4
08-JUL-09 06.08.43.000000 PM             5               4          4           


6 rows selected.

PROCESS_TIME                    PRODUCT_ID PRODUCT_TYPE_ID    PREVROW    NEXTROW
------------------------------- ---------- --------------- ---------- ----------
05-JUL-09 03.08.43.000000 PM             5               4          4          6
07-JUL-09 04.08.43.000000 PM             5               6          6          4
08-JUL-09 06.08.43.000000 PM             5               4          4           


3 rows selected.
Commit complete.
Table dropped.

Use the LAG analytic function to find the prior value of the product_type_id column. 使用LAG分析函数查找product_type_id列的先前值。 If the current and prior values are different then that should be the row that you want. 如果当前值和先前值不同,则应该是您想要的行。 For the first row, the LAG function will return null because there is no prior row so you will also need to test for that. 对于第一行, LAG函数将返回null,因为没有前一行,因此您还需要测试它。

select 
  process_time, 
  product_id, 
  product_type_id, 
from (
  select 
    process_time, 
    product_id, 
    product_type_id, 
    lag(product_type_id) over (order by process_time) as prior_product_type_id
  from the_table
)
where 
  (prior_product_type_id <> product_type_id or prior_product_type_id is null)

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

相关问题 将列添加到 SQL 查询中,该查询显示来自另一个 SQL 查询的字母序列值 - add Column to an SQL query which shows alphabetical sequence value from another SQL query 如何在 PHP 脚本中将表与自身连接起来进行查询,并告诉脚本从哪个表中选择哪一列? - How canI join a table with itself for a query in a PHP script, and tell the script which column to pick from which table? 使用 postgresql 根据包含来自 table1 的 count() 的选择查询更新 table2 中的列 - Update column in table2 based on select query which contains count() from table1 using postgresql 查询名称每天更改的表 - Bigquery - Query table which whose name changes everyday - Bigquery 创建一个视图,显示来自哪个表内容 - Create a view that shows from which table content comes 从表中显示每个出版商的最新出版书籍的查询 - A query that shows latest published books from each publisher from a table 通过另一个表中的列查询访问表 - Query Access Table by a column from another Table MySQL查询仅显示列中的单个结果 - Mysql query that ONLY shows individual results from column SQL:确定从哪个表查询 - SQL: Determining from which table query 在MYSQL中查询,其中以列作为表名,另一列作为外键,以列作为表名 - Query in MYSQL which have column as table name and another column as foreign key to column as table name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM