我有两个m:m
表,profile_wordcards
和profile_activities
。我想按活动创建日期(profile_id = 2
)对所有单词卡进行分组。
也就是说,如果在activity 1
上创建了2019-01-19 2:12:05
,则在该日期或该日期之前创建的任何单词卡都应按activity 1
进行分组。如果在activity 2
上创建了2019-01-19 2:14:22
,则在该日期或该日期之前创建的所有单词卡应按activity 2
进行分组,依此类推。
表格: profile_activities
activity_id | profile_id | created_at
------------------------------------------
1 2 2019-01-19 2:12:05
2 2 2019-01-19 2:14:22
表格: profile_wordcards
wordcard_id | profile_id | created_at
-----------------------------------------
386 2 2019-01-19 2:04:07 >> Everything below: less than activity 1 created at
385 2 2019-01-19 2:05:19
263 2 2019-01-19 2:05:19
234 2 2019-01-19 2:11:49
175 2 2019-01-19 2:12:02
201 2 2019-01-19 2:12:02
226 2 2019-01-19 2:12:04
409 2 2019-01-19 2:12:05
361 2 2019-01-19 2:12:05
359 2 2019-01-19 2:12:25 >> Everything below: less than activity 2 created at
188 2 2019-01-19 2:12:34
227 2 2019-01-19 2:12:59
187 2 2019-01-19 2:13:01
228 2 2019-01-19 2:13:18
384 2 2019-01-19 2:13:37
177 2 2019-01-19 2:14:00
225 2 2019-01-19 2:14:00
所需的输出:
wordcard_id | profile_id | created_at | activity_id | activity_created_at
---------------------------------------------------------------------------------------
-- GROUP 1 (ACTIVITY ID 1, any wordcard.created_at <= 2019-01-19 2:12:05)
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
386 2 2019-01-19 2:04:07 1 2019-01-19 2:12:05
385 2 2019-01-19 2:05:19 1 2019-01-19 2:12:05
263 2 2019-01-19 2:05:19 1 2019-01-19 2:12:05
234 2 2019-01-19 2:11:49 1 2019-01-19 2:12:05
175 2 2019-01-19 2:12:02 1 2019-01-19 2:12:05
201 2 2019-01-19 2:12:02 1 2019-01-19 2:12:05
226 2 2019-01-19 2:12:04 1 2019-01-19 2:12:05
409 2 2019-01-19 2:12:05 1 2019-01-19 2:12:05
361 2 2019-01-19 2:12:05 1 2019-01-19 2:12:05
-- GROUP 2 (ACTIVITY ID 2, any wordcard.created_at <= 2019-01-19 2:14:22 but > 2019-01-19 2:12:05)
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
359 2 2019-01-19 2:12:25 2 2019-01-19 2:14:22
188 2 2019-01-19 2:12:34 2 2019-01-19 2:14:22
227 2 2019-01-19 2:12:59 2 2019-01-19 2:14:22
187 2 2019-01-19 2:13:01 2 2019-01-19 2:14:22
228 2 2019-01-19 2:13:18 2 2019-01-19 2:14:22
384 2 2019-01-19 2:13:37 2 2019-01-19 2:14:22
177 2 2019-01-19 2:14:00 2 2019-01-19 2:14:22
225 2 2019-01-19 2:14:00 2 2019-01-19 2:14:22
我尝试过:
select pwc.wordcard_id, pwc.created_at, pa.activity_id, pa.created_at, pwc.profile_id
from profile_wordcards pwc
left join profile_activities pa on (pa.created_at < pwc.created_at)
where pwc.profile_id = 2
order by activity_id asc
但这将返回a)未附加到配置文件2的活动ID,b)未按预期分组。
答案 0 :(得分:1)
不是联接profile_activities
,而是直接联接一个子查询,该子查询从profile_activities
以及lag()
ed created_at
中选择所有列。然后,您可以与“上一个”活动的created_at
进行比较。对于lag()
的默认值,该默认值是在没有先前活动的情况下获取的,请使用'-infinity'
。由于所有时间戳都大于负无穷大,因此在这种情况下,与单词卡created_at
的比较也将起作用。
SELECT w.wordcard_id,
w.profile_id,
w.created_at,
a.activity_id,
a.profile_id,
a.created_at
FROM (SELECT a.activity_id,
a.profile_id,
a.created_at,
lag(a.created_at,
1,
'-infinity') OVER (ORDER BY a.created_at) created_at_lag
FROM profile_activities a) a
INNER JOIN profile_wordcards w
ON w.profile_id = a.profile_id
AND w.created_at > a.created_at_lag
AND w.created_at <= a.created_at
ORDER BY a.activity_id;