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?













0















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



    1. Are there better solutions (better performance, simpler query, ...) for me?

    2. What if counter1 is the number of following records I need to select?










share|improve this question




























    0















    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



      1. Are there better solutions (better performance, simpler query, ...) for me?

      2. What if counter1 is the number of following records I need to select?










    share|improve this question


























      0












      0








      0








      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



        1. Are there better solutions (better performance, simpler query, ...) for me?

        2. What if counter1 is the number of following records I need to select?










      share|improve this question
















      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



        1. Are there better solutions (better performance, simpler query, ...) for me?

        2. What if counter1 is the number of following records I need to select?







      sql window-functions amazon-athena prestodb






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 9 at 16:32









      Piotr Findeisen

      5,49211644




      5,49211644










      asked Mar 8 at 5:05









      AverellAverell

      718




      718






















          1 Answer
          1






          active

          oldest

          votes


















          1














          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;





          share|improve this answer























          • 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










          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
          );



          );













          draft saved

          draft discarded


















          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









          1














          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;





          share|improve this answer























          • 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















          1














          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;





          share|improve this answer























          • 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













          1












          1








          1







          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;





          share|improve this answer













          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;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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

















          • 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



















          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.




          draft saved


          draft discarded














          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





















































          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

          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme