我有一个包含3列的表A:
+---------+--------------+-----------------+
| user_id | amount spent | timestamp |
+---------+--------------+-----------------+
| 2433036 | 59,95 | 01.04.2018 6:34 |
| 139280 | 129 | 01.04.2018 3:09 |
| 139280 | 14,95 | 01.04.2018 3:15 |
| 2498579 | 14,95 | 02.04.2018 6:45 |
| 1548313 | 29,95 | 02.04.2018 7:08 |
| 2105392 | 14,95 | 03.04.2018 7:07 |
| 188470 | 29,95 | 03.04.2018 8:54 |
| 2433036 | 59,95 | 22.03.2018 6:34 |
| 139280 | 129 | 24.03.2018 3:09 |
| 139280 | 14,95 | 25.03.2018 3:15 |
+---------+--------------+-----------------+
我需要根据用户花费的总金额(用户的LTV)创建3个组,并制作一个表格,我可以看到每个组每天带来多少钱。该表必须这样:
+------------+--------+-------------+---------+
| date | < $ 50 | $ 50 - $ 99 | $ 100 + |
+------------+--------+-------------+---------+
| 01.04.2018 | 0 | 60 | 90 |
| 02.04.2018 | 14,95 | 75 | 150 |
| 03.04.2018 | 14,95 | 35 | 125 |
+------------+--------+-------------+---------+
我用CASE WHEN表达式完成了聚合部分,但是后来合并了。有人可以帮忙解决这个问题吗?
SELECT user_id, SUM(amount_spent) AS total_amount,
CASE
WHEN (sum(amount_spent) < 50) THEN '< $ 50'
WHEN (sum(amount_spent) < 100) THEN ' $ 50 - $ 100'
ELSE ' > $ 100'
END AS Category
FROM TableA
GROUP BY user_id;
此代码为每个用户生成聚合。