Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 某些记录无下一笔交易的LEAD函数_Sql_Postgresql - Fatal编程技术网

Sql 某些记录无下一笔交易的LEAD函数

Sql 某些记录无下一笔交易的LEAD函数,sql,postgresql,Sql,Postgresql,我有多个客户。他们中的一些人只做了一笔交易,而另一些人做了更多的交易。我想运行一个查询,帮助我查看每个客户的前两个交易、时间戳和花费的金额。为此,我使用以下查询: select t.* from (select customer_id, transaction_type as tx_0, transaction_timestamp as tx_0_time, amount as tx_0_amount, lead(transaction_type, 1) ov

我有多个客户。他们中的一些人只做了一笔交易,而另一些人做了更多的交易。我想运行一个查询,帮助我查看每个客户的前两个交易、时间戳和花费的金额。为此,我使用以下查询:

select t.* 
from 
  (select customer_id, 
    transaction_type as tx_0, 
    transaction_timestamp as tx_0_time, amount as tx_0_amount,
    lead(transaction_type, 1) over (partition by customer_id order by transaction_timestamp) as tx_1,
    lead(transaction_timestamp, 1) over (partition by customer_id order by transaction_timestamp) as tx_1_time
    lead(amount, 1) over (partition by customer_id order by transaction_timestamp) as tx_1_amount,
  from payment_table 
  ) t
right join 
  (select customer_id, min(transaction_timestamp) as minimal
  from payment_table
  where transaction_type = 'Buy'
  group by customer_id) s
on t.customer_id = s.customer_id and t.tx_0_time = s.minimal
where tx_0 = 'Buy'
and tx_1 in ('Buy', '', null)
我只想获得特定类型的交易(='Buy'),我只想获得有史以来的前两个交易


查询工作正常,但仅适用于至少进行了两次交易的客户。如果客户只进行了一次交易,他不会出现在结果中。是否有办法修复查询,以便对于未进行第二次交易的客户,我将获得与第二次交易相关的列的空格?

第二次
WHERE
条件将过滤
t.tx_1
中的所有空值

请记住,NULL的行为与其他值不同,并且('Buy','',NULL)中的
NULL将导致NULL而不是TRUE

您应该将该条件重写为

(t.tx_1 IS NULL OR t.tx_1 = 'Buy')
或者更简洁,但也更神秘

coalesce(t.tx_1, 'Buy') = 'Buy'

或者,将比较移动到连接条件

中的
in
中的
NULL
与任何内容都不匹配,因此这是一个问题。但是,您正在执行一个
连接
,其中不需要任何连接:

select distinct on (customer_id) t.* 
from (select customer_id, 
             transaction_type as tx_0, 
             transaction_timestamp as tx_0_time,
             amount as tx_0_amount,
             lead(transaction_type, 1) over (partition by customer_id order by transaction_timestamp) as tx_1,
             lead(transaction_timestamp, 1) over (partition by customer_id order by transaction_timestamp) as tx_1_time
             lead(amount, 1) over (partition by customer_id order by transaction_timestamp) as tx_1_amount
      from payment_table 
     ) t
where tx_0 = 'Buy' and (tx_1 = 'Buy' or tx_1 is null)
order by customer_id, tx_0_time;

无需加入,您可以使用
行\编号
筛选到每个客户的第一行。此外,您不需要在最终结果中为任何一笔交易包含
transaction\u type
,您只需对其进行筛选,因为您只对
Buy
交易感兴趣

SELECT customer_id, tx_0_time, tx_0_amount, tx_1_time, tx_1_amount
FROM 
  (SELECT
     customer_id,
     transaction_timestamp AS tx_0_time,
     amount AS tx_0_amount,
     lead(transaction_timestamp, 1)
       OVER (PARTITION BY customer_id ORDER BY transaction_timestamp)
       AS tx_1_time,
     lead(amount, 1)
       OVER (PARTITION BY customer_id ORDER BY transaction_timestamp)
       AS tx_1_amount,
     row_number()
       OVER (PARTITION BY customer_id ORDER BY transaction_timestamp)
       AS rn
   FROM payment_table
   WHERE transaction_type = 'Buy'
  ) t
WHERE rn = 1;

请提供样品数据和结构,这是完美的工作。这是过滤器的问题。