smart way to create a master list (avoiding cross joins)












0















I need to create a Table of date,product and inventory count only for the days inventory 0 , something like this



Date Product store Inv
Jan1 1 1 0
Feb4 1 1 0


From the inventory table that only has a record whenever inventory changes
Like this



 Store Product start_date end_date Inv
1 1 Jan 4 Jan10 5
1 1 Jan10 jan 15 4


I know I can create a master table by cross joining all store,product and calendar days in a year and then join only with days where date falls between start and end date of the inventory table. Is there a better way than this ? Can cross join be avoided ? Thanks










share|improve this question

























  • I am using netezza

    – mezz
    Nov 19 '18 at 18:58






  • 1





    Please provide a sample set of desired input and output records- February is not mentioned in your input, and the two dates mentioned in your output seems ‘out of the blue’ ?

    – Lars G Olsen
    Nov 21 '18 at 17:30
















0















I need to create a Table of date,product and inventory count only for the days inventory 0 , something like this



Date Product store Inv
Jan1 1 1 0
Feb4 1 1 0


From the inventory table that only has a record whenever inventory changes
Like this



 Store Product start_date end_date Inv
1 1 Jan 4 Jan10 5
1 1 Jan10 jan 15 4


I know I can create a master table by cross joining all store,product and calendar days in a year and then join only with days where date falls between start and end date of the inventory table. Is there a better way than this ? Can cross join be avoided ? Thanks










share|improve this question

























  • I am using netezza

    – mezz
    Nov 19 '18 at 18:58






  • 1





    Please provide a sample set of desired input and output records- February is not mentioned in your input, and the two dates mentioned in your output seems ‘out of the blue’ ?

    – Lars G Olsen
    Nov 21 '18 at 17:30














0












0








0








I need to create a Table of date,product and inventory count only for the days inventory 0 , something like this



Date Product store Inv
Jan1 1 1 0
Feb4 1 1 0


From the inventory table that only has a record whenever inventory changes
Like this



 Store Product start_date end_date Inv
1 1 Jan 4 Jan10 5
1 1 Jan10 jan 15 4


I know I can create a master table by cross joining all store,product and calendar days in a year and then join only with days where date falls between start and end date of the inventory table. Is there a better way than this ? Can cross join be avoided ? Thanks










share|improve this question
















I need to create a Table of date,product and inventory count only for the days inventory 0 , something like this



Date Product store Inv
Jan1 1 1 0
Feb4 1 1 0


From the inventory table that only has a record whenever inventory changes
Like this



 Store Product start_date end_date Inv
1 1 Jan 4 Jan10 5
1 1 Jan10 jan 15 4


I know I can create a master table by cross joining all store,product and calendar days in a year and then join only with days where date falls between start and end date of the inventory table. Is there a better way than this ? Can cross join be avoided ? Thanks







sql netezza






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 19:00









a_horse_with_no_name

297k46452547




297k46452547










asked Nov 19 '18 at 18:19









mezzmezz

72129




72129













  • I am using netezza

    – mezz
    Nov 19 '18 at 18:58






  • 1





    Please provide a sample set of desired input and output records- February is not mentioned in your input, and the two dates mentioned in your output seems ‘out of the blue’ ?

    – Lars G Olsen
    Nov 21 '18 at 17:30



















  • I am using netezza

    – mezz
    Nov 19 '18 at 18:58






  • 1





    Please provide a sample set of desired input and output records- February is not mentioned in your input, and the two dates mentioned in your output seems ‘out of the blue’ ?

    – Lars G Olsen
    Nov 21 '18 at 17:30

















I am using netezza

– mezz
Nov 19 '18 at 18:58





I am using netezza

– mezz
Nov 19 '18 at 18:58




1




1





Please provide a sample set of desired input and output records- February is not mentioned in your input, and the two dates mentioned in your output seems ‘out of the blue’ ?

– Lars G Olsen
Nov 21 '18 at 17:30





Please provide a sample set of desired input and output records- February is not mentioned in your input, and the two dates mentioned in your output seems ‘out of the blue’ ?

– Lars G Olsen
Nov 21 '18 at 17:30












2 Answers
2






active

oldest

votes


















0














Are you looking for lag():



select t.*
from (select t.*,
lag(inventory) over (partition by product, store order by date) as prev_inventory
from t
) t
where prev_inventory is null or prev_inventory <> inventory;





share|improve this answer































    0














    Create a table with dates (which can be handy to have around for a ton of reasons) then left join from the inventory table and use BETWEEN against your date columns.






    share|improve this answer
























    • could you elaborate your suggestion, please ?

      – mezz
      Nov 21 '18 at 19:49











    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%2f53380513%2fsmart-way-to-create-a-master-list-avoiding-cross-joins%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









    0














    Are you looking for lag():



    select t.*
    from (select t.*,
    lag(inventory) over (partition by product, store order by date) as prev_inventory
    from t
    ) t
    where prev_inventory is null or prev_inventory <> inventory;





    share|improve this answer




























      0














      Are you looking for lag():



      select t.*
      from (select t.*,
      lag(inventory) over (partition by product, store order by date) as prev_inventory
      from t
      ) t
      where prev_inventory is null or prev_inventory <> inventory;





      share|improve this answer


























        0












        0








        0







        Are you looking for lag():



        select t.*
        from (select t.*,
        lag(inventory) over (partition by product, store order by date) as prev_inventory
        from t
        ) t
        where prev_inventory is null or prev_inventory <> inventory;





        share|improve this answer













        Are you looking for lag():



        select t.*
        from (select t.*,
        lag(inventory) over (partition by product, store order by date) as prev_inventory
        from t
        ) t
        where prev_inventory is null or prev_inventory <> inventory;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 18:36









        Gordon LinoffGordon Linoff

        773k35306408




        773k35306408

























            0














            Create a table with dates (which can be handy to have around for a ton of reasons) then left join from the inventory table and use BETWEEN against your date columns.






            share|improve this answer
























            • could you elaborate your suggestion, please ?

              – mezz
              Nov 21 '18 at 19:49
















            0














            Create a table with dates (which can be handy to have around for a ton of reasons) then left join from the inventory table and use BETWEEN against your date columns.






            share|improve this answer
























            • could you elaborate your suggestion, please ?

              – mezz
              Nov 21 '18 at 19:49














            0












            0








            0







            Create a table with dates (which can be handy to have around for a ton of reasons) then left join from the inventory table and use BETWEEN against your date columns.






            share|improve this answer













            Create a table with dates (which can be handy to have around for a ton of reasons) then left join from the inventory table and use BETWEEN against your date columns.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 1:56









            joebjoeb

            2,20611519




            2,20611519













            • could you elaborate your suggestion, please ?

              – mezz
              Nov 21 '18 at 19:49



















            • could you elaborate your suggestion, please ?

              – mezz
              Nov 21 '18 at 19:49

















            could you elaborate your suggestion, please ?

            – mezz
            Nov 21 '18 at 19:49





            could you elaborate your suggestion, please ?

            – mezz
            Nov 21 '18 at 19:49


















            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%2f53380513%2fsmart-way-to-create-a-master-list-avoiding-cross-joins%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

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)