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









share|improve this question






















  • 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















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









share|improve this question






















  • 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













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









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python