| monthly |Count (r.user_id)| Sum (p.price_amount) |
| 15-01 | | |
| 15-02 | | |
| 15-03 | | |
| 15-04 | | |
| 15-05 | | |
我想编写最终会出现如上所述的查询。
我可以尝试在excel上制作格式的最接近的格式如下...
SELECT
r.user_id,
max(r.created_at) as "typeA_max",
min(r_a.created_at) as "TypeB_min",
max(r.created_at) - min(r_a.created_at) AS "interval"
FROM reservations r
INNER JOIN offers o ON o.id = r.offer_id
INNER JOIN reservations r_a ON r_a.user_id = r.user_id AND o.type = 'A'
INNER JOIN payments AS p ON p.reservation_id = r.id
WHERe r.user_id in (
SELECT r.user_id
From reservations r
INNER JOIN offers o ON o.id = r.offer_id
INNER JOIN reservations r_a ON r_a.user_id = r.user_id AND o.type = 'B'
Group by 1
Having count (r.id) >= 2
)
AND r.status = 'finish'
Group by 1
在整个行中,我已经采样并发现了一些错误; max(r.created_at)或min(r.created_at)的日期都不是我认为我定义的每种类型的专用:(
我想计算: - 至少一个月前在B类之前购买A类的唯一用户数。 (即B型在4月1日购买,A型在5月2日购买 - >计数)
如果我能放的话会更容易 最大值(r.created_at) - 最小值(r.created_at)>三十 作为过滤器,但因为在WHERE子句上这是不可能的(当我为此应用“CASE WHEN”子句时我会得到常量错误)我不知道如何解决这个问题。
非常感谢您对此的帮助:)