简体   繁体   English

需要获取与最近列值关联的 id

[英]Need to get an id associated with the most recent column value

SQL QUERY SQL 查询

select id, month_id, id_type,
      max(case when immediate_prev <> id_type then immediate_prev
              end) over (partition by id, id_type, (seqnum - seqnum_2)
              ) as id_type_prev
from (select *,
            row_number() over (partition by id order by month_id) as seqnum,
            row_number() over (partition by id, id_type order by month_id) as seqnum_2,
            lag(id_type) over (partition by id order by month_id) as immediate_prev
      from `my_table`
      WHERE id = 123
        )
ORDER BY month_id asc

my_table data my_table数据

id|month_id|id_type
123|202001|aaa
123|202002|aaa
123|202003|aaa
123|202004|bbb
123|202005|bbb
123|202006|bbb

Query return data查询返回数据

id|month_id|id_type|id_type_prev
123|202001|aaa|null
123|202002|aaa|null
123|202003|aaa|null
123|202004|bbb|aaa
123|202005|bbb|aaa
123|202006|bbb|aaa

I have a SQL query that returns the previous id_type value for a given id.我有一个 SQL 查询,它返回给定 id 的前一个id_type值。 I would also like to know the month_id of the previous id_type but I am not sure how to get this information.我也想知道前一个month_idid_type ,但我不知道如何获取这些信息。 Above is the table data and what my current query returns.以上是表数据和我当前查询返回的内容。

Below is the additional data I am after, I woud like help getting the month_id_prev added to my above query.下面是我所追求的附加数据,我希望能帮助我将month_id_prev添加到我的上述查询中。 This would be the previous id_type 's most recent month_id.这将是前一个id_type最近的 month_id。

id|month_id|id_type|id_type_prev|month_id_prev
123|202001|aaa|null|null
123|202002|aaa|null|null
123|202003|aaa|null|null
123|202004|bbb|aaa|202003
123|202005|bbb|aaa|202003
123|202006|bbb|aaa|202003

Consider the approach below using your sample data:使用您的示例数据考虑以下方法:

with sample_data as (
  select 123 as id, 202001 as month_id, 'aaa' as id_type,
  union all select 123 as id, 202002 as month_id, 'aaa' as id_type,
  union all select 123 as id, 202003 as month_id, 'aaa' as id_type,
  union all select 123 as id, 202004 as month_id, 'bbb' as id_type,
  union all select 123 as id, 202005 as month_id, 'bbb' as id_type,
  union all select 123 as id, 202006 as month_id, 'bbb' as id_type,
),
cte1 as (
select id, month_id, id_type,
      max(case when immediate_prev <> id_type then immediate_prev
              end) over (partition by id, id_type, (seqnum - seqnum_2)
              ) as id_type_prev,
      latest_ym,
      lag(latest_ym) over (partition by id order by month_id) as prev_ym
from
(select *,
        row_number() over (partition by id order by month_id) as seqnum,
        row_number() over (partition by id, id_type order by month_id) as seqnum_2,
        lag(id_type) over (partition by id order by month_id) as immediate_prev,
        last_value(month_id) over (partition by id,id_type order by id) as latest_ym
from sample_data)

)

select 
  id,
  month_id,
  id_type,
  id_type_prev,
  max(if(month_id > prev_ym, prev_ym, null)) over (partition by id,id_type) as month_id_prev
from cte1
order by month_id asc

Output: Output:

在此处输入图像描述

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

相关问题 使用 orderByChild Firebase 获取最新的帖子 - Using orderByChild Firebase to get most recent posts Airflow 将最新文件从 GCS 存储桶复制到本地 - Airflow to copy most recent file from GCS bucket to local 根据列值在 bigquery 中获取列名 - get column names in bigquery based on the column value 根据另一列获取每列的最小值 - Get the lowest value per column based on another column 获取 firebase 中特定值的推送 ID android - Get the pushed ID for specific value in firebase android GitLab 注册表清理策略:如何保留最近的n个标签,而删除其他标签? - GitLab registry cleanup policy: how to keep most recent n tags, but delete others? 2020 年 12 月状态 -“部署 ID 未与当前项目相关联” - Status december 2020 - 'The deployment ID is not associated with the current project' 如何获取记录中列值的最大值? (大查询) - How to get max value of column values in a record ? (BigQuery) Twilio Studio IVR - 输入无效数字时,是否有办法播放默认错误消息并返回最新的 Gather 小部件? - Twilio Studio IVR - Is there a way to play a default error message and return to the most recent Gather widget when invalid digits are entered? 特定时间内重复次数最多的值 window - Most recurring value in a specific time window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM