SQL - select surrounding records of the one fulfil a conditionInsert into … values ( SELECT … FROM … )How can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeInserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableSQL select only rows with max value on a columnHow to import an SQL file using the command line in MySQL?
Travelling outside the UK without a passport
Is there a single word describing earning money through any means?
How to implement a feedback to keep the DC gain at zero for this conceptual passive filter?
When were female captains banned from Starfleet?
Melting point of aspirin, contradicting sources
Why did the EU agree to delay the Brexit deadline?
What is this called? Old film camera viewer?
Why did the HMS Bounty go back to a time when whales are already rare?
Are the IPv6 address space and IPv4 address space completely disjoint?
Problem with TransformedDistribution
What was this official D&D 3.5e Lovecraft-flavored rulebook?
Where did Heinlein say "Once you get to Earth orbit, you're halfway to anywhere in the Solar System"?
Is it better practice to read straight from sheet music rather than memorize it?
Create all possible words using a set or letters
How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?
Freedom of speech and where it applies
Drawing ramified coverings with tikz
Longest common substring in linear time
Why should universal income be universal?
How can "mimic phobia" be cured or prevented?
It grows, but water kills it
Loading commands from file
Does the expansion of the universe explain why the universe doesn't collapse?
Offered money to buy a house, seller is asking for more to cover gap between their listing and mortgage owed
SQL - select surrounding records of the one fulfil a condition
Insert into … values ( SELECT … FROM … )How can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeInserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableSQL select only rows with max value on a columnHow to import an SQL file using the command line in MySQL?
I have the following table:
epochTime,id,counter1,value
123,Alpha,2,2
124,Beta,0,3
135,Alpha,0,1
112,Alpha,0,5
150,Alpha,0,-1
225,Beta,1,2
228,Beta,1,0
300,Beta,0,2
I want to select all records with counter1 > 0 and the record after that, partitioning by id and order by epochTime (the requirement similar to Unix "grep -A 1" command)
So the expected result from the data above would be
epochTime id counter1 value
123 Alpha 2 2
135 Alpha 0 1
225 Beta 1 2
228 Beta 1 0
300 Beta 0 2
I am using AWS Athena, and got the following query, which works as expected.
SELECT * FROM (
SELECT id,
epochTime,
counter1,
value,
first_value(counter1) OVER (
PARTITION BY id
ORDER BY epochTime
ROWS BETWEEN 1 PRECEDING AND CURRENT ROW
) AS preCounter
FROM testsql
) WHERE counter1 > 0 OR preCounter > 0
However, I see two problems with the query:
- It is a nested query
I needed to create a dummy column (preCounter). If the requirements on the WHERE condition become more complex (i.e: conditions on multiple columns), I would need to create multiple dummy columns
- Are there better solutions (better performance, simpler query, ...) for me?
- What if counter1 is the number of following records I need to select?
sql window-functions amazon-athena prestodb
add a comment |
I have the following table:
epochTime,id,counter1,value
123,Alpha,2,2
124,Beta,0,3
135,Alpha,0,1
112,Alpha,0,5
150,Alpha,0,-1
225,Beta,1,2
228,Beta,1,0
300,Beta,0,2
I want to select all records with counter1 > 0 and the record after that, partitioning by id and order by epochTime (the requirement similar to Unix "grep -A 1" command)
So the expected result from the data above would be
epochTime id counter1 value
123 Alpha 2 2
135 Alpha 0 1
225 Beta 1 2
228 Beta 1 0
300 Beta 0 2
I am using AWS Athena, and got the following query, which works as expected.
SELECT * FROM (
SELECT id,
epochTime,
counter1,
value,
first_value(counter1) OVER (
PARTITION BY id
ORDER BY epochTime
ROWS BETWEEN 1 PRECEDING AND CURRENT ROW
) AS preCounter
FROM testsql
) WHERE counter1 > 0 OR preCounter > 0
However, I see two problems with the query:
- It is a nested query
I needed to create a dummy column (preCounter). If the requirements on the WHERE condition become more complex (i.e: conditions on multiple columns), I would need to create multiple dummy columns
- Are there better solutions (better performance, simpler query, ...) for me?
- What if counter1 is the number of following records I need to select?
sql window-functions amazon-athena prestodb
add a comment |
I have the following table:
epochTime,id,counter1,value
123,Alpha,2,2
124,Beta,0,3
135,Alpha,0,1
112,Alpha,0,5
150,Alpha,0,-1
225,Beta,1,2
228,Beta,1,0
300,Beta,0,2
I want to select all records with counter1 > 0 and the record after that, partitioning by id and order by epochTime (the requirement similar to Unix "grep -A 1" command)
So the expected result from the data above would be
epochTime id counter1 value
123 Alpha 2 2
135 Alpha 0 1
225 Beta 1 2
228 Beta 1 0
300 Beta 0 2
I am using AWS Athena, and got the following query, which works as expected.
SELECT * FROM (
SELECT id,
epochTime,
counter1,
value,
first_value(counter1) OVER (
PARTITION BY id
ORDER BY epochTime
ROWS BETWEEN 1 PRECEDING AND CURRENT ROW
) AS preCounter
FROM testsql
) WHERE counter1 > 0 OR preCounter > 0
However, I see two problems with the query:
- It is a nested query
I needed to create a dummy column (preCounter). If the requirements on the WHERE condition become more complex (i.e: conditions on multiple columns), I would need to create multiple dummy columns
- Are there better solutions (better performance, simpler query, ...) for me?
- What if counter1 is the number of following records I need to select?
sql window-functions amazon-athena prestodb
I have the following table:
epochTime,id,counter1,value
123,Alpha,2,2
124,Beta,0,3
135,Alpha,0,1
112,Alpha,0,5
150,Alpha,0,-1
225,Beta,1,2
228,Beta,1,0
300,Beta,0,2
I want to select all records with counter1 > 0 and the record after that, partitioning by id and order by epochTime (the requirement similar to Unix "grep -A 1" command)
So the expected result from the data above would be
epochTime id counter1 value
123 Alpha 2 2
135 Alpha 0 1
225 Beta 1 2
228 Beta 1 0
300 Beta 0 2
I am using AWS Athena, and got the following query, which works as expected.
SELECT * FROM (
SELECT id,
epochTime,
counter1,
value,
first_value(counter1) OVER (
PARTITION BY id
ORDER BY epochTime
ROWS BETWEEN 1 PRECEDING AND CURRENT ROW
) AS preCounter
FROM testsql
) WHERE counter1 > 0 OR preCounter > 0
However, I see two problems with the query:
- It is a nested query
I needed to create a dummy column (preCounter). If the requirements on the WHERE condition become more complex (i.e: conditions on multiple columns), I would need to create multiple dummy columns
- Are there better solutions (better performance, simpler query, ...) for me?
- What if counter1 is the number of following records I need to select?
sql window-functions amazon-athena prestodb
sql window-functions amazon-athena prestodb
edited Mar 9 at 16:32
Piotr Findeisen
5,49211644
5,49211644
asked Mar 8 at 5:05
AverellAverell
718
718
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can use lag()
:
select t.*
from (select t.*,
lag(counter) over (partition by id order by epochtime) as prev_counter
from testseql t
) t
where counter > 0 or prev_counter > 0;
Thanks Gordon Lag, in this specific example, would make the query less complex by avoiding that "between row and row". But let's say I need to select 2 subsequent records (instead of 1 as in my example), lag won't work. Lag also doesn't help when my WHERE condition is on multiple columns.
– Averell
Mar 8 at 5:28
@Averell . . . "I want to select all records with counter1 > 0 and the record after that, partitioning by id and order by epochTime ". If you have a different question, ask a new question.
– Gordon Linoff
Mar 8 at 11:42
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f55057028%2fsql-select-surrounding-records-of-the-one-fulfil-a-condition%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use lag()
:
select t.*
from (select t.*,
lag(counter) over (partition by id order by epochtime) as prev_counter
from testseql t
) t
where counter > 0 or prev_counter > 0;
Thanks Gordon Lag, in this specific example, would make the query less complex by avoiding that "between row and row". But let's say I need to select 2 subsequent records (instead of 1 as in my example), lag won't work. Lag also doesn't help when my WHERE condition is on multiple columns.
– Averell
Mar 8 at 5:28
@Averell . . . "I want to select all records with counter1 > 0 and the record after that, partitioning by id and order by epochTime ". If you have a different question, ask a new question.
– Gordon Linoff
Mar 8 at 11:42
add a comment |
You can use lag()
:
select t.*
from (select t.*,
lag(counter) over (partition by id order by epochtime) as prev_counter
from testseql t
) t
where counter > 0 or prev_counter > 0;
Thanks Gordon Lag, in this specific example, would make the query less complex by avoiding that "between row and row". But let's say I need to select 2 subsequent records (instead of 1 as in my example), lag won't work. Lag also doesn't help when my WHERE condition is on multiple columns.
– Averell
Mar 8 at 5:28
@Averell . . . "I want to select all records with counter1 > 0 and the record after that, partitioning by id and order by epochTime ". If you have a different question, ask a new question.
– Gordon Linoff
Mar 8 at 11:42
add a comment |
You can use lag()
:
select t.*
from (select t.*,
lag(counter) over (partition by id order by epochtime) as prev_counter
from testseql t
) t
where counter > 0 or prev_counter > 0;
You can use lag()
:
select t.*
from (select t.*,
lag(counter) over (partition by id order by epochtime) as prev_counter
from testseql t
) t
where counter > 0 or prev_counter > 0;
answered Mar 8 at 5:07
Gordon LinoffGordon Linoff
790k35314418
790k35314418
Thanks Gordon Lag, in this specific example, would make the query less complex by avoiding that "between row and row". But let's say I need to select 2 subsequent records (instead of 1 as in my example), lag won't work. Lag also doesn't help when my WHERE condition is on multiple columns.
– Averell
Mar 8 at 5:28
@Averell . . . "I want to select all records with counter1 > 0 and the record after that, partitioning by id and order by epochTime ". If you have a different question, ask a new question.
– Gordon Linoff
Mar 8 at 11:42
add a comment |
Thanks Gordon Lag, in this specific example, would make the query less complex by avoiding that "between row and row". But let's say I need to select 2 subsequent records (instead of 1 as in my example), lag won't work. Lag also doesn't help when my WHERE condition is on multiple columns.
– Averell
Mar 8 at 5:28
@Averell . . . "I want to select all records with counter1 > 0 and the record after that, partitioning by id and order by epochTime ". If you have a different question, ask a new question.
– Gordon Linoff
Mar 8 at 11:42
Thanks Gordon Lag, in this specific example, would make the query less complex by avoiding that "between row and row". But let's say I need to select 2 subsequent records (instead of 1 as in my example), lag won't work. Lag also doesn't help when my WHERE condition is on multiple columns.
– Averell
Mar 8 at 5:28
Thanks Gordon Lag, in this specific example, would make the query less complex by avoiding that "between row and row". But let's say I need to select 2 subsequent records (instead of 1 as in my example), lag won't work. Lag also doesn't help when my WHERE condition is on multiple columns.
– Averell
Mar 8 at 5:28
@Averell . . . "I want to select all records with counter1 > 0 and the record after that, partitioning by id and order by epochTime ". If you have a different question, ask a new question.
– Gordon Linoff
Mar 8 at 11:42
@Averell . . . "I want to select all records with counter1 > 0 and the record after that, partitioning by id and order by epochTime ". If you have a different question, ask a new question.
– Gordon Linoff
Mar 8 at 11:42
add a comment |
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.
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%2f55057028%2fsql-select-surrounding-records-of-the-one-fulfil-a-condition%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