SQL to populate data for missing weeks












-1















I would like to build a sql to fill the missing weeks of data. Could you please help.



Example:



Year - Week    order
2018 W45 250
2018 W51 300


I would like the display to be as below:



Year - Week    order
2018 W45 250
2018 W46 250
2018 W47 250
2018 W48 250
2018 W49 250
2018 W50 250
2018 W51 300
...
2019 W52 300


Assuming the date end limit is 2019 December.



Thanks










share|improve this question




















  • 4





    Tag your question with the database you are using.

    – Gordon Linoff
    Nov 22 '18 at 12:16











  • What actually are you trying to do?.... could you be more specific?

    – Fmanin
    Nov 22 '18 at 12:18













  • In order to save space in the db - we are storing data only on weeks of change. I was wondering if I can use a sql to replicate the data in between the weeks so that I can display the data by weeks.

    – paty26s
    Nov 22 '18 at 12:39








  • 1





    What database server are you using? which version?

    – Fmanin
    Nov 22 '18 at 12:58
















-1















I would like to build a sql to fill the missing weeks of data. Could you please help.



Example:



Year - Week    order
2018 W45 250
2018 W51 300


I would like the display to be as below:



Year - Week    order
2018 W45 250
2018 W46 250
2018 W47 250
2018 W48 250
2018 W49 250
2018 W50 250
2018 W51 300
...
2019 W52 300


Assuming the date end limit is 2019 December.



Thanks










share|improve this question




















  • 4





    Tag your question with the database you are using.

    – Gordon Linoff
    Nov 22 '18 at 12:16











  • What actually are you trying to do?.... could you be more specific?

    – Fmanin
    Nov 22 '18 at 12:18













  • In order to save space in the db - we are storing data only on weeks of change. I was wondering if I can use a sql to replicate the data in between the weeks so that I can display the data by weeks.

    – paty26s
    Nov 22 '18 at 12:39








  • 1





    What database server are you using? which version?

    – Fmanin
    Nov 22 '18 at 12:58














-1












-1








-1








I would like to build a sql to fill the missing weeks of data. Could you please help.



Example:



Year - Week    order
2018 W45 250
2018 W51 300


I would like the display to be as below:



Year - Week    order
2018 W45 250
2018 W46 250
2018 W47 250
2018 W48 250
2018 W49 250
2018 W50 250
2018 W51 300
...
2019 W52 300


Assuming the date end limit is 2019 December.



Thanks










share|improve this question
















I would like to build a sql to fill the missing weeks of data. Could you please help.



Example:



Year - Week    order
2018 W45 250
2018 W51 300


I would like the display to be as below:



Year - Week    order
2018 W45 250
2018 W46 250
2018 W47 250
2018 W48 250
2018 W49 250
2018 W50 250
2018 W51 300
...
2019 W52 300


Assuming the date end limit is 2019 December.



Thanks







sql database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 12:42







paty26s

















asked Nov 22 '18 at 12:13









paty26spaty26s

11




11








  • 4





    Tag your question with the database you are using.

    – Gordon Linoff
    Nov 22 '18 at 12:16











  • What actually are you trying to do?.... could you be more specific?

    – Fmanin
    Nov 22 '18 at 12:18













  • In order to save space in the db - we are storing data only on weeks of change. I was wondering if I can use a sql to replicate the data in between the weeks so that I can display the data by weeks.

    – paty26s
    Nov 22 '18 at 12:39








  • 1





    What database server are you using? which version?

    – Fmanin
    Nov 22 '18 at 12:58














  • 4





    Tag your question with the database you are using.

    – Gordon Linoff
    Nov 22 '18 at 12:16











  • What actually are you trying to do?.... could you be more specific?

    – Fmanin
    Nov 22 '18 at 12:18













  • In order to save space in the db - we are storing data only on weeks of change. I was wondering if I can use a sql to replicate the data in between the weeks so that I can display the data by weeks.

    – paty26s
    Nov 22 '18 at 12:39








  • 1





    What database server are you using? which version?

    – Fmanin
    Nov 22 '18 at 12:58








4




4





Tag your question with the database you are using.

– Gordon Linoff
Nov 22 '18 at 12:16





Tag your question with the database you are using.

– Gordon Linoff
Nov 22 '18 at 12:16













What actually are you trying to do?.... could you be more specific?

– Fmanin
Nov 22 '18 at 12:18







What actually are you trying to do?.... could you be more specific?

– Fmanin
Nov 22 '18 at 12:18















In order to save space in the db - we are storing data only on weeks of change. I was wondering if I can use a sql to replicate the data in between the weeks so that I can display the data by weeks.

– paty26s
Nov 22 '18 at 12:39







In order to save space in the db - we are storing data only on weeks of change. I was wondering if I can use a sql to replicate the data in between the weeks so that I can display the data by weeks.

– paty26s
Nov 22 '18 at 12:39






1




1





What database server are you using? which version?

– Fmanin
Nov 22 '18 at 12:58





What database server are you using? which version?

– Fmanin
Nov 22 '18 at 12:58












1 Answer
1






active

oldest

votes


















0














On PostgreSQL you can try something like the following.



SELECT sq."year-week"
, sq.orders
FROM
(
SELECT LEFT(a.rstrt, 6) || W "year-week"
, a.orders
, ROW_NUMBER() OVER(PARTITION BY LEFT(a.rstrt, 6) || W ORDER BY a.rend DESC) RN
FROM(SELECT "year-week" rstrt
, COALESCE(LEAD ("year-week", 1) OVER(), 2018 || ' W' || 52) rend
, orders
FROM orders
UNION
SELECT '2019 W01', '2019 W52', 300) a
LEFT JOIN (SELECT generate_series(1, 52, 1) w) b ON b.w BETWEEN RIGHT(a.rstrt, 2)::INT AND RIGHT(a.rend, 2)::INT
ORDER BY LEFT(a.rstrt, 4)::INT, w
) sq
WHERE RN = 1;





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',
    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%2f53430769%2fsql-to-populate-data-for-missing-weeks%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









    0














    On PostgreSQL you can try something like the following.



    SELECT sq."year-week"
    , sq.orders
    FROM
    (
    SELECT LEFT(a.rstrt, 6) || W "year-week"
    , a.orders
    , ROW_NUMBER() OVER(PARTITION BY LEFT(a.rstrt, 6) || W ORDER BY a.rend DESC) RN
    FROM(SELECT "year-week" rstrt
    , COALESCE(LEAD ("year-week", 1) OVER(), 2018 || ' W' || 52) rend
    , orders
    FROM orders
    UNION
    SELECT '2019 W01', '2019 W52', 300) a
    LEFT JOIN (SELECT generate_series(1, 52, 1) w) b ON b.w BETWEEN RIGHT(a.rstrt, 2)::INT AND RIGHT(a.rend, 2)::INT
    ORDER BY LEFT(a.rstrt, 4)::INT, w
    ) sq
    WHERE RN = 1;





    share|improve this answer






























      0














      On PostgreSQL you can try something like the following.



      SELECT sq."year-week"
      , sq.orders
      FROM
      (
      SELECT LEFT(a.rstrt, 6) || W "year-week"
      , a.orders
      , ROW_NUMBER() OVER(PARTITION BY LEFT(a.rstrt, 6) || W ORDER BY a.rend DESC) RN
      FROM(SELECT "year-week" rstrt
      , COALESCE(LEAD ("year-week", 1) OVER(), 2018 || ' W' || 52) rend
      , orders
      FROM orders
      UNION
      SELECT '2019 W01', '2019 W52', 300) a
      LEFT JOIN (SELECT generate_series(1, 52, 1) w) b ON b.w BETWEEN RIGHT(a.rstrt, 2)::INT AND RIGHT(a.rend, 2)::INT
      ORDER BY LEFT(a.rstrt, 4)::INT, w
      ) sq
      WHERE RN = 1;





      share|improve this answer




























        0












        0








        0







        On PostgreSQL you can try something like the following.



        SELECT sq."year-week"
        , sq.orders
        FROM
        (
        SELECT LEFT(a.rstrt, 6) || W "year-week"
        , a.orders
        , ROW_NUMBER() OVER(PARTITION BY LEFT(a.rstrt, 6) || W ORDER BY a.rend DESC) RN
        FROM(SELECT "year-week" rstrt
        , COALESCE(LEAD ("year-week", 1) OVER(), 2018 || ' W' || 52) rend
        , orders
        FROM orders
        UNION
        SELECT '2019 W01', '2019 W52', 300) a
        LEFT JOIN (SELECT generate_series(1, 52, 1) w) b ON b.w BETWEEN RIGHT(a.rstrt, 2)::INT AND RIGHT(a.rend, 2)::INT
        ORDER BY LEFT(a.rstrt, 4)::INT, w
        ) sq
        WHERE RN = 1;





        share|improve this answer















        On PostgreSQL you can try something like the following.



        SELECT sq."year-week"
        , sq.orders
        FROM
        (
        SELECT LEFT(a.rstrt, 6) || W "year-week"
        , a.orders
        , ROW_NUMBER() OVER(PARTITION BY LEFT(a.rstrt, 6) || W ORDER BY a.rend DESC) RN
        FROM(SELECT "year-week" rstrt
        , COALESCE(LEAD ("year-week", 1) OVER(), 2018 || ' W' || 52) rend
        , orders
        FROM orders
        UNION
        SELECT '2019 W01', '2019 W52', 300) a
        LEFT JOIN (SELECT generate_series(1, 52, 1) w) b ON b.w BETWEEN RIGHT(a.rstrt, 2)::INT AND RIGHT(a.rend, 2)::INT
        ORDER BY LEFT(a.rstrt, 4)::INT, w
        ) sq
        WHERE RN = 1;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 8:28

























        answered Nov 22 '18 at 19:29









        Newbie92Newbie92

        83




        83
































            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%2f53430769%2fsql-to-populate-data-for-missing-weeks%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

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]