Updating an is_latest column on redshift table











up vote
0
down vote

favorite












I'm attempting to update an is_latest column on a redshift table grouped by source and source_primary_key with the following update statement, but getting an error that window functions are not allowed in update statements. What is the best way to go about this?



update my_schema.production_log
set is_latest =
case when run_start_time = max(run_start_time) over (partition by
source, source_primary_key)
then ‘t’ else ‘f’
end









share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm attempting to update an is_latest column on a redshift table grouped by source and source_primary_key with the following update statement, but getting an error that window functions are not allowed in update statements. What is the best way to go about this?



    update my_schema.production_log
    set is_latest =
    case when run_start_time = max(run_start_time) over (partition by
    source, source_primary_key)
    then ‘t’ else ‘f’
    end









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm attempting to update an is_latest column on a redshift table grouped by source and source_primary_key with the following update statement, but getting an error that window functions are not allowed in update statements. What is the best way to go about this?



      update my_schema.production_log
      set is_latest =
      case when run_start_time = max(run_start_time) over (partition by
      source, source_primary_key)
      then ‘t’ else ‘f’
      end









      share|improve this question















      I'm attempting to update an is_latest column on a redshift table grouped by source and source_primary_key with the following update statement, but getting an error that window functions are not allowed in update statements. What is the best way to go about this?



      update my_schema.production_log
      set is_latest =
      case when run_start_time = max(run_start_time) over (partition by
      source, source_primary_key)
      then ‘t’ else ‘f’
      end






      sql amazon-redshift






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 1:03

























      asked Nov 20 at 0:35









      willbarclay

      62




      62
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          Redshift supports a FROM clause in the update statement. Try this:



          update my_schema.production_log
          set is_latest = (case when run_start_time = max_run_start_time
          then 't' else 'f'
          end)
          from (select source, source_primary_key, max(run_start_time) as max_run_start_time
          from my_schema.production_log
          group by source, source_primary_key
          ) pl2
          where pl2.source = production_log.source and
          pl2.source_primary_key = production_log.source_primary_key;





          share|improve this answer




























            up vote
            0
            down vote













            You need to rewrite the query so it can join to a derived table:



            UPDATE u
            SET u.is_latest =CASE
            WHEN u.run_start_time = j.max_time THEN 't'
            ELSE 'f'
            END
            FROM my_schema.production_log AS u
            INNER JOIN (
            SELECT
            source
            , source_primary_key
            , MAX( run_start_time ) max_time
            FROM my_schema.production_log
            GROUP BY
            source
            , source_primary_key
            ) AS j
            ON u.source = j.source
            AND u.source_primary_key = j.source_primary_key





            share|improve this answer





















              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%2f53384602%2fupdating-an-is-latest-column-on-redshift-table%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              Redshift supports a FROM clause in the update statement. Try this:



              update my_schema.production_log
              set is_latest = (case when run_start_time = max_run_start_time
              then 't' else 'f'
              end)
              from (select source, source_primary_key, max(run_start_time) as max_run_start_time
              from my_schema.production_log
              group by source, source_primary_key
              ) pl2
              where pl2.source = production_log.source and
              pl2.source_primary_key = production_log.source_primary_key;





              share|improve this answer

























                up vote
                0
                down vote













                Redshift supports a FROM clause in the update statement. Try this:



                update my_schema.production_log
                set is_latest = (case when run_start_time = max_run_start_time
                then 't' else 'f'
                end)
                from (select source, source_primary_key, max(run_start_time) as max_run_start_time
                from my_schema.production_log
                group by source, source_primary_key
                ) pl2
                where pl2.source = production_log.source and
                pl2.source_primary_key = production_log.source_primary_key;





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Redshift supports a FROM clause in the update statement. Try this:



                  update my_schema.production_log
                  set is_latest = (case when run_start_time = max_run_start_time
                  then 't' else 'f'
                  end)
                  from (select source, source_primary_key, max(run_start_time) as max_run_start_time
                  from my_schema.production_log
                  group by source, source_primary_key
                  ) pl2
                  where pl2.source = production_log.source and
                  pl2.source_primary_key = production_log.source_primary_key;





                  share|improve this answer












                  Redshift supports a FROM clause in the update statement. Try this:



                  update my_schema.production_log
                  set is_latest = (case when run_start_time = max_run_start_time
                  then 't' else 'f'
                  end)
                  from (select source, source_primary_key, max(run_start_time) as max_run_start_time
                  from my_schema.production_log
                  group by source, source_primary_key
                  ) pl2
                  where pl2.source = production_log.source and
                  pl2.source_primary_key = production_log.source_primary_key;






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 2:43









                  Gordon Linoff

                  750k34285391




                  750k34285391
























                      up vote
                      0
                      down vote













                      You need to rewrite the query so it can join to a derived table:



                      UPDATE u
                      SET u.is_latest =CASE
                      WHEN u.run_start_time = j.max_time THEN 't'
                      ELSE 'f'
                      END
                      FROM my_schema.production_log AS u
                      INNER JOIN (
                      SELECT
                      source
                      , source_primary_key
                      , MAX( run_start_time ) max_time
                      FROM my_schema.production_log
                      GROUP BY
                      source
                      , source_primary_key
                      ) AS j
                      ON u.source = j.source
                      AND u.source_primary_key = j.source_primary_key





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        You need to rewrite the query so it can join to a derived table:



                        UPDATE u
                        SET u.is_latest =CASE
                        WHEN u.run_start_time = j.max_time THEN 't'
                        ELSE 'f'
                        END
                        FROM my_schema.production_log AS u
                        INNER JOIN (
                        SELECT
                        source
                        , source_primary_key
                        , MAX( run_start_time ) max_time
                        FROM my_schema.production_log
                        GROUP BY
                        source
                        , source_primary_key
                        ) AS j
                        ON u.source = j.source
                        AND u.source_primary_key = j.source_primary_key





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You need to rewrite the query so it can join to a derived table:



                          UPDATE u
                          SET u.is_latest =CASE
                          WHEN u.run_start_time = j.max_time THEN 't'
                          ELSE 'f'
                          END
                          FROM my_schema.production_log AS u
                          INNER JOIN (
                          SELECT
                          source
                          , source_primary_key
                          , MAX( run_start_time ) max_time
                          FROM my_schema.production_log
                          GROUP BY
                          source
                          , source_primary_key
                          ) AS j
                          ON u.source = j.source
                          AND u.source_primary_key = j.source_primary_key





                          share|improve this answer












                          You need to rewrite the query so it can join to a derived table:



                          UPDATE u
                          SET u.is_latest =CASE
                          WHEN u.run_start_time = j.max_time THEN 't'
                          ELSE 'f'
                          END
                          FROM my_schema.production_log AS u
                          INNER JOIN (
                          SELECT
                          source
                          , source_primary_key
                          , MAX( run_start_time ) max_time
                          FROM my_schema.production_log
                          GROUP BY
                          source
                          , source_primary_key
                          ) AS j
                          ON u.source = j.source
                          AND u.source_primary_key = j.source_primary_key






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 20 at 2:50









                          Used_By_Already

                          21.9k21838




                          21.9k21838






























                              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%2f53384602%2fupdating-an-is-latest-column-on-redshift-table%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

                              404 Error Contact Form 7 ajax form submitting

                              How to know if a Active Directory user can login interactively

                              TypeError: fit_transform() missing 1 required positional argument: 'X'