Using lag function in mysql to get previous values Announcing the arrival of Valued Associate...

What does F' and F" mean?

Why light coming from distant stars is not discrete?

When a candle burns, why does the top of wick glow if bottom of flame is hottest?

Okay to merge included columns on otherwise identical indexes?

Denied boarding although I have proper visa and documentation. To whom should I make a complaint?

Why do people hide their license plates in the EU?

Generate an RGB colour grid

At the end of Thor: Ragnarok why don't the Asgardians turn and head for the Bifrost as per their original plan?

Why didn't this character "real die" when they blew their stack out in Altered Carbon?

Why did the IBM 650 use bi-quinary?

Novel: non-telepath helps overthrow rule by telepaths

ListPlot join points by nearest neighbor rather than order

What would be the ideal power source for a cybernetic eye?

How do I keep my slimes from escaping their pens?

How to find out what spells would be useless to a blind NPC spellcaster?

How to call a function with default parameter through a pointer to function that is the return of another function?

Do I really need recursive chmod to restrict access to a folder?

Book where humans were engineered with genes from animal species to survive hostile planets

First console to have temporary backward compatibility

How to override model in magento2?

Can a non-EU citizen traveling with me come with me through the EU passport line?

How do pianists reach extremely loud dynamics?

How to tell that you are a giant?

How to find all the available tools in mac terminal?



Using lag function in mysql to get previous values



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Strange behaviour with MySQL 5.1 TO_DAYS partitioning schemaMySQL Insert into two tables using new IDsHow to arrange rows selected in MYSQL query?Unable to get the latest value added to Mysql table based on posted timeGrouping Data Using Joined Tables with Certain Distinct ValuesIs it possible to clean up duplicate entries with just SQL?Strange sort behavior on a partitioned table with a prefix indexMYSQL Including Missing Values Using the Previous Most Recent RecordMysql temp table insert from json can't handle null valuesMYSQL: How to select all attributes of all duplicate entries?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I am trying to create MySQL dataset which has entries from previous study (surgical study with the first study having value of 1) as well for that userid. Each user can have multiple studies. I am trying to use Lag function to get previous study data. I am getting duplicate values for study 1 while trying to use the lag function.



WITH Complete_Data AS (
SELECT *
FROM Data
GROUP BY UserId, studyno
)
SELECT *, LAG(indication_of_failure, 1)
OVER (
PARTITION BY UserId
ORDER BY studyno
) prev_study_indication_of_failure
,
LAG(BCVA_State,1)
OVER (
PARTITION BY UserId
ORDER BY studyno
) prev_study_BCVA
FROM
Complete_Data
GROUP BY UserId, studyno
;


I am expecting nulls in prev_study_indication_of_failure and prev_study_BCVA when studyno = 1 but filled with values when studyno >=2.



Due to duplicate entries for studyno = 1, I am getting values in prev_study_indication_of_failure and prev_study_BCVA









share







New contributor




user1048278 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



























    0















    I am trying to create MySQL dataset which has entries from previous study (surgical study with the first study having value of 1) as well for that userid. Each user can have multiple studies. I am trying to use Lag function to get previous study data. I am getting duplicate values for study 1 while trying to use the lag function.



    WITH Complete_Data AS (
    SELECT *
    FROM Data
    GROUP BY UserId, studyno
    )
    SELECT *, LAG(indication_of_failure, 1)
    OVER (
    PARTITION BY UserId
    ORDER BY studyno
    ) prev_study_indication_of_failure
    ,
    LAG(BCVA_State,1)
    OVER (
    PARTITION BY UserId
    ORDER BY studyno
    ) prev_study_BCVA
    FROM
    Complete_Data
    GROUP BY UserId, studyno
    ;


    I am expecting nulls in prev_study_indication_of_failure and prev_study_BCVA when studyno = 1 but filled with values when studyno >=2.



    Due to duplicate entries for studyno = 1, I am getting values in prev_study_indication_of_failure and prev_study_BCVA









    share







    New contributor




    user1048278 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      I am trying to create MySQL dataset which has entries from previous study (surgical study with the first study having value of 1) as well for that userid. Each user can have multiple studies. I am trying to use Lag function to get previous study data. I am getting duplicate values for study 1 while trying to use the lag function.



      WITH Complete_Data AS (
      SELECT *
      FROM Data
      GROUP BY UserId, studyno
      )
      SELECT *, LAG(indication_of_failure, 1)
      OVER (
      PARTITION BY UserId
      ORDER BY studyno
      ) prev_study_indication_of_failure
      ,
      LAG(BCVA_State,1)
      OVER (
      PARTITION BY UserId
      ORDER BY studyno
      ) prev_study_BCVA
      FROM
      Complete_Data
      GROUP BY UserId, studyno
      ;


      I am expecting nulls in prev_study_indication_of_failure and prev_study_BCVA when studyno = 1 but filled with values when studyno >=2.



      Due to duplicate entries for studyno = 1, I am getting values in prev_study_indication_of_failure and prev_study_BCVA









      share







      New contributor




      user1048278 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I am trying to create MySQL dataset which has entries from previous study (surgical study with the first study having value of 1) as well for that userid. Each user can have multiple studies. I am trying to use Lag function to get previous study data. I am getting duplicate values for study 1 while trying to use the lag function.



      WITH Complete_Data AS (
      SELECT *
      FROM Data
      GROUP BY UserId, studyno
      )
      SELECT *, LAG(indication_of_failure, 1)
      OVER (
      PARTITION BY UserId
      ORDER BY studyno
      ) prev_study_indication_of_failure
      ,
      LAG(BCVA_State,1)
      OVER (
      PARTITION BY UserId
      ORDER BY studyno
      ) prev_study_BCVA
      FROM
      Complete_Data
      GROUP BY UserId, studyno
      ;


      I am expecting nulls in prev_study_indication_of_failure and prev_study_BCVA when studyno = 1 but filled with values when studyno >=2.



      Due to duplicate entries for studyno = 1, I am getting values in prev_study_indication_of_failure and prev_study_BCVA







      mysql





      share







      New contributor




      user1048278 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      user1048278 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      user1048278 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 4 mins ago









      user1048278user1048278

      1




      1




      New contributor




      user1048278 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user1048278 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user1048278 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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
          });


          }
          });






          user1048278 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f235005%2fusing-lag-function-in-mysql-to-get-previous-values%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          user1048278 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          user1048278 is a new contributor. Be nice, and check out our Code of Conduct.













          user1048278 is a new contributor. Be nice, and check out our Code of Conduct.












          user1048278 is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • 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%2fdba.stackexchange.com%2fquestions%2f235005%2fusing-lag-function-in-mysql-to-get-previous-values%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

          ORA-01691 (unable to extend lob segment) even though my tablespace has AUTOEXTEND onORA-01692: unable to...

          Always On Availability groups resolving state after failover - Remote harden of transaction...

          Circunscripción electoral de Guipúzcoa Referencias Menú de navegaciónLas claves del sistema electoral en...