简体   繁体   中英

SQL Data manipulation - window functions and complicated joins likely

Streaming Video data (event type with timestamp) to source and target using device.

基本表

期望的输出

select 
r1.customer_id,
'Play on ' || (r1.device) as source,
'Play on ' || lead(r2.device,1) OVER(PARTITION BY r2.customer_id ORDER BY 
r2.time::timestamp ASC) as target
from streaming r1
left join streaming r2 on r1.customer_id=r2.customer_id

This query above is giving me 8*8 = 64 rows, but I need 8 (Like the pic above), any help would be appreciated

*using PostgreSQL

Why are you doing a self-join? The results seem to only want lag() :

select s.customer_id,
       lag(s.device) over (partition by s.customer_id order by s.time) as source,
       s.device as target,
from streaming s;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM