DECODE - Need explanation how this works
615711Jun 4 2009 — edited Jun 4 2009I have inherited some code that I do not fully understand and am hoping one of you gurus can explain it to me. The decode stmt pivots columns to rows and helps match up data to the qry above it. Please explain what the decode is doing. The stmt syntax and explanations in the manuals has not helped me. Also, if there is a better way to do this in 10g, I'm open to suggestion. I've enclosed dummy output as well. Thanks and please let me know if you have any questions.
SELECT pp.MONTH, pp.sg_code, pp.acct_num, pp.acct_description,
TO_CHAR (pp.totals, '999,999,999.99') AS pkt_price_totals,
TO_CHAR (accr.amt, '999,999,999.99') AS accrual_totals,
TO_CHAR (accr.amt - pp.totals, '999,999,999.99') AS difference
FROM (SELECT dd.month_descr AS MONTH, rm.selling_group_code AS sg_code,
pad.acct_num AS acct_num, pad.acct_descr AS acct_description,
ROUND (SUM (sef.expense_dtl_amt), 2) AS totals
FROM day_dim dd,
restated_market rm,
ppdss_acct_dim pad,
sales_expense_fact sef,
non_restated_market nrm,
ppdss_expense_profile_dim pep
WHERE nrm.selling_group_code IN('119000', '125000','141000','424000')
AND dd.month_ending_date = TO_DATE ('25-APR-2009', 'DD-MON-YYYY')
AND pad.acct_num BETWEEN 1 AND 30
AND pep.actual_expense_flag = 'Y'
AND sef.ppdss_acct_key = pad.ppdss_acct_key
AND sef.override_market_key = nrm.non_restated_market_key
AND sef.restated_market_key = rm.restated_market_key
AND pep.expense_profile_key = sef.expense_profile_key
AND sef.expense_payment_month_day_key = dd.day_key
GROUP BY dd.month_descr,
rm.selling_group_code,
pad.acct_num,
pad.acct_descr) pp,
(SELECT sg,
DECODE (l, 1, '1' ,
DECODE(l, 2, '2',
DECODE(l, 3, '3',
DECODE(l, 4, '4',
DECODE(l, 5, '5',
DECODE(l, 6, '6',
DECODE(l, 7, '7',
DECODE(l, 8, '8',
DECODE(l, 9, '9',
DECODE(l, 10, '10',
DECODE(l, 11, '11',
DECODE(l, 12, '12',
DECODE(l, 13, '13',
DECODE(l, 14, '14',
DECODE(l, 15, '15',
DECODE(l, 16, '16',
DECODE(l, 17, '17',
DECODE(l, 18, '18',
DECODE(l, 19, '19',
DECODE(l, 20, '20',
DECODE(l, 21, '21' ))))))))))))))))))))) ACCT ,
DECODE ( l, 1, "1" ,
DECODE(l, 2, "2",
DECODE(l, 3, "3",
DECODE(l, 4, "4",
DECODE(l, 5, "5",
DECODE(l, 6, "6",
DECODE(l, 7, "7",
DECODE(l, 8, "8",
DECODE(l, 9, "9",
DECODE(l, 10, "10",
DECODE(l, 11, "11",
DECODE(l, 12, "12",
DECODE(l, 13, "13",
DECODE(l, 14, "14",
DECODE(l, 15, "15",
DECODE(l, 16, "16",
DECODE(l, 17, "17",
DECODE(l, 18, "18",
DECODE(l, 19, "19",
DECODE(l, 20, "20",
DECODE(l, 21, "21" ))))))))))))))))))))) AMT
FROM
(select nrm.selling_group_code as "SG",
DD.MONTH_ENDING_DATE as "DATE",
sum(NVL(INV.GROSS_SALES_AMOUNT,0)) as "1",
sum(NVL(INV.POUNDS_SHIPPED,0)) as "2",
sum(inv.Freight_Amount
+ aa.Drayage
+ aa.Outbound_Freight
+ aa.Consigned_Freight
+ aa.Stevedoring_Accr
+ aa.MDF_Frt_Fwd_Insurance) as "3",
sum(inv.cases_shipped
+ aa.adj_cases_shipped) as "4",
sum(aa.Primary_Brokerage
+ aa.Brokerage_Secondary
+ aa.Brokerage_Variance
+ aa.MDF_Incent_Brokerage) as "5",
sum(NVL(INV.INVENTORY_COST_OF_SALES,0)) as "6",
sum (aa.Accr_Bus_Division_Expense
+ aa.Sale_No_Charge_Pallet
+ aa.Accr_Direct_Prod_Grp_Expense
+ aa.Accr_Sales_Marketing_Admn) as "7",
sum(inv.ALLOW_AMOUNT) as "8",
sum(aa.Corporate_GNA
+ aa.Division_GNA) as "9",
sum(aa.Shuttle_Freight) as "10",
sum(aa.Outside_Freezer)as "11",
sum(aa.Tyson_Freezer_Charges)as "12",
sum(aa.Accr_Corp_Marketing) as "13",
sum(aa.Hedging_Result) as "14",
sum(aa.ADVERTISING) as "15",
sum(aa.DEMO) as "16",
sum(aa.MDF_SALE_RELATED_EXPENSE) as "17",
sum(aa.MDF_PROFESSIONAL_SERVICES) as "18",
sum(aa.MDF_PRODUCT_RELATED_EXPENSE) as "19",
sum(aa.GENERAL_EVENT) as "20",
sum(aa.PROMO_FEE) as "21"
from invoice_fact inv, accrual_adjustment aa, day_dim dd, non_restated_market nrm, non_restated_product nrp
where inv.SALES_ORDER_LINE_KEY = aa.SALES_ORDER_LINE_KEY
and inv.invoice_DAY_KEY = dd.DAY_KEY
and inv.override_MARKET_KEY = nrm.non_RESTATED_MARKET_KEY
and inv.override_product_key = nrp.non_RESTATED_PRODUCT_KEY
and dd.month_ending_date = to_date('25-APR-2009','DD-MON-YYYY')
and nrm.SELLING_GROUP_CODE in ('119000','125000','141000','424000')
and nrp.MINOR_LINE_CODE <> '088'
and inv.SAMPLE_FLAG <> 'Y'
group by nrm.SELLING_GROUP_CODE, DD.MONTH_ENDING_DATE
ORDER BY nRM.SELLING_GROUP_CODE),
(SELECT level l FROM DUAL X CONNECT BY LEVEL <=21) ) accr
where pp.sg_code = accr.sg and pp.acct_num = accr.acct
ORDER BY pp.sg_code, pp.acct_num;
PRODUCES THIS OUTPUT:
MONTH SG_CODE ACCT_NUM ACCT_DESCRIPTION PKT_PRICE_TOTALS ACCRUAL_TOTALS DIFFERENCE
APRFY2009 119000 1 GROSS SALES 888.78 888.78 .00
APRFY2009 119000 2 POUNDS SHIPPED 329.67 329.67 .00
APRFY2009 119000 3 CUSTOMER FREIGHT 476.49 476.49 .00
APRFY2009 119000 4 CASES SHIPPED 566.00 566.00 .00
APRFY2009 119000 5 BROKERAGE 388.48 388.48 .00
APRFY2009 119000 6 INVENTORY COST OF 35.67 35.67 .00
APRFY2009 119000 7 TOTAL OTHER SALES 132.48 132.48 .00
APRFY2009 119000 8 ALLOWANCES/SURCHAR 976.86 976.86 .00
APRFY2009 119000 9 TOTAL GNA 871.93 871.93 .00
APRFY2009 119000 10 SHUTTLE FREIGHT 334.82 334.82 .00
APRFY2009 119000 11 OUTSIDE FREEZER 11.96 11.96 .00
APRFY2009 119000 12 FREEZER 32.32 32.32 .00
APRFY2009 119000 13 CORPORATE MARKETIN 317.07 317.07 .00
APRFY2009 119000 20 GENERAL MDF 654.22 654.22 .00