The combination refCursors and summary

Given the following DDL:
CREATE TABLE  FOODSALESTEST 
("WEEKNBR" NUMBER, 
 "STOREID" NUMBER NOT NULL, 
 "GUESTS" NUMBER NOT NULL, 
 "TACOSALES" NUMBER NOT NULL, 
 "SOUPYSALES" NUMBER NOT NULL); 

Insert into FoodSalesTest VALUES (1,74,302,3021.71,3021.71);
Insert into FoodSalesTest VALUES (1,83,231,2106.58,2106.58);
Insert into FoodSalesTest VALUES (1,101,251,2163.5,2163.5);
Insert into FoodSalesTest VALUES (1,113,298,2953.82,2953.82);
Insert into FoodSalesTest VALUES (2,74,131,1168.11,1168.11);
Insert into FoodSalesTest VALUES (2,83,156,1258.22,1258.22);
Insert into FoodSalesTest VALUES (2,101,164,1550.87,1550.87);
Insert into FoodSalesTest VALUES (2,113,129,1384.26,1384.26);
Insert into FoodSalesTest VALUES (3,74,144,1594.02,1594.02);
Insert into FoodSalesTest VALUES (3,83,196,1867.2,1867.2);
Insert into FoodSalesTest VALUES (3,101,196,2052.96,2052.96);
Insert into FoodSalesTest VALUES (3,113,142,1490.63,1490.63);
Insert into FoodSalesTest VALUES (7,74,192,2496.17,2496.17);
Insert into FoodSalesTest VALUES (7,83,178,1971.54,1971.54);
Insert into FoodSalesTest VALUES (7,101,294,3477.88,3477.88);
Insert into FoodSalesTest VALUES (7,103,336,3398.42,3398.42);
--=== 
CREATE OR REPLACE  PROCEDURE  GETWEEKLYFOODSALES 
    (inweeknbr         number
                   ,  p_rcWeekly      in  out sys_refcursor
 )  is
BEGIN
OPEN   p_rcWeekly FOR
  -- This has been simplified and as is not very practical. 
  -- It is actually a huge select statement with 6 "WITH" clauses and 15 joins and many selection criteria parameters
   Select * from FoodSalesTest where weeknbr = inWeekNbr;
END GetWeeklyFoodSales;

--===
 CREATE OR REPLACE  PROCEDURE GETALLFOODSALES( 
                   NbrOfWeeks       In number
                ,  p_rcWeek1        Out sys_refcursor
                ,  p_rcWeek2        out sys_refcursor
                ,  p_rcWeek3        out sys_refcursor
                ,  p_rcWeek7        out sys_refcursor
) is
begin
                 GetWeeklyFoodSales( 1 , p_rcWeek1);
                 GetWeeklyFoodSales( 2 , p_rcWeek2);
                 if NbrOfWeeks = 3 then
                    GetWeeklyFoodSales( 3 , p_rcWeek3);
                 end if;
                 GetWeeklyFoodSales( 7 , p_rcWeek7);
END GetAllFoodSales;
GetWeeklyfoodSales is actually a huge select statement with 6 'WITH' Guide to the clauses and 15 joints and many parameters of selection criteria. It's probably possible to combine all the features in stored procedure 1 but I want to modularize.

(1) how can I select all the sliders in 1 single slider in GetAllFoodSales?

(2) why can't I do that? = Why the Cursor is not maintained because it is defined as IN GetWeeklyFoodSales?


GetWeeklyFoodSales (1, p_TheOnlyCursor);
GetWeeklyFoodSales (2, p_TheOnlyCursor);
If NbrOfWeeks = 3 then
GetWeeklyFoodSales (3, p_TheOnlyCursor);
end if;
GetWeeklyFoodSales (7, p_TheOnlyCursor);

WHERE the called procedure has these settings:
inweeknbr in numbers
p_rcWeekly IN OUT sys_refcursor

(3) I also have 2 output slider more in GetAllFoodSales. Description:
1 p_SummedResultsByStoreID
2 p_SummedResultsByWeekNumber

These must be separated from the sliders for the program to use to report

