AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 295139
Accepted
jsf80238
jsf80238
Asked: 2021-07-02 14:57:33 +0800 CST2021-07-02 14:57:33 +0800 CST 2021-07-02 14:57:33 +0800 CST

计算给定时间的累积金额

  • 772

我想找出在 P 小时内花费超过 D 美元的客户。假设 D=10 美元,P=48。然后在下面的交易数据中:

CUSTOMER         STAMP        AMOUNT
--------         -----        ------
   A     2021-06-02 00:22:53    1.44
   A     2021-06-02 06:24:17    1.51
   A     2021-06-03 07:09:45    2.73
   A     2021-06-03 15:57:30    3.92
   A     2021-06-04 06:41:21    1.83
   B     2021-06-01 02:50:22    2.65
   B     2021-06-03 07:01:36    4.05
   B     2021-06-04 05:20:10    3.30
   B     2021-06-04 09:53:53    2.64
   B     2021-06-04 14:54:00    2.26
   C     2021-06-01 16:01:38    2.61
   C     2021-06-01 23:38:25    1.16
   C     2021-06-02 14:41:02    2.82
   C     2021-06-03 00:28:37    1.54
   C     2021-06-03 02:06:46    1.19
   C     2021-06-04 17:16:29    2.05

客户 B 符合标准,客户 A 和 C 不符合(即使他们的总支出 > 10 美元)。

我知道如何使用窗口函数,但在这种情况下,我事先不知道窗口应该有多大。


我认为答案是这样的:

select
  customer
, stamp
, amount
, amount > coalesce(sum(amount) over (order by extract('epoch_second', stamp)
range between 2*24*60*60 preceding and current row), 0) as is_alert
from t
where amount is not null
order by stamp

但我的数据库是雪花,它似乎不支持这种语法。该页面说:

对于累积窗口框架: ... RANGE 类似于 ROWS,除了它只计算与当前行具有相同值的行的结果(根据指定的 ORDER BY 子句)。

window-functions snowflake
  • 1 1 个回答
  • 36 Views

1 个回答

  • Voted
  1. Best Answer
    john.da.costa
    2021-09-15T20:02:47+08:002021-09-15T20:02:47+08:00

    Snowflake 确实支持执行您的要求,但语法不同:

    这是脚本:

    CREATE SCHEMA IF NOT EXISTS demo;
    
    create or replace table demo.customer_transactions(
      transaction_id integer identity
    ,customer varchar
    ,txn_date timestamp_ltz
    ,amount float
    );
    
    //
    //CUSTOMER         STAMP        AMOUNT
    //--------         -----        ------
    insert into demo.customer_transactions (customer, txn_date, amount)
    values
    ('A',     '2021-06-02 00:22:53',    1.44)
    ,('A',     '2021-06-02 06:24:17',    1.51)
    ,('A',     '2021-06-03 07:09:45',    2.73)
    ,('A',     '2021-06-03 15:57:30',    3.92)
    ,('A',     '2021-06-04 06:41:21',    1.83)
    ,('B',     '2021-06-01 02:50:22',    2.65)
    ,('B',     '2021-06-03 07:01:36',    4.05)
    ,('B',     '2021-06-04 05:20:10',   3.30)
    ,('B',     '2021-06-04 09:53:53',    2.64)
    ,('B',     '2021-06-04 14:54:00',    2.26)
    ,('C',    '2021-06-01 16:01:38',    2.61)
    ,('C',     '2021-06-01 23:38:25',    1.16)
    ,('C',     '2021-06-02 14:41:02',    2.82)
    ,('C',     '2021-06-03 00:28:37',    1.54)
    ,('C',     '2021-06-03 02:06:46',    1.19)
    ,('C',     '2021-06-04 17:16:29',    2.05)
    ;
    
    
    select
    *
    ,case when window_transaction_amount > 10.00 then 'warn' else 'ok' end as is_alert
    from
    (
    select 
     a.transaction_id
    ,a.customer
    ,a.txn_date
    ,a.txn_date as window_start_date
    ,dateadd(hour,48, a.txn_date) as window_end_date
    ,a.amount
    //,array_construct(b.transaction_id) as b_transactions
    ,listagg(distinct b.transaction_id,',') within group (order by b.transaction_id) as window_transaction_ids
    ,sum(b.amount) as window_transaction_amount
    from demo.customer_transactions a left outer join demo.customer_transactions b on a.customer = b.customer and b.txn_date between a.txn_date and dateadd(hour,48, a.txn_date)
    group by 
      a.transaction_id
    ,a.customer
    ,a.txn_date
    ,dateadd(hour,48, a.txn_date)
    ,a.amount
    ) q
    order by 1,2,3;
    

    你得到的输出是这样的:在此处输入图像描述

    • 1

相关问题

  • 如何获得每个用户每个主题的最新五个分数?

  • 选择第 n 个百分位的行

  • 在 Microsoft SQL Server 2008 中,语法会生成错误“未启用并行数据仓库 (PDW) 功能”。

  • 使用 PL/R 对几何进行聚类

  • 如何让 row_number 具有 dense_rank 的行为

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve