【问题标题】:How to find the first transaction done by customers?如何找到客户完成的第一笔交易?
【发布时间】:2016-11-23 05:04:18
【问题描述】:

在消费者支出数据中,我如何找到客户完成的第一笔交易。例如,如果您有 50 笔交易,并且您有 10 位不同的客户完成了这 50 笔交易,那么如何找到这 10 位客户中的每一个客户完成的第一笔交易? 在 SAS 或 SQL 中执行此操作的建议确实会有所帮助。

【问题讨论】:

  • 如果您有交易日期与列,这是可能的。发布表结构
  • 尝试在数据步骤中查看by 组和first. 处理。这是一个很基础的SAS编程,网上搜索一下就可以轻松解决。在此论坛上提问时,您应该展示您所做的研究或尝试。

标签: sql sas


【解决方案1】:

我正在用 SQL 回答。

如果您是交易日期,请使用

Select min(transactiondate) from [Your Transaction table]
group by customer.

或者你可以直接使用

Select min(transaction) from [Your Table]
group by Customer.

使用 group by 将区分您的每个客户,Min() 将找到您的每个客户的最小或第一个交易日期。您可以在 where 子句中添加其他条件来过滤您的结果集。像这样

Select min(transaction) from [You Table]
where date>=[Starting transaction date] 
and date<=[End transactiondate].

【讨论】:

    【解决方案2】:

    在这里,您需要每个客户的第一笔交易,这就是我使用 RANK 功能并将记录分区到 customer_id 的原因。此函数将为每个客户的第一笔交易返回值 1,因此我在 where 子句中使用 firsttransaction=1,我们不能在 where 子句中直接使用 RANK 函数,所以这里我使用 INLINE 概念在 RANK 函数上应用条件

    Select * from
              (
        Select customer,
        RANK()
        OVER(partition by customer_id
                   Order by transaction_date) firsttransaction
        From table_name
                )
        Where firsttransaction=1
    

    【讨论】:

      【解决方案3】:

      在这里您需要每个客户的第一笔交易,这就是为什么我在这里使用RANK() 函数并根据customer_id 分区记录,此函数将返回值1,对于每个客户的第一笔交易,所以我在@987654323 中使用firsttransaction=1 @ 子句我们不能直接在where 子句中使用RANK() func 所以这里我使用INLINE 概念在RANK() 函数上应用条件

       Select * from ( Select customer, RANK() OVER(partition by customer_id Order by transaction_date) firsttransaction From table_name ) Where firsttransaction=1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-16
        • 2022-11-02
        • 1970-01-01
        • 1970-01-01
        • 2013-08-15
        • 2014-03-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多