OK, let's go nothing {noformat} * takes a deep breath * {noformat}

WITH weekly_totals AS (SELECT fk_str_main_id AS storeid,
                             TRUNC(busi_date, iw) week_date, --- may need to add / subtract days if your start of week is not Monday, eg. Tuesday would be (TRUNC(busi_date - 1, iw) + 1
                             SUM(csh.conf_numb2) AS net_sales,
                             SUM(conf_numb49) AS largejuices_food_sales,
                             SUM(conf_numb44) AS total_drink_sales,
                             SUM(csh.conf_numb2)
                               - SUM(conf_numb44)
                               - SUM(conf_numb3)
                               - SUM(conf_numb4) AS net_food,
                             SUM(conf_numb38) AS aw_sales,
                             SUM(conf_numb56) AS tatoebowl_sales,
                             NVL(SUM(conf_numb14 + conf_numb15), 0) AS charge_ticket_avg,
                             NVL(SUM(conf_numb32), 0) AS labor_hourly,
                             NVL(SUM(conf_numb18), 0) AS charge_count,
                             NVL(SUM(conf_numb36), 0) AS labor_management,
                             NVL(SUM(conf_numb23), 0) AS coupon_count,
                             NVL(SUM(conf_numb24), 0) AS coupon_dollars,
                             NVL(SUM(conf_numb1), 0) AS ticket_ave,--=== Also known as head_avg
                             NVL(SUM(conf_numb33), 0) AS chickenheads,
                             NVL(SUM(conf_numb34), 0) AS pieces_sold,
                             NVL(SUM(conf_numb35), 0) AS pieces_scrapped,
                             NVL(SUM(conf_numb33)
                                 - (SUM(conf_numb35) + SUM(conf_numb34)), 0) AS pieces_unaccounted,
                             NVL(SUM(conf_numb33)/8, 0) AS chickenusage,
                             NVL(SUM(conf_numb35)/8, 0) AS scrappeddiv8,
                             NVL((SUM(conf_numb33)
                                  - (SUM(conf_numb35) + SUM(conf_numb34)))/8, 0) AS unaccounteddiv8,
                             NVL(SUM(conf_numb2
                                     + conf_numb5
                                     + conf_numb6
                                     + conf_numb8
                                     + conf_numb9
                                     + conf_numb11), 0) AS cashacc,
                             NVL(SUM(conf_numb12
                                     + conf_numb13
                                     + conf_numb14
                                     + conf_numb15
                                     + conf_numb16
                                     + conf_numb17
                                     + conf_numb21
                                     + conf_numb37
                                     + conf_numb42), 0) AS totdep,
                             NVL(SUM(conf_numb19 + conf_numb20), 0) AS cash,
                             NVL(SUM(conf_numb31), 0) AS hourly_labor_count
                      FROM   csh_main csh
                      WHERE busi_date BETWEEN v_bow AND v_eow + p_no_of_weeks*7
                      GROUP BY storeid,
                               TRUNC(busi_date, iw)),
weeklyfoodsalesly AS (SELECT storeid AS storeid,
                             TRUNC(busi_date, iw) week_date,
                             SUM(csh.conf_numb2) AS net_sales
                      FROM   csh_main csh
                      WHERE  busi_date BETWEEN bow_ly AND eow_ly + p_no_of_weeks*7
                      GROUP BY storeid,
                               TRUNC(busi_date, iw)),
     categorycost AS (SELECT storeid,
                             week_date,
                             UPPER(description) description,
                             SUM(NVL(prev_delv_cost
                                     + transfer_in_cost
                                     + delv_cost
                                     - transfer_out_cost
                                     - total_cost, 0)) AS cost
                      FROM   (SELECT storeid AS storeid,
                                     trunc(busi_date, 'iw') week_date,
                                     cat.text_description AS description,
                                     ROUND(SUM(transfer_in_cost), 3) AS transfer_in_cost,
                                     ROUND(SUM(transfer_out_cost), 3) AS transfer_out_cost,
                                     ROUND(SUM(delv_cost), 3) AS delv_cost,
                                     ROUND(SUM(prev_delv_cost), 3) AS prev_delv_cost,
                                     ROUND(SUM(total_cost), 3) AS total_cost
                              FROM   inv_main inv
                                     INNER JOIN inv_fin_detail detail ON inv.primary_key = detail.fk_header_id
                                     INNER JOIN fit_main fit ON detail.fk_fit_main_id = fit.primary_key
                                     INNER JOIN lk_food_item_category cat ON cat.primary_key = fit.fk_lk_fit_category_id
                              WHERE  busi_date BETWEEN bow AND eow + p_no_of_weeks*7
                              GROUP BY inv.storeid,
                                       cat.text_description)),
 pivoted_cat_cost AS (SELECT storeid,
                             week_date,
                             NVL(MAX(DECODE(description, 'CHICKEN', cost)), 0) chicken,
                             NVL(MAX(DECODE(description, 'FILETS', cost)), 0) filets,
                             NVL(MAX(DECODE(description, 'POPCORN', cost)), 0) popcorn,
                             NVL(MAX(DECODE(description, 'STRIPS', cost)), 0) strips,
                             NVL(MAX(DECODE(description, 'SPECIAL', cost)), 0) special,
                             NVL(MAX(DECODE(description, 'WINGS', cost)), 0) wings,
                             NVL(MAX(DECODE(description, 'SHORTENING', cost)), 0) shortening,
                             NVL(MAX(DECODE(description, 'FLOUR/SEAS', cost)), 0) flour,
                             NVL(MAX(DECODE(description, 'BISCUITS', cost)), 0) biscuits,
                             NVL(MAX(DECODE(description, 'WD/FRY/OR', cost)), 0) friesonionrings,
                             NVL(MAX(DECODE(description, 'MASHED', cost)), 0) mashedpotatoes,
                             NVL(MAX(DECODE(description, 'DESSERTS', cost)), 0) desserts,
                             NVL(MAX(DECODE(description, 'BEVERAGES', cost)), 0) drinks,
                             NVL(MAX(DECODE(description, 'CORN', cost)), 0) corn,
                             NVL(MAX(DECODE(description, 'MISC. ENT', cost)), 0) miscentrees,
                             NVL(MAX(DECODE(description, 'SALADS', cost)), 0) salads,
                             NVL(MAX(DECODE(description, 'CONDIMENT', cost)), 0) condiments,
                             NVL(MAX(DECODE(description, 'PAPER', cost)), 0) paper,
                             NVL(MAX(DECODE(description, 'APPLEANDWINE', cost)), 0) awsandwiches,
                             NVL(MAX(DECODE(description, 'LARGEJUICES PROD', cost)), 0) largejuiceprod,
                             NVL(MAX(DECODE(description, 'TATOEBOWL PROD', cost)), 0) tatoebowlprod
                      FROM   categorycost
                      GROUP BY storeid,
                               week_date),
     storedetails AS (SELECT *
                      FROM   kettlefriedcorn_report_stores
                      WHERE  storename NOT LIKE '%CLOSED%'
                       ORDER BY districtid, storenbr)
SELECT foods.week_date,
       foods.storeid,
       ROUND(NVL(foods.net_sales
                 - (foods.aw_sales
                    + foods.tatoebowl_sales
                    + foods.largejuices_drinks
                    + foods.largejuices_food_sales), 0), 2) AS kettlefriedcorn_net_sales,
       ROUND(NVL(foods.aw_sales, 0), 2) AS aw_sales,
       ROUND(NVL(foods.tatoebowl_sales, 0), 2) AS tatoebowl_sales,
       ROUND(NVL(foods.largejuices_drinks + foods.largejuices_food_sales, 0), 2) AS largejuices_net_sales,
       ROUND(NVL(foods.net_sales, 0), 2) AS net_sales, -- Last Year Sales and Last Year Next Year Sales
       ROUND(NVL(foods.net_sales - foodsly.net_sales, 0), 2) AS wtdsalesincrease,
       cat.chicken,
       cat.filets,
       cat.popcorn,
       cat.strips,
       cat.special,
       cat.wings,
       cat.shortening,
       cat.flour,
       cat.biscuits,
       cat.friesonionrings,
       cat.mashedpotatoes,
       cat.desserts,
       cat.drinks,
       cat.corn,
       cat.miscentrees,
       cat.salads,
       cat.condiments,
       cat.paper,
       cat.awsandwiches,
       cat.largejuiceprod,
       cat.tatoebowlprod,
       ROUND(NVL(foods.labor_hourly, 0), 2) AS labor_hourly,
       ROUND(NVL(foods.labor_management, 0), 2) AS labor_management,
       ROUND(NVL(foods.charge_count, 0), 2) AS charge_count,
       CASE WHEN NVL(foods.charge_count, 0) = 0 THEN 0
            ELSE ROUND(NVL(foods.charge_ticket_avg / foods.charge_count, 0), 2)
       END AS charge_ticket_avg,
       ROUND(NVL(foods.coupon_count, 0), 2) AS coupon_count,
       ROUND(NVL(foods.coupon_dollars, 0), 2) AS coupon_dollars,
       CASE WHEN NVL(foods.ticket_ave, 0) = 0 THEN 0
            ELSE ROUND (NVL (foods.net_sales / foods.ticket_ave, 0), 2)
       END AS ticket_avg,
       ROUND(NVL(foods.chickenusage, 0), 2) AS chickenusage,
       CASE WHEN NVL(foods.chickenusage, 0) = 0 THEN 0
            ELSE ROUND (foods.net_sales / foods.chickenusage, 2)
       END AS head_average,
       ROUND(NVL(foods.pieces_scrapped, 0), 2) AS pieces_scrapped,
       ROUND(NVL(foods.pieces_unaccounted, 0), 2) AS pieces_unaccounted,
       ROUND(NVL(foods.pieces_sold, 0), 2) AS pieces_sold, --===
                                                         --  1 chicken = 8.3  pieces
                                                         --  the  ((pieces accounted) - (pieces scrapped)   /8)
                                                         --  to determine efficiencyh.
       ROUND(NVL(foods.scrappeddiv8, 0), 2) AS scrappeddiv8,
       ROUND(NVL(foods.unaccounteddiv8, 0), 2) AS unaccounteddiv8,
       CASE WHEN NVL(foods.chickenusage, 0) = 0 THEN 0
            ELSE ROUND(((foods.chickenusage - foods.scrappeddiv8 - foods.unaccounteddiv8)/foods.chickenusage) * 100, 2)
       END AS efficiency,
       (foods.cashacc + foods.totdep - foods.cash) * (-1) AS cashovershort,
       CASE WHEN TRUNC(MONTHS_BETWEEN(bow, stores.opendate)) < 12 THEN 0
            ELSE 1
       END AS isopenfor12months,
       CASE WHEN TRUNC(MONTHS_BETWEEN(bow, stores.opendate)) < 15 THEN 0
            ELSE 1
       END AS isopenfor15months,
       foodsly.week_date,
       ROUND(NVL(foodsly.net_sales, 0), 2) AS wtdlynetsales,
       stores.*
FROM  weekly_totals foods
       LEFT OUTER JOIN weeklyfoodsalesly foodsly ON foodsly.storeid = foods.storeid AND foodsly.week_date = DECODE(foods.week_date, bow, bow_ly,
                                                                                                                                   bow + 7, bow_ly + 7,
                                                                                                                                   bow + 14, bow_ly + 14,
                                                                                                                                   bow + 21, bow_ly + 21,
                                                                                                                                   bow + 28, bow_ly + 28)
       LEFT OUTER JOIN storedetails stores ON stores.storeid = foods.storeid
       LEFT OUTER JOIN pivoted_cat_cost cat ON cat.storeid = foods.storeid AND cat.week_date = foods.week_date
ORDER BY companyname,
         districtname,
         storenbr;

Published by: Boneist on October 20, 2009 17:31
ETA: fixed syntax errors - request should now run without syntax errors, well if there are issues of join is another matter!

Tags: Database

Similar Questions

Maybe you are looking for