smart way to create a master list (avoiding cross joins)
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
I am using netezza
– mezz
Nov 19 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 at 17:30
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
sql netezza
edited Nov 19 at 19:00
a_horse_with_no_name
289k46436532
289k46436532
asked Nov 19 at 18:19
mezz
74129
74129
I am using netezza
– mezz
Nov 19 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 at 17:30
add a comment |
I am using netezza
– mezz
Nov 19 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 at 17:30
I am using netezza
– mezz
Nov 19 at 18:58
I am using netezza
– mezz
Nov 19 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 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 at 17:30
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
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;
add a comment |
up vote
0
down vote
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.
could you elaborate your suggestion, please ?
– mezz
Nov 21 at 19:49
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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;
add a comment |
up vote
0
down vote
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;
add a comment |
up vote
0
down vote
up vote
0
down vote
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;
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;
answered Nov 19 at 18:36
Gordon Linoff
751k34286394
751k34286394
add a comment |
add a comment |
up vote
0
down vote
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.
could you elaborate your suggestion, please ?
– mezz
Nov 21 at 19:49
add a comment |
up vote
0
down vote
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.
could you elaborate your suggestion, please ?
– mezz
Nov 21 at 19:49
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Nov 21 at 1:56
joeb
2,05111519
2,05111519
could you elaborate your suggestion, please ?
– mezz
Nov 21 at 19:49
add a comment |
could you elaborate your suggestion, please ?
– mezz
Nov 21 at 19:49
could you elaborate your suggestion, please ?
– mezz
Nov 21 at 19:49
could you elaborate your suggestion, please ?
– mezz
Nov 21 at 19:49
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I am using netezza
– mezz
Nov 19 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 at 17:30