我有一个评论表和一个交易表。我想找出客户提交评论后的交易数量。
查看表如下所示:
+-------------+--------------+-------------+----------------+
| customer_id | review_score | review_date | transaction_id |
+-------------+--------------+-------------+----------------+
| 123 | 4 | 2019-01-01 | 894 |
+-------------+--------------+-------------+----------------+
| 123 | 9 | 2019-05-23 | 897 |
+-------------+--------------+-------------+----------------+
| etc | etc | etc | etc |
+-------------+--------------+-------------+----------------+
交易表如下:
+-------------+------------------+----------------+
| customer_id | transaction_date | transaction_id |
+-------------+------------------+----------------+
| 123 | 2019-10-01 | 901 |
+-------------+------------------+----------------+
| 123 | 2019-12-04 | 903 |
+-------------+------------------+----------------+
| etc | etc | etc |
+-------------+------------------+----------------+
我希望看到以下内容:
+-------------+--------------+-------------+----------------+-------------+------------------+----------------+
| customer_id | review_score | review_date | transaction_id | customer_id | transaction_date | transaction_id |
+-------------+--------------+-------------+----------------+-------------+------------------+----------------+
| 123 | 4 | 2019-01-01 | 894 | null | null | null |
+-------------+--------------+-------------+----------------+-------------+------------------+----------------+
| 123 | 9 | 2019-05-23 | 897 | 123 | 2019-10-01 | 901 |
+-------------+--------------+-------------+----------------+-------------+------------------+----------------+
| 123 | 9 | 2019-05-23 | 897 | 123 | 2019-12-04 | 903 |
+-------------+--------------+-------------+----------------+-------------+------------------+----------------+
| etc | etc | etc | etc | etc | etc | etc |
+-------------+--------------+-------------+----------------+-------------+------------------+----------------+
交易是在提交最新评论之后进行的。在某些情况下,客户会进行多次购买并提交评论。我想在提交评论后且提交下一个评论之前加入交易。
我的查询:
with review_cte as (
select transaction_id
, customer_id
, review_date
, lead(review_date, 1) over (partition by customer_id order by review_date) as review_date_lead
, review_score
from review
)
select rev.*
, b.transaction_id
, b.customer_id
, b.transaction_date
from review_cte as rev
join booking b
on b.unique_customer_id = rev.customer_id
and b.transaction_date > rev.review_date
and b.transaction_date < rev.review_date_lead
and b.booking_id <> rev.booking_id
order by rev.customer_id, rev.review_date
, b.customer_id, b.transaction_date
;
我的查询的问题是它不会返回所有行,也不会多次加入评论交易ID,因为它满足交易日期为>审阅日期的条件。我说依赖是因为我一直在尝试通过一些连接条件和类似的事情进行
答案 0 :(得分:1)
这应该做您想要的:
select rev.*,
b.transaction_id, b.customer_id, b.transaction_date
from (select r.*,
lead(review_date) over (partition by customer_id order by review_date) as next_review_date
from review r
) r left join
booking b
on b.unique_customer_id = r.customer_id and
b.transaction_date > r.review_date and
(b.transaction_date < r.review_date_lead or r.review_date_lead is null
) and
b.booking_id <> r.booking_id
order by r.customer_id, r.review_date, b.customer_id, b.transaction_date;