找出超过某个值的第一个记录

时间:2016-09-26 16:42:09

标签: sql postgresql greatest-n-per-group

我有一个包含以下列的派生表:

  • 电子邮件(主要标识符)
  • transaction_time
  • 金额

如何根据PostgreSQL中第一个事务的amount > 500查找客户(通过电子邮件识别)?

注意:这用于用于过滤主表的子查询。

4 个答案:

答案 0 :(得分:2)

以下解决方案将更具可移植性DISTINCT ON,这是Postgres特定的。使用row_number()枚举行并获取第一笔交易金额大于500的所有不同客户(通过电子邮件识别)。

编辑:我已经提供了三种方法来实现相同的结果。选择您喜欢的任何一种。

第一种方法 - 使用row_number()

select 
  distinct email
from (
  select 
    email, 
    amount,
    row_number() OVER (PARTITION BY email ORDER BY transaction_time) AS rn
  from <derived_table_here>
  ) t
where
  rn = 1
  and amount > 500

第二种方法 - 使用DISTINCT ON

select 
  email 
from (
  select distinct on (email) 
    email, 
    amount
  from <derived_table_here>
  order by email, transaction_time
  ) t 
where amount > 500

第三种方法 - 使用NOT EXISTS

select 
  email
from <derived_table_here> t1
where 
  amount > 500 
  and not exists(
    select 1 
    from <derived_table_here> t2 
    where 
      t1.email = t2.email 
      and t1.transaction_time > t2.transaction_time
    )

我发现第三种方法最便携,因为MySQL例如不支持窗口函数AFAIK。这是为了将来在数据库之间进行切换 - 为您减少工作量。

测试下面的样本:

      email      |      transaction_time      | amount
-----------------+----------------------------+--------
 first@mail.com  | 2016-09-26 19:01:15.297251 |    400 -- 1st, amount < 500
 first@mail.com  | 2016-09-26 19:01:19.160095 |    500
 first@mail.com  | 2016-09-26 19:01:21.526307 |    550
 second@mail.com | 2016-09-26 19:01:28.659847 |    600 -- 1st, amount > 500
 second@mail.com | 2016-09-26 19:01:30.292691 |    200
 second@mail.com | 2016-09-26 19:01:31.748649 |    300
 third@mail.com  | 2016-09-26 19:01:38.59275  |    200 -- 1st, amount < 500
 third@mail.com  | 2016-09-26 19:01:40.833897 |    100
 fourth@mail.com | 2016-09-26 19:01:51.593279 |    501 -- 1st, amount > 500

答案 1 :(得分:0)

这可能应该这样做:

SELECT DISTINCT ON (email) *
FROM t
WHERE amount > 500
ORDER BY email, transaction_time

它将返回每封电子邮件的第一笔交易(相对于transaction_time)。

答案 2 :(得分:0)

另一种选择:

select * from t t1
where amount > 500
and not exists
(select 1 from t t2 where t1.email=t2.email and t1.transaction_time>t2.transaction_time)

答案 3 :(得分:0)

LEFT SELF JOIN METHOD

  SELECT t1.*
  FROM
      ExmapleTable t1
      LEFT JOIN ExmapleTable t2
      ON t1.Email = t2.Email
      AND t2.transaction_time < t1.transaction_time
  WHERE
      t1.Amount >= 500
      AND t2.Email IS NULL
  ;

http://rextester.com/XRQTX2627