简体   繁体   English

如何在 SQL 中查找最后更改行 - Big Query

[英]How to Find Last Change row in SQL - Big Query

Can someone please provide a query that I can use in Google Big Query to identify the total count of users for whom the value changed specifically from 'C' to 'P'?有人可以提供一个查询,我可以在 Google Big Query 中使用它来确定值从“C”具体更改为“P”的用户总数吗? In the below table userid=123 satisfies this even though later userid = 123 changes back from 'P' to 'C'.在下表中,userid=123 满足这一点,即使后来 userid=123 从“P”变回“C”。

userid    timestamp            Value
123       9-15-2020 02:35:45   C
456       9-15-2020 01:45:09   P
789       9-15-2020 06:22:10   P
123       9-15-2020 03:43:00   P
456       9-15-2020 03:45:10   C
123       9-15-2020 07:40:34   C

You can try using lag()您可以尝试使用lag()

select userid from
(
select userid, timestamp, value, lag(value) over(partition by userid order by timestamp) as prev_value
from tablename
)A where value='P' and prev_value='C'

Can someone please provide a query that I can use in Google Big Query to identify the total count of users for whom the value changed specifically from 'C' to 'P'有人可以提供一个查询,我可以在 Google Big Query 中使用它来确定值从“C”更改为“P”的用户总数

Note that this is not consistent with the title of the question.请注意,这与问题的标题不一致。

lag() is the key idea. lag()是关键思想。 But it is unclear whether you want the count of users or the count of changes.但是不清楚您是想要用户数还是更改数。 This calculates both:这计算:

select count(*) as num_changes,
       count(distinct userid) as num_users_with_change
from (select t.*,
             lag(value) over(partition by userid order by timestamp) as prev_value
      from tablename t
     ) t
where value = 'P' and prev_value = 'C';

The second column counts a user only once, regardless of the number of times they have changed (which is my interpretation of your question).第二列只计算一个用户一次,不管他们改变了多少次(这是我对你的问题的解释)。

identify the total count of users for whom the value changed specifically from 'C' to 'P'?确定值从“C”更改为“P”的用户总数?

Below is for BigQuery Standard SQL下面是 BigQuery 标准 SQL

#standardSQL
SELECT COUNT(DISTINCT userid) AS qualified_users
FROM `project.dataset.table` 
GROUP BY userid
HAVING STRPOS(STRING_AGG(value, '' ORDER BY timestamp), 'CP') > 0    

Note;笔记; I assume your timestamp column is of TIMESTAMP data type - otherwise you will need to use PARSE_TIMESTAMP in ORDER BY portion我假设您的时间戳列是 TIMESTAMP 数据类型 - 否则您将需要在 ORDER BY 部分使用PARSE_TIMESTAMP

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

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