Sql 一笔金额少于200美元有多久了

Sql 一笔金额少于200美元有多久了,sql,teradata,teradata-sql-assistant,Sql,Teradata,Teradata Sql Assistant,我需要确定有多长时间该金额低于200 我的数据集看起来像 Id user time amount max_amount 25 3618 1 0 1 25 3618 1 17040 3 25 3618 1 30 2 27 4281 1 0 1 27 4281 1 14188 3 27 4281 1 17372 4 27 4281 1 190 2 等等 生成它的代码如下所示:

我需要确定有多长时间该金额低于200

我的数据集看起来像

Id user time amount max_amount
25 3618  1    0        1
25 3618  1    17040    3
25 3618  1    30       2
27 4281  1    0        1
27 4281  1    14188    3
27 4281  1    17372    4
27 4281  1    190      2
等等

生成它的代码如下所示:

Select t2.id, t2.user, t1.time, sum(t1.amount_amt as float) / (t1.eur_amt as float) as amount,
rank () over (partition by t2.user order by amount) max_amount
From table1
Inner join table2 as t2
on t1.user=t2.user
Group by 1,2,3 
我的预期产出是

Id user time spent  
    25 3618  1    2
    27 4281  1    2

我怎样才能得到这个结果?

我想您只需要筛选和聚合:

select id, user, time, count(*)
from t
where amount < 200
group by id, user, time;
选择id、用户、时间、计数(*)
从t
其中金额<200
按id、用户、时间分组;
如果该表是由问题中的代码生成的,您可以在上述查询之前使用CTE:

with t as (
      <query>
     )
带t作为(
)