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

          Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

          Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

          How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page