Sql 我可以在CASE语句中使用可变条件吗?
想象一下一个企业与人之间的互动:Sql 我可以在CASE语句中使用可变条件吗?,sql,Sql,想象一下一个企业与人之间的互动: +-----------+---------------------+-----------------+ | user_name | action_timestamp | action | +-----------+---------------------+-----------------+ | john | 2017-01-01 10:00:00 | phone_call | +-----------+----
+-----------+---------------------+-----------------+
| 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列交互,因为案例是基于前一列的结果预测的,并且需要在行之间变化
这比表面上看起来简单吗?如果有人关心我不在呼叫中心工作,这只是一个简单得多的类比,我正在尝试做什么 这会给你一个想法。我已经在ORACLE上进行了测试,它运行良好
WITH CUS_DET AS (
SELECT *
FROM INTERACTIONS
WHERE ACTION = 'became_customer'
)
SELECT INTERACTIONS.USER_NAME
, SUM(CASE
WHEN (CUS_DET.ACTION_TIMESTAMP IS NULL
OR INTERACTIONS.ACTION_TIMESTAMP < CUS_DET.ACTION_TIMESTAMP)
THEN 1
ELSE 0
END) BEF
, SUM(CASE
WHEN INTERACTIONS.ACTION_TIMESTAMP > CUS_DET.ACTION_TIMESTAMP
THEN 1
ELSE 0
END) AFT
FROM INTERACTIONS
LEFT OUTER JOIN CUS_DET
ON INTERACTIONS.USER_NAME = CUS_DET.USER_NAME
GROUP BY INTERACTIONS.USER_NAME;
这会给你一个想法。我已经在ORACLE上进行了测试,它运行良好
WITH CUS_DET AS (
SELECT *
FROM INTERACTIONS
WHERE ACTION = 'became_customer'
)
SELECT INTERACTIONS.USER_NAME
, SUM(CASE
WHEN (CUS_DET.ACTION_TIMESTAMP IS NULL
OR INTERACTIONS.ACTION_TIMESTAMP < CUS_DET.ACTION_TIMESTAMP)
THEN 1
ELSE 0
END) BEF
, SUM(CASE
WHEN INTERACTIONS.ACTION_TIMESTAMP > CUS_DET.ACTION_TIMESTAMP
THEN 1
ELSE 0
END) AFT
FROM INTERACTIONS
LEFT OUTER JOIN CUS_DET
ON INTERACTIONS.USER_NAME = CUS_DET.USER_NAME
GROUP BY INTERACTIONS.USER_NAME;
如果用户只能成为客户一次,则使用条件聚合:
select
t.user_name
, case when c.action_timestamp is not null then 'Yes' else 'No' end as Is_Customer
, c.action_timestamp as became_customer
, count(case when t.action_timestamp < coalesce(c.action_timestamp,'2525-01-01') then 1 end) as interactions_before_customer
, count(case when t.action_timestamp > c.action_timestamp then 1 end) as interactions_after_customer
from t
left join t as c
on t.user_name = c.user_name
and c.action = 'became_customer'
group by t.user_name, c.action_timestamp
如果用户只能成为客户一次,则使用条件聚合:
select
t.user_name
, case when c.action_timestamp is not null then 'Yes' else 'No' end as Is_Customer
, c.action_timestamp as became_customer
, count(case when t.action_timestamp < coalesce(c.action_timestamp,'2525-01-01') then 1 end) as interactions_before_customer
, count(case when t.action_timestamp > c.action_timestamp then 1 end) as interactions_after_customer
from t
left join t as c
on t.user_name = c.user_name
and c.action = 'became_customer'
group by t.user_name, c.action_timestamp
啊,很抱歉,很难在这些答案中做出选择!我之所以选择这一款,是因为你是SQL名人,我也了解了coalesce,但Pons也很管用,他提前了几分钟就到了。谢谢你的回复:啊,很抱歉,很难在这些答案中做出选择!我之所以选择这一款,是因为你是SQL名人,我也了解了coalesce,但Pons也很管用,他提前了几分钟就到了。谢谢你的回复:太棒了。谢谢令人惊叹的谢谢