圖片此表交互的業務與人們:我可以在CASE語句中使用變量條件嗎?
+-----------+---------------------+-----------------+
| user_name | action_timestamp | action |
+-----------+---------------------+-----------------+
| john | 2017-01-01 10:00:00 | phone_call |
+-----------+---------------------+-----------------+
| john | 2017-01-02 12:00:00 | became_customer |
+-----------+---------------------+-----------------+
| john | 2017-01-03 14:00:00 | phone_call |
+-----------+---------------------+-----------------+
| jane | 2016-08-06 10:00:00 | phone_call |
+-----------+---------------------+-----------------+
| jane | 2016-08-06 11:00:00 | phone_call |
+-----------+---------------------+-----------------+
| jane | 2016-08-06 12:00:00 | became_customer |
+-----------+---------------------+-----------------+
| tony | 2016-12-01 15:00:00 | phone_call |
+-----------+---------------------+-----------------+
我想要得到的東西是這樣的:
+-----------+---------------+-------------+---------------------+------------------------------+-----------------------------+
| user_name | total_actions | is_customer | became_customer | interactions_before_customer | interactions_after_customer |
+-----------+---------------+-------------+---------------------+------------------------------+-----------------------------+
| john | 3 | TRUE | 2017-01-02 12:00:00 | 1 | 1 |
+-----------+---------------+-------------+---------------------+------------------------------+-----------------------------+
| jane | 3 | TRUE | 2016-08-06 12:00:00 | 2 | 0 |
+-----------+---------------+-------------+---------------------+------------------------------+-----------------------------+
| tony | 1 | FALSE | NULL | 1 | 0 |
+-----------+---------------+-------------+---------------------+------------------------------+-----------------------------+
第4列是微不足道的一些分組和案例,但我不知道如何做第5和第6列(客戶之前的交互以及客戶之後的交互),因爲案例是根據前一列的結果進行判斷的,需要在行之間進行變化。
這是比它顯得更簡單嗎?如果有人關心,我不在呼叫中心工作,它只是一個更簡單的模擬我想要做的;)
真棒。謝謝! –