I have enclosed my SQL to see if anyone can offer a suggestion on how to accomplish my task. If you look at the creation of temporary table ZZMD07, the first on is what is being generated and the second on is what I would like it to be.
What I am attempting to do is find out what stores customers purchase in and rank them based on the amount of spending. Once I rank the customers stores, give me the sales in the number one store and give me the sales in all the other stores.
Where I am having trouble is on the report resolution. Trying to show the sales for the number one store (primary store) along with the other store sales on the same row. So that the report would look kind of like:
________________________________________________________________________________
Store# Str Sales Other Store Sales
Store number 001 $48,000.00 $10,000
Hopefully, you can help? Thank you for all suggestions.
/* This step ranks a customer’s spending store based on sales within that store */
create global temporary table ZZAM00
on commit preserve rows as
select a13.HH_ADDR_KEY HH_ADDR_KEY,
a11.LOC_KEY LOC_KEY,
rank () over(partition by a13.HH_ADDR_KEY order by sum(a11.NET_SALES_AMT) asc) WJXBFS1
from F_LC_STR_TRANS_WK_VW a11,
D_TF_WEEK_VW a12,
D_CARD_VW a13,
D_STORE_VW a14
where a11.TIMEFRAME_KEY = a12.WK_KEY and
a11.LYLTY_CARD_KEY = a13.LYLTY_CARD_KEY and
a11.LOC_KEY = a14.LOC_KEY
and (a14.REGION_DESC in ('EASTERN')
and a12.PRD_KEY in (10792)
and a11.LYLTY_CARD_KEY > 0)
group by a13.HH_ADDR_KEY,
a11.LOC_KEY
/* This step returns only a customer’s number 1 spending store */
create global temporary table ZZMQ01
on commit preserve rows as
select pa1.HH_ADDR_KEY HH_ADDR_KEY,
pa1.LOC_KEY LOC_KEY
from ZZAM00 pa1
where (pa1.WJXBFS1 = 1)
/* This step builds the TEMP table for metric collection */
create global temporary table ZZRF02
on commit preserve rows as
select distinct pa2.HH_ADDR_KEY HH_ADDR_KEY,
pa2.LOC_KEY LOC_KEY
from ZZMQ01 pa2,
D_CARD_VW r11,
D_TF_PRD_VW r12,
D_STORE_VW r13
where pa2.HH_ADDR_KEY = r11.HH_ADDR_KEY and
pa2.LOC_KEY = r13.LOC_KEY
and (r13.REGION_DESC in ('EASTERN')
and r12.PRD_KEY in (10792)
and r11.LYLTY_CARD_KEY > 0)
/* This step ranks a customer’s spending store based on sales within that store */
create global temporary table ZZAM03
on commit preserve rows as
select a13.HH_ADDR_KEY HH_ADDR_KEY,
a11.LOC_KEY LOC_KEY,
rank () over(partition by a13.HH_ADDR_KEY order by sum(a11.NET_SALES_AMT) asc) WJXBFS1
from F_LC_STR_TRANS_WK_VW a11,
D_TF_WEEK_VW a12,
D_CARD_VW a13,
D_STORE_VW a14
where a11.TIMEFRAME_KEY = a12.WK_KEY and
a11.LYLTY_CARD_KEY = a13.LYLTY_CARD_KEY and
a11.LOC_KEY = a14.LOC_KEY
and (a14.REGION_DESC in ('EASTERN')
and a12.PRD_KEY in (10792)
and a11.LYLTY_CARD_KEY > 0)
group by a13.HH_ADDR_KEY,
a11.LOC_KEY
/* This step returns all of a customer’s spending stores except the number 1 ranked store */
create global temporary table ZZMQ04
on commit preserve rows as
select pa4.HH_ADDR_KEY HH_ADDR_KEY,
pa4.LOC_KEY LOC_KEY
from ZZAM03 pa4
where (pa4.WJXBFS1 > 1)
/* This step builds the TEMP table for metric collection */
create global temporary table ZZRF05
on commit preserve rows as
select distinct pa5.HH_ADDR_KEY HH_ADDR_KEY
from ZZMQ04 pa5,
D_CARD_VW r11,
D_STORE_VW r12,
D_TF_PRD_VW r13
where pa5.HH_ADDR_KEY = r11.HH_ADDR_KEY and
pa5.LOC_KEY = r12.LOC_KEY
and (r12.REGION_DESC in ('EASTERN')
and r13.PRD_KEY in (10792)
and r11.LYLTY_CARD_KEY > 0)
/* This step collects the net sales for the customer’s number 1 store */
create global temporary table ZZMD06
on commit preserve rows as
select a11.LOC_KEY LOC_KEY,
sum(a11.NET_SALES_AMT) BASKETNETSAL
from F_LC_STR_TRANS_WK_VW a11,
D_CARD_VW a12,
ZZRF02 pa3
where a11.LYLTY_CARD_KEY = a12.LYLTY_CARD_KEY and
a11.LOC_KEY = pa3.LOC_KEY and
a12.HH_ADDR_KEY = pa3.HH_ADDR_KEY
group by a11.LOC_KEY
/* This step collects the net sales for all the other customer’s store */
/* This is what is generated */
create global temporary table ZZMD07
on commit preserve rows as
select a11.LOC_KEY LOC_KEY,
sum(a11.NET_SALES_AMT) BASKETNETSAL
from F_LC_STR_TRANS_WK_VW a11,
D_CARD_VW a12,
ZZRF05 pa6
where a11.LYLTY_CARD_KEY = a12.LYLTY_CARD_KEY and
a12.HH_ADDR_KEY = pa6.HH_ADDR_KEY
group by a11.LOC_KEY
/* This is what I would like generated */
create global temporary table ZZMD07
on commit preserve rows as
select pa7.LOC_KEY LOC_KEY,
sum(a11.NET_SALES_AMT) BASKETNETSAL
from F_LC_STR_TRANS_WK_VW a11,
D_CARD_VW a12,
ZZRF05 pa6,
ZZRF02 pa7
where a11.LYLTY_CARD_KEY = a12.LYLTY_CARD_KEY and
a12.HH_ADDR_KEY = pa6.HH_ADDR_KEY and
pa6.HH_ADDR_KEY = pa7.HH_ADDR_KEY
group by pa7.LOC_KEY
/* This step builds the report */
select pa7.LOC_KEY LOC_KEY,
a11.LOC_DESC LOC_DESC,
pa7.BASKETNETSAL BASKETNETSAL,
pa8.BASKETNETSAL BASKETNETSAL1
from ZZMD06 pa7,
ZZMD07 pa8,
D_STORE_VW a11
where pa7.LOC_KEY = pa8.LOC_KEY and
pa7.LOC_KEY = a11.LOC_KEY
What I am attempting to do is find out what stores customers purchase in and rank them based on the amount of spending. Once I rank the customers stores, give me the sales in the number one store and give me the sales in all the other stores.
Where I am having trouble is on the report resolution. Trying to show the sales for the number one store (primary store) along with the other store sales on the same row. So that the report would look kind of like:
________________________________________________________________________________
Store# Str Sales Other Store Sales
Store number 001 $48,000.00 $10,000
Hopefully, you can help? Thank you for all suggestions.
/* This step ranks a customer’s spending store based on sales within that store */
create global temporary table ZZAM00
on commit preserve rows as
select a13.HH_ADDR_KEY HH_ADDR_KEY,
a11.LOC_KEY LOC_KEY,
rank () over(partition by a13.HH_ADDR_KEY order by sum(a11.NET_SALES_AMT) asc) WJXBFS1
from F_LC_STR_TRANS_WK_VW a11,
D_TF_WEEK_VW a12,
D_CARD_VW a13,
D_STORE_VW a14
where a11.TIMEFRAME_KEY = a12.WK_KEY and
a11.LYLTY_CARD_KEY = a13.LYLTY_CARD_KEY and
a11.LOC_KEY = a14.LOC_KEY
and (a14.REGION_DESC in ('EASTERN')
and a12.PRD_KEY in (10792)
and a11.LYLTY_CARD_KEY > 0)
group by a13.HH_ADDR_KEY,
a11.LOC_KEY
/* This step returns only a customer’s number 1 spending store */
create global temporary table ZZMQ01
on commit preserve rows as
select pa1.HH_ADDR_KEY HH_ADDR_KEY,
pa1.LOC_KEY LOC_KEY
from ZZAM00 pa1
where (pa1.WJXBFS1 = 1)
/* This step builds the TEMP table for metric collection */
create global temporary table ZZRF02
on commit preserve rows as
select distinct pa2.HH_ADDR_KEY HH_ADDR_KEY,
pa2.LOC_KEY LOC_KEY
from ZZMQ01 pa2,
D_CARD_VW r11,
D_TF_PRD_VW r12,
D_STORE_VW r13
where pa2.HH_ADDR_KEY = r11.HH_ADDR_KEY and
pa2.LOC_KEY = r13.LOC_KEY
and (r13.REGION_DESC in ('EASTERN')
and r12.PRD_KEY in (10792)
and r11.LYLTY_CARD_KEY > 0)
/* This step ranks a customer’s spending store based on sales within that store */
create global temporary table ZZAM03
on commit preserve rows as
select a13.HH_ADDR_KEY HH_ADDR_KEY,
a11.LOC_KEY LOC_KEY,
rank () over(partition by a13.HH_ADDR_KEY order by sum(a11.NET_SALES_AMT) asc) WJXBFS1
from F_LC_STR_TRANS_WK_VW a11,
D_TF_WEEK_VW a12,
D_CARD_VW a13,
D_STORE_VW a14
where a11.TIMEFRAME_KEY = a12.WK_KEY and
a11.LYLTY_CARD_KEY = a13.LYLTY_CARD_KEY and
a11.LOC_KEY = a14.LOC_KEY
and (a14.REGION_DESC in ('EASTERN')
and a12.PRD_KEY in (10792)
and a11.LYLTY_CARD_KEY > 0)
group by a13.HH_ADDR_KEY,
a11.LOC_KEY
/* This step returns all of a customer’s spending stores except the number 1 ranked store */
create global temporary table ZZMQ04
on commit preserve rows as
select pa4.HH_ADDR_KEY HH_ADDR_KEY,
pa4.LOC_KEY LOC_KEY
from ZZAM03 pa4
where (pa4.WJXBFS1 > 1)
/* This step builds the TEMP table for metric collection */
create global temporary table ZZRF05
on commit preserve rows as
select distinct pa5.HH_ADDR_KEY HH_ADDR_KEY
from ZZMQ04 pa5,
D_CARD_VW r11,
D_STORE_VW r12,
D_TF_PRD_VW r13
where pa5.HH_ADDR_KEY = r11.HH_ADDR_KEY and
pa5.LOC_KEY = r12.LOC_KEY
and (r12.REGION_DESC in ('EASTERN')
and r13.PRD_KEY in (10792)
and r11.LYLTY_CARD_KEY > 0)
/* This step collects the net sales for the customer’s number 1 store */
create global temporary table ZZMD06
on commit preserve rows as
select a11.LOC_KEY LOC_KEY,
sum(a11.NET_SALES_AMT) BASKETNETSAL
from F_LC_STR_TRANS_WK_VW a11,
D_CARD_VW a12,
ZZRF02 pa3
where a11.LYLTY_CARD_KEY = a12.LYLTY_CARD_KEY and
a11.LOC_KEY = pa3.LOC_KEY and
a12.HH_ADDR_KEY = pa3.HH_ADDR_KEY
group by a11.LOC_KEY
/* This step collects the net sales for all the other customer’s store */
/* This is what is generated */
create global temporary table ZZMD07
on commit preserve rows as
select a11.LOC_KEY LOC_KEY,
sum(a11.NET_SALES_AMT) BASKETNETSAL
from F_LC_STR_TRANS_WK_VW a11,
D_CARD_VW a12,
ZZRF05 pa6
where a11.LYLTY_CARD_KEY = a12.LYLTY_CARD_KEY and
a12.HH_ADDR_KEY = pa6.HH_ADDR_KEY
group by a11.LOC_KEY
/* This is what I would like generated */
create global temporary table ZZMD07
on commit preserve rows as
select pa7.LOC_KEY LOC_KEY,
sum(a11.NET_SALES_AMT) BASKETNETSAL
from F_LC_STR_TRANS_WK_VW a11,
D_CARD_VW a12,
ZZRF05 pa6,
ZZRF02 pa7
where a11.LYLTY_CARD_KEY = a12.LYLTY_CARD_KEY and
a12.HH_ADDR_KEY = pa6.HH_ADDR_KEY and
pa6.HH_ADDR_KEY = pa7.HH_ADDR_KEY
group by pa7.LOC_KEY
/* This step builds the report */
select pa7.LOC_KEY LOC_KEY,
a11.LOC_DESC LOC_DESC,
pa7.BASKETNETSAL BASKETNETSAL,
pa8.BASKETNETSAL BASKETNETSAL1
from ZZMD06 pa7,
ZZMD07 pa8,
D_STORE_VW a11
where pa7.LOC_KEY = pa8.LOC_KEY and
pa7.LOC_KEY = a11.LOC_KEY