首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用分区by查找期间开始和结束时的帐户状态

使用分区by查找期间开始和结束时的帐户状态
EN

Stack Overflow用户
提问于 2022-07-04 19:44:24
回答 3查看 78关注 0票数 0

图中显示了两个日期(1月31日和7月31日)的每个帐户及其相关帐户状态(即开放或关闭)。底部的代码生成所示的图像。如何编辑/添加代码以提取1月31日至7月31日期间有多少帐户从开放到关闭,以及有多少帐户向相反方向移动?我对窗口功能不太熟悉,所以我会感谢你的帮助。

代码语言:javascript
复制
SELECT 
date
,account_id
,account_status 
,row_number() over(partition by account_id,account_status order by date asc) as row_rank

from X
INNER JOIN Y on x.account_id=y.account_id
qualify row_rank in (1,7)
EN

回答 3

Stack Overflow用户

发布于 2022-07-05 00:27:27

使用STRING_AGG聚合函数而不是使用窗口函数,

代码语言:javascript
复制
SELECT account_id, STRING_AGG(account_status, '->' ORDER BY date) AS status_change
  FROM sample GROUP BY 1;

您可以看到每个帐户的状态变化。

然后计数status_change

代码语言:javascript
复制
SELECT status_change, COUNT(1) cnt FROM (
  SELECT account_id, STRING_AGG(account_status, '->' ORDER BY date) AS status_change
    FROM sample GROUP BY 1
) GROUP BY 1;

产出将是:

样本表:

代码语言:javascript
复制
CREATE TEMP TABLE sample AS
SELECT '2020-01-31' date, 1 account_id, 'OPEN' account_status, 1 row_rank UNION ALL
SELECT '2020-07-31', 1, 'CLOSED', 7 UNION ALL
SELECT '2020-01-31', 2, 'OPEN', 1 UNION ALL
SELECT '2020-07-31', 2, 'OPEN', 7 UNION ALL
SELECT '2020-01-31', 3, 'CLOSED', 1 UNION ALL
SELECT '2020-07-31', 3, 'OPEN', 7;
票数 0
EN

Stack Overflow用户

发布于 2022-07-05 00:44:02

您可以尝试以下查询:

代码语言:javascript
复制
with sample_data as (
  select date('2020-01-31') as date, 111 as account_id, 'OPEN' as account_status,
  union all select date('2020-07-31') as date, 111 as account_id, 'OPEN' as account_status,
  union all select date('2020-01-31') as date, 222 as account_id, 'OPEN' as account_status,
  union all select date('2020-07-31') as date, 222 as account_id, 'CLOSED' as account_status,
  union all select date('2020-01-31') as date, 333 as account_id, 'OPEN' as account_status,
  union all select date('2020-07-31') as date, 333 as account_id, 'CLOSED' as account_status,
  union all select date('2020-01-31') as date, 444 as account_id, 'OPEN' as account_status,
  union all select date('2020-07-31') as date, 444 as account_id, 'OPEN' as account_status,
  union all select date('2020-01-31') as date, 555 as account_id, 'CLOSED' as account_status,
  union all select date('2020-07-31') as date, 555 as account_id, 'OPEN' as account_status,
  union all select date('2020-08-01') as date, 555 as account_id, 'CLOSED' as account_status,
),
sample_data_mod as (
  SELECT 
date
,account_id
,account_status 
,row_number() over(partition by account_id,account_status order by date asc) as row_rank,
lag(account_status) over (partition by account_id order by date asc) as prev_status
from sample_data
)


select 
date,
account_id,
account_status,
prev_status,
countif(prev_status = 'OPEN' and account_status = 'CLOSED') over (partition by account_id order by date asc) as open_to_close,
countif(prev_status = 'CLOSED' and account_status = 'OPEN') over (partition by account_id order by date asc) as close_to_open,
from sample_data_mod

输出:

这显示了每个account_status更改的运行更新。

票数 0
EN

Stack Overflow用户

发布于 2022-07-05 04:18:28

考虑以下选项

代码语言:javascript
复制
select account_status, next_account_status, count(*) accounts_moved
from (
  select *, lead(account_status) over statuses next_account_status
  from your_table
  window statuses as (partition by account_id order by date)
)
where not next_account_status is null
group by account_status, next_account_status 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72861375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档