Joining by closest timestamp in PrestoDB
up vote
0
down vote
favorite
I have two large tables in a Presto db - calls
and mobile_updates
. Each mobile_id
has multiple updates, each with a new software version
.
Each call was made at a fixed timestamp call_started
.
My objective is join the two by the closest (previous) created_at
timestamp in mobile_updates
to when the call was made, and by mobile_id
to get the version
at the time the call was made in Presto. The result should be two columns: call_started
and version
The existing code for some reason does not produce the desired results - it only returns the most recent call for each mobile_id
, whereas there should be multiple calls.
Subqueries are not supported in Presto, so that is why partitions must be used. What could be wrong?
with calls as (
SELECT
call_started
, mobile_id
from calls
)
, ranking as (
select
mu.mobile_id
, mu.version
, mu.created_at
, c.call_started
, rank() over (partition by mu.mobile_id order by mu.created_at desc) as rank
from mobile_updates mu
join calls c
on mu.mobile_id = c.mobile_id
where mu.created_at < c.call_started
)
select
call_started
,version
from ranking
where rank = 1
sql prestodb
add a comment |
up vote
0
down vote
favorite
I have two large tables in a Presto db - calls
and mobile_updates
. Each mobile_id
has multiple updates, each with a new software version
.
Each call was made at a fixed timestamp call_started
.
My objective is join the two by the closest (previous) created_at
timestamp in mobile_updates
to when the call was made, and by mobile_id
to get the version
at the time the call was made in Presto. The result should be two columns: call_started
and version
The existing code for some reason does not produce the desired results - it only returns the most recent call for each mobile_id
, whereas there should be multiple calls.
Subqueries are not supported in Presto, so that is why partitions must be used. What could be wrong?
with calls as (
SELECT
call_started
, mobile_id
from calls
)
, ranking as (
select
mu.mobile_id
, mu.version
, mu.created_at
, c.call_started
, rank() over (partition by mu.mobile_id order by mu.created_at desc) as rank
from mobile_updates mu
join calls c
on mu.mobile_id = c.mobile_id
where mu.created_at < c.call_started
)
select
call_started
,version
from ranking
where rank = 1
sql prestodb
Sample data and desired results would help.
– Gordon Linoff
Nov 12 at 2:14
What do you mean by "Subqueries are not supported in Presto"? I know many TPC-DS queries include complex subqueries and Presto is able to execute all of them. Could you please show us a subquery that did not work for you? Also, what's the version of Presto you are using?
– Kamil Bajda-Pawlikowski
Nov 13 at 18:33
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have two large tables in a Presto db - calls
and mobile_updates
. Each mobile_id
has multiple updates, each with a new software version
.
Each call was made at a fixed timestamp call_started
.
My objective is join the two by the closest (previous) created_at
timestamp in mobile_updates
to when the call was made, and by mobile_id
to get the version
at the time the call was made in Presto. The result should be two columns: call_started
and version
The existing code for some reason does not produce the desired results - it only returns the most recent call for each mobile_id
, whereas there should be multiple calls.
Subqueries are not supported in Presto, so that is why partitions must be used. What could be wrong?
with calls as (
SELECT
call_started
, mobile_id
from calls
)
, ranking as (
select
mu.mobile_id
, mu.version
, mu.created_at
, c.call_started
, rank() over (partition by mu.mobile_id order by mu.created_at desc) as rank
from mobile_updates mu
join calls c
on mu.mobile_id = c.mobile_id
where mu.created_at < c.call_started
)
select
call_started
,version
from ranking
where rank = 1
sql prestodb
I have two large tables in a Presto db - calls
and mobile_updates
. Each mobile_id
has multiple updates, each with a new software version
.
Each call was made at a fixed timestamp call_started
.
My objective is join the two by the closest (previous) created_at
timestamp in mobile_updates
to when the call was made, and by mobile_id
to get the version
at the time the call was made in Presto. The result should be two columns: call_started
and version
The existing code for some reason does not produce the desired results - it only returns the most recent call for each mobile_id
, whereas there should be multiple calls.
Subqueries are not supported in Presto, so that is why partitions must be used. What could be wrong?
with calls as (
SELECT
call_started
, mobile_id
from calls
)
, ranking as (
select
mu.mobile_id
, mu.version
, mu.created_at
, c.call_started
, rank() over (partition by mu.mobile_id order by mu.created_at desc) as rank
from mobile_updates mu
join calls c
on mu.mobile_id = c.mobile_id
where mu.created_at < c.call_started
)
select
call_started
,version
from ranking
where rank = 1
sql prestodb
sql prestodb
asked Nov 12 at 2:09
the_darkside
1,9251033
1,9251033
Sample data and desired results would help.
– Gordon Linoff
Nov 12 at 2:14
What do you mean by "Subqueries are not supported in Presto"? I know many TPC-DS queries include complex subqueries and Presto is able to execute all of them. Could you please show us a subquery that did not work for you? Also, what's the version of Presto you are using?
– Kamil Bajda-Pawlikowski
Nov 13 at 18:33
add a comment |
Sample data and desired results would help.
– Gordon Linoff
Nov 12 at 2:14
What do you mean by "Subqueries are not supported in Presto"? I know many TPC-DS queries include complex subqueries and Presto is able to execute all of them. Could you please show us a subquery that did not work for you? Also, what's the version of Presto you are using?
– Kamil Bajda-Pawlikowski
Nov 13 at 18:33
Sample data and desired results would help.
– Gordon Linoff
Nov 12 at 2:14
Sample data and desired results would help.
– Gordon Linoff
Nov 12 at 2:14
What do you mean by "Subqueries are not supported in Presto"? I know many TPC-DS queries include complex subqueries and Presto is able to execute all of them. Could you please show us a subquery that did not work for you? Also, what's the version of Presto you are using?
– Kamil Bajda-Pawlikowski
Nov 13 at 18:33
What do you mean by "Subqueries are not supported in Presto"? I know many TPC-DS queries include complex subqueries and Presto is able to execute all of them. Could you please show us a subquery that did not work for you? Also, what's the version of Presto you are using?
– Kamil Bajda-Pawlikowski
Nov 13 at 18:33
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255184%2fjoining-by-closest-timestamp-in-prestodb%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Sample data and desired results would help.
– Gordon Linoff
Nov 12 at 2:14
What do you mean by "Subqueries are not supported in Presto"? I know many TPC-DS queries include complex subqueries and Presto is able to execute all of them. Could you please show us a subquery that did not work for you? Also, what's the version of Presto you are using?
– Kamil Bajda-Pawlikowski
Nov 13 at 18:33