Substr/Instr/analytics question

I have a table like this on 9i (soon to be 10g)
CREATE TABLE BASELINE_TESTRUN
RUNID VARCHAR2(42 BYTE) NOT NULL
with data like this:
insert into BASELINE_TESTRUN values ('DEV1-XXX-01');
insert into BASELINE_TESTRUN values ('DEV1-XXX-03');
insert into BASELINE_TESTRUN values ('DEV2-XXX-01');
insert into BASELINE_TESTRUN values ('DEV2-XXX-02');
insert into BASELINE_TESTRUN values ('DEV2-XXX-03');
insert into BASELINE_TESTRUN values ('DEV2-XXX-05');
insert into BASELINE_TESTRUN values ('DEV2-XXX-06');
insert into BASELINE_TESTRUN values ('DEV2-XXX-06');
The output should be the next higher number of the last substring in runid
DEV1-XXX 04
DEV2-XXX 07
There may be missing no, and/or duplicates.
This was my Query for this - It works but seems rather clumsy. I would like to have done it in 1 SQL (avoiding the select from (select)):
select runid , max (next_runno) next_runno
from
select substr(runid,1,instr(runid,'-',1,2)-1) runid,
max(substr(runid,instr(runid,'-',1,2)+1))
over(partition by substr(runid,1,instr(runid,'-',1,2)-1)) + 1 next_runno
from baseline_testrun
where instr(runid,'-',1,2) > 0
---where runid like 'whatever%'
group by runid;
Alternate solutions would be appreciated ... and NO I did not define that table like this.
I would have preferred a sequence and a keys that was not composit (Codd/Date, pls. forgive the creators of the table)
best regards
Mette

You are grouping you data. In this case an aggregate function could be more helpful than an analytic function.
In 9i already you are be able to use the KEEP syntax for aggregates.
Look at this example
SQL> select substr(runid,1,instr(runid,'-',1,2)-1) group_run,
  2  to_number(
  3  max(substr(runid, instr(runid,'-',1,2)+1))
  4  keep (dense_rank last order by substr(runid, instr(runid,'-',1,2)))
  5  )+1 max_run
  6  from BASELINE_TESTRUN
  7  group by substr(runid,1,instr(runid,'-',1,2)-1);
GROUP_RUN                                     MAX_RUN
DEV1-XXX                                            4
DEV2-XXX                                            7
SQL> Dimas Version is better, since it avoids the keep syntax, that is not really needed for this special case.
Message was edited by:
Sven W.

Similar Messages

  • Dynamic PL/SQL & substr, instr function

    I am having trouble with incorporating the SUBSTR and INSTR functions into my dynamic PL/SQL procedure using Oracle 8i.
    I have data that is packed into one column seperated by a delimiter (':')
    I need to seperate the data to use indicidual pieces.
    If I run my query in general -
    select substr(secondcol, 1, instr(secondcol, ':',1,1)-1) ONE,
    substr(secondcol,instr(secondcol, ':',1,1)+1,instr(secondcol, ':',1,1)-1) TWO,
    substr(secondcol,instr(secondcol, ':',1,2)+1,instr(secondcol, ':',1,1)-1) THREE,
    substr(secondcol,instr(secondcol, ':',1,3)+1,instr(secondcol, ':',1,1)-1) FOUR
    from temp_table where firstcol=100
    This works and gives me the right result.
    e.g
    DATA :
    Firstcol SECONDCOL
    100 1:2:3:4
    Result:
    ONE TWO THREE FOUR
    1 2 3 4
    However to make this generic if I use it in a function passing it a parameter which has ':' delimited data it does not work and gives me errors. All I want is to get the output as a string that looks like my query above so I can use it in my proc.
    create or replace function MYJUNK(TFieldNew IN CHAR)
    RETURN CHAR IS
    UpdateString Varchar2(100);
    BEGIN
    UpdateString := 'First=substr('||TFieldNew||', 1, instr('||TFieldNew||', '':'',1,1)-1) ONE, ';
    UpdateString := UpdateString || ' Second=substr('||TFieldNew||', instr('||TFieldNew||', '':'',1,2)+1, instr('||TFieldNew||', '':'',1,1)-1) TWO, ';
    UpdateString := UpdateString || ' third=substr('||TFieldNew||', instr('||TFieldNew||', '':'',1,3)+1, instr('||TFieldNew||', '':'',1,1)-1) THREE from temp_table';
    return UpdateString;
    END;
    The function compiles but gives me run time errors
    This is what I get -
    SQL> select myjunk('''1:2:3:4''') from dual;
    select myjunk('''1:2:3:4''') from dual
    ERROR at line 1:
    ORA-06502: PL/SQL: numeric or value error
    ORA-06512: at "SGHDTA.MYJUNK", line 8
    ORA-06512: at line 1

    You are getting an error because updatestring is longer than the 100 characters you defined it as. Try using VARCHAR2(4000). Also, if you are trying to generate the sql statement, you need to get rid of first=, second= and third= when you build the string.
    This is what your function returns. I put in line breaks for clarity:
    First=substr('1:2:3:4', 1, instr('1:2:3:4', ':',1,1)-1) ONE,
    Second=substr('1:2:3:4', instr('1:2:3:4', ':',1,2)+1, instr('1:2:3:4',':',1,1)-1) TWO, 
    third=substr('1:2:3:4', instr('1:2:3:4', ':',1,3)+1,instr('1:2:3:4', ':',1,1)-1) THREE
    from temp_tableIf you are trying to actually parse the column, then you need something more like:
    create or replace procedure MYJUNK(TFieldNew IN VARCHAR2,out1 OUT VARCHAR2,
                                       out2 OUT VARCHAR2, out3 OUT VARCHAR2) is
    BEGIN
       out1 := SUBSTR(TFieldNew,1, INSTR(TFieldNew,':',1,1)-1);
       out2 := SUBSTR(TFieldNew, INSTR(TFieldNew,':',1,2)+1, INSTR(TFieldNew,':',1,1)-1);
       out3 := SUBSTR(, INSTR(TFieldNew,':',1,3)+1, INSTR(TFieldNew,':',1,1)-1);
    END;

  • Substr / Instr or Subquery

    Hi All,
    I have a requirement for which I am looking for solution, kindly do the needful.
    CREATE TABLE CO_VENDOR_COMMODITY
    (VENDOR_CODE VARCHAR2(240),
    COMMODITY_CODE NUMBER);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('25',1);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('25',2);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('25',3);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('25',4);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('20',1);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('20',2);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('20',3);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('20',4);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('20',5);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('30',21);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('30',22);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('30',23);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('30',24);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('30',25);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('40',15);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('40',16);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('40',17);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('40',18);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('35',15);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('35',16);
    INSERT INTO CO_VENDOR_COMMODITY VALUES ('35',17);
    CREATE OR REPLACE VIEW OV_VENDOR AS
    select
    VENDOR_CODE,
    rtrim (xmlagg (xmlelement (e,COMMODITY_CODE || ',')).extract ('//text()'), ',') commcode
    from
    CO_VENDOR_COMMODITY
    group by
    VENDOR_CODE;     
    Output should display as below
    VENDOR_CODE     COL1 COL2 COL3 COL4 COL5
    20 1 2 3 4 5
    25 1 2 3 4
    30 21 22 23 24 25
    35 15 16 17
    40 15 16 17 18
    as of now I have built the below query further I am not able to proceed.
    SELECT VENDOR_CODE,
    COMMCODE,
    SUBSTR(COMMCODE,1,(INSTR(COMMCODE,',',1,1) -1)) COL1,
    SUBSTR(COMMCODE,(INSTR(COMMCODE,',',1,1)+1),(LENGTH(SUBSTR(COMMCODE,(INSTR(COMMCODE,',',1,1)+1)))-LENGTH(SUBSTR(COMMCODE,(INSTR(COMMCODE,',',1,2)))))) COL2,
    SUBSTR(COMMCODE,(INSTR(COMMCODE,',',1,2)+1),(LENGTH(SUBSTR(COMMCODE,(INSTR(COMMCODE,',',1,2)+1)))-LENGTH(SUBSTR(COMMCODE,(INSTR(COMMCODE,',',1,3)))))) COL3,
    SUBSTR(COMMCODE,(INSTR(COMMCODE,',',1,3)+1),(LENGTH(SUBSTR(COMMCODE,(INSTR(COMMCODE,',',1,3)+1)))-LENGTH(SUBSTR(COMMCODE,(INSTR(COMMCODE,',',1,4)))))) COL4_1,
    SUBSTR(COMMCODE,(INSTR(COMMCODE,',',1,4)+1)) COL5,
    'SUBSTR(COMMCODE,('||INSTR(COMMCODE,',',1,3)||'+1),('||LENGTH(SUBSTR(COMMCODE,(INSTR(COMMCODE,',',1,3)+1)))||' - '||LENGTH(SUBSTR(COMMCODE,(INSTR(COMMCODE,',',1,4))))||'))' COL4
    FROM OV_VENDOR
    To acheive the above output whether I need use substr/instr or should I go for subquery? which one is best? It is possible to acheive the above output by using subquery?
    Thanks in advance.
    -Satyam

    Oh wait, I now see you want the commodity codes in separate columns.
    If you have a fixed maximum of possible commodity codes, you can use a subquery:
    SQL> select vendor_code
      2  ,      max(case when rn=1 then commodity_code else null end ) col1
      3  ,      max(case when rn=2 then commodity_code else null end ) col2
      4  ,      max(case when rn=3 then commodity_code else null end ) col3
      5  ,      max(case when rn=4 then commodity_code else null end ) col4
      6  ,      max(case when rn=5 then commodity_code else null end ) col5
      7  from ( select vendor_code
      8         ,      commodity_code
      9         ,      row_number() over (partition by vendor_code order by commodity_code) rn
    10         from   co_vendor_commodity
    11       )
    12  group by vendor_code;
    VENDO       COL1       COL2       COL3       COL4       COL5
    20             1          2          3          4          5
    25             1          2          3          4
    30            21         22         23         24         25
    35            15         16         17
    40            15         16         17         18

  • Substring instr issue in obiee

    Hi,
    I want to use the INSTR function in OBIEE
    POSITION function found in the forum
    INSTR function takes four values:
    INSTR (string1, string2, number, number)
    Eg IP address
    111.222.333.444
    The desired result
    SUBSTR (IP_ADD, 1, INSTR (IP_ADD, '.', 1,3) -1)
    111.222.333
    POSITION function, how should I use?
    Thanks,
    Mino

    1st example '0.0.0.0' will def be a problem here. Unfortunately, I don't think any other OBIEE string function could support this.
    I was assuming for min 2 numbers like 00.00.00.00. Do you have any case like the first one in your table ?
    =========
    As I said, you can't use POSITION function here..Just do help for String Function in RPD)
    Position
    Returns the numerical position of the character_expression1 in a character expression. If the character_expression1 is not found, the function returns 0.
    Syntax:
    POSITION(character_expression1 IN character_expression2)
    where:
    character_expression1
    Any expression that evaluates to a character string. Used to search in the second string.
    character_expression2
    Any expression that evaluates to a character string.
    So, these are the 2 expression, In your case its '.' & IP_ADDR.
    =========
    Hope its helpful

  • Pivot not working; Analytics question

    I got a lot done this past weekend on tough query for me but now have more questions. You must run the top query to understand the understand the second.
    Background:
    This is a period to date report which includes amounts from beginning of the year as well as last year.
    My example Report is for Period 9 which begins on Aug 24, 2009
    Period has 5 weeks (Most periods have 4) ;
    Also if period is requested in Week 3 then only weeks 1 and 2 will be reflected on report
    The report looks like this: (Data provided should produce these numbers except for maybe the YTD column.
                 WEEK1       WEEK2           WEEK3         WEEK4             WEEK5       Period-to-date          Year-to-date
    Net - Landau     11,485.79   11,416.60      11,609.01     11,049.76      12,867.10      58,428.00             454,231.37      
    Net - AW     0.00               0.00          0.00        0.00                   0.00          0.00              0.00
    Net - LJS     0.00                   0.00           0.00         0.00           0.00          0.00            0.00
    Net - TB     7,118.17     7,228.13    7,657.94      7,699.53           7,958.53      37,662.00            306,115.59      
    Total Net     18,603.96    18,644.73      19,266.95     18,749.29         20,825.63      96,091.00            760,346.96      
    Last Year Sales     23,515.95    24,244.37       23,962.74    23,134.79      24,440.87      119,299.00           856,363.36      
    Increase     (4,911.99)  (5,599.64)      (4,695.79)  (4,385.50)           (3,615.24)     (23,208.00)           (96,016.40)     
    Last year
      Next Week     24,244.37    23,962.74       23,134.79    24,440.87      23,055.87      118,839.00           879,419.23      --===== Current Year Dates
    Beginning of period: (BOP) Mon Aug 24
    Week 1: Mon Aug 24 - Aug 30
    Week 2: Mon Aug 11 - Sept 6
    Week 3: Mon Sep 7 - Sept 13
    Week 4: Mon Sept 14 - Sept 20
    Week 5: Mon Sept 21 - Sept 27
    Beginning of fiscal year( BOY) = '28-Dec-08'
    --===== Last Year Dates
    Beginning of period: (BOP_LY) Mon Aug 25
    Week 1: Mon Aug 25 - Aug 31
    Week 2: Mon Aug 1 - Sept 7
    Week 3: Mon Sep 8 - Sept 14
    Week 4: Mon Sept 15 - Sept 21
    Week 5: Mon Sept 22 - Sept 28
    Beginning of fiscal year( BOY) = '31-Dec-07'
    My challenge over weekend was to get entire year of data vs. just the period data.
    There are 7 columns on this report. 5 weeks of period, PeriodToDate (total of the weeks) and YearToDate (PeriodToDate + Sum of all data from beginning of year to the end of the previous period.
    I'm not really concerned with the PTD, the program can handle that. I got the BOY date with the following code:
    All data is summed for the week and grouped by storeid and TRUNC(date, 'IW') which is the Monday of each week.
    of each week. (The resultset contains data for 2 stores instead of 1. This is there only to make sure my query was filtering correctly)
    drop table my_csh_main;
    CREATE TABLE MY_CSH_MAIN
       (     "FK_STR_MAIN_ID" NUMBER,
         "BUSI_DATE" DATE,
         "CONF_NUMB2" NUMBER,
         "CONF_NUMB49" NUMBER,
         "CONF_NUMB44" NUMBER,
         "CONF_NUMB3" NUMBER,
         "CONF_NUMB4" NUMBER,
         "CONF_NUMB38" NUMBER,
         "CONF_NUMB56" NUMBER
    REM INSERTING into MY_CSH_MAIN
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (141,to_date('28-AUG-08','DD-MON-RR'),22103.69,0,0,119,0,4605.21,0);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (141,to_date('27-AUG-09','DD-MON-RR'),18081.37,0,0,0,0,3533.45,0);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (141,to_date('17-SEP-09','DD-MON-RR'),18211.29,0,0,0,0,3806.32,0);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (221,to_date('04-SEP-08','DD-MON-RR'),24244.37,0,0,284.94,0,0,9395.63);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (221,to_date('03-SEP-09','DD-MON-RR'),18644.73,0,0,85.48,0,0,7228.13);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (141,to_date('24-SEP-09','DD-MON-RR'),16809.21,0,0,64.99,0,3014.61,0);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (221,to_date('25-SEP-08','DD-MON-RR'),24440.87,0,0,0,0,0,9469.64);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (221,to_date('28-AUG-08','DD-MON-RR'),23515.95,0,0,0,80.38,0,9379.9);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (221,to_date('24-SEP-09','DD-MON-RR'),20825.63,0,0,73.97,0,0,7958.53);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (221,to_date('17-SEP-09','DD-MON-RR'),18749.29,0,0,0,0,0,7699.53);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (141,to_date('11-SEP-08','DD-MON-RR'),22839.3,0,0,206.39,116.74,4493.28,0);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (141,to_date('04-SEP-08','DD-MON-RR'),22627.74,0,0,279.98,0,4423.83,0);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (221,to_date('27-AUG-09','DD-MON-RR'),18603.96,0,0,81.25,0,0,7118.17);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (221,to_date('11-SEP-08','DD-MON-RR'),23962.74,0,0,153.1,0,0,9335.35);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (221,to_date('18-SEP-08','DD-MON-RR'),23134.79,0,0,44.12,0,0,8978.87);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (141,to_date('25-SEP-08','DD-MON-RR'),24950.45,0,0,129.98,0,5330.22,0);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (221,to_date('10-SEP-09','DD-MON-RR'),19266.95,0,0,0,0,0,7657.94);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (141,to_date('03-SEP-09','DD-MON-RR'),17183.25,0,0,0,0,3487.12,0);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (141,to_date('18-SEP-08','DD-MON-RR'),21372.82,0,0,0,0,4546.15,0);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (141,to_date('10-SEP-09','DD-MON-RR'),17688.41,0,0,113.12,0,3424.17,0);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (141,to_date('31-DEC-08','DD-MON-RR'),611016.24,0,0,1276.62,724.96,122236.02,0);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (221,to_date('31-DEC-08','DD-MON-RR'),667612.63,0,0,1018.81,0,0,269777.87);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (141,to_date('02-JAN-08','DD-MON-RR'),1676737.13,0,0,5652.47,3850.68,345971.1,500.5);
    Insert into MY_CSH_MAIN (FK_STR_MAIN_ID,BUSI_DATE,CONF_NUMB2,CONF_NUMB49,CONF_NUMB44,CONF_NUMB3,CONF_NUMB4,CONF_NUMB38,CONF_NUMB56) values (221,to_date('02-JAN-08','DD-MON-RR'),1786451.11,0,0,3167.61,175.38,0,788438.73);
      CREATE TABLE  LANDAU_REPORT_STORES
       (     "COMPANYNAME" CHAR(40 BYTE),
         "DISTRICTNAME" VARCHAR2(50 BYTE),
         "STOREID" NUMBER(4,0) NOT NULL ENABLE,
         "STORENAME" VARCHAR2(70 BYTE),
         "STORENBR" CHAR(4 BYTE) NOT NULL ENABLE
    REM INSERTING into LANDAU_REPORT_STORES
    Insert into LANDAU_REPORT_STORES (COMPANYNAME,DISTRICTNAME,STOREID,STORENAME,STORENBR) values ('Landau                                  ','DIST 10',64,'N_Main','0004');
    Insert into LANDAU_REPORT_STORES(COMPANYNAME,DISTRICTNAME,STOREID,STORENAME,STORENBR) values ('Landau                                  ','DIST 10',65,'Belvidere','0005');
    Insert into LANDAU_REPORT_STORES(COMPANYNAME,DISTRICTNAME,STOREID,STORENAME,STORENBR) values ('Landau                                  ','DIST 10',104,'Mulford','0032');
    Insert into LANDAU_REPORT_STORES(COMPANYNAME,DISTRICTNAME,STOREID,STORENAME,STORENBR) values ('Landau                                  ','DIST 50',141,'Charleston','0043');
    Insert into LANDAU_REPORT_STORES(COMPANYNAME,DISTRICTNAME,STOREID,STORENAME,STORENBR) values ('Landau                                  ','DIST 10',61,'Kilburn','0002');
    Insert into LANDAU_REPORT_STORES(COMPANYNAME,DISTRICTNAME,STOREID,STORENAME,STORENBR) values ('Landau                                  ','DIST 10',62,'11th_St','0003');
       with WeeklyTotals as
       ( select                           StoreId
                                         , week_date
                                         , net_sales
                                         , ljs_food_sales
                                         , total_drink_sales
                                         , net_food
                                         , CASE
                                              WHEN net_food is null  then 0
                                              WHEN net_food  = 0  then 0
                                              ELSE Ljs_food_sales / net_food
                                           END as LJS_Percent
                                         , CASE
                                              WHEN net_food is null  then 0
                                              WHEN net_food = 0  then 0
                                              else total_drink_sales * (ljs_food_sales / net_food)
                                           END as LJS_Drinks
                                         , aw_sales
                                         , tb_sales
              FROM                            (Select fk_str_main_id as StoreId,
                                                                               trunc(csh.busi_date, 'IW') as week_date,
                                                                         sum(csh.conf_numb2) As net_sales,
                                                                         sum(conf_numb49) as ljs_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 tb_sales
                                                     from my_csh_main csh
                                                     WHERE BUSI_DATE BETWEEN  TO_DATE( '28-Dec-08' ,'DD-MON-YY') AND  TO_DATE( '27-SEP-09' ,'DD-MON-YY')
                                                    and fk_str_main_id in (141, 221)
                                                    GROUP BY CSH.FK_STR_MAIN_ID,    trunc(csh.busi_date, 'IW')
       ,  WeeklyFoodSalesLY as
        SELECT    fk_str_main_id AS storeid
               ,  TRUNC(busi_date, 'iw') week_date
               ,  SUM(csh.conf_numb2) AS net_sales
        FROM   my_csh_main csh
        WHERE busi_date BETWEEN   to_date('31-DEc-07', 'dd-Mon-yy')  and  to_date('28-Sep-08', 'dd-Mon-yy')
        GROUP BY fk_str_main_id, TRUNC(busi_date, 'iw')
      , StoreDetails  AS
             select * from LANDAU_REPORT_STORES where STORENAME NOT like  '%CLOSED%'  order by DistrictNAme, StoreNbr
       Select 
                 foods.storeid
              ,   CASE
                   WHEN   to_date(Foods.week_date, 'dd-Mon-yy')  =  to_date('24-AUG-09', 'dd-Mon-yy')  then 1
                   WHEN   to_date(Foods.week_date, 'dd-Mon-yy')  =  to_date('24-AUG-09', 'dd-Mon-yy') + 7 then 2
                   WHEN   to_date(Foods.week_date, 'dd-Mon-yy')  =  to_date('24-AUG-09', 'dd-Mon-yy') + 14 then 3
                   WHEN   to_date(Foods.week_date, 'dd-Mon-yy')  =  to_date('24-AUG-09','dd-Mon-yy') + 21 then 4
                   WHEN   to_date(Foods.week_date, 'dd-Mon-yy')  =  to_date('24-AUG-09', 'dd-Mon-yy') + 28 then 5
                   ELSE  0
             end as WeekNBr
        ,   foods.week_date  as CurrWeekDate
        ,   foodsLY.week_date as LastYearWEekDate   
        ,   ROUND(NVL(foods.net_sales - (aw_sales + tb_sales + ljs_drinks + ljs_food_sales ), 0), 2) as Landau_Net_Sales
        ,   ROUND(NVL(aw_sales, 0), 2) as aw_sales
        ,   ROUND(NVL(tb_sales, 0), 2) as tb_sales
        ,   ROUND(NVL(ljs_drinks + ljs_food_sales, 0),2)    as ljs_net_sales
        ,   ROUND(NVL(foods.Net_Sales, 0), 2) as net_sales
        --==
        -- Last Year Sales and Last Year Next Year Sales
       --==
    ,    ROUND(NVL(foodsLY.Net_Sales, 0),2)    as    WTDLYNetSales
    ,    ROUND(NVL(Foods.Net_Sales -  FoodsLY.Net_Sales, 0),2)  as    WTDSalesIncrease
    -- ,    ROUND(NVL(FoodsLY_NextWeek.Net_Sales, 0), 2)          as    WTDFoodsSalesLY 
    ,   stores.*
    from WEeklyTotals Foods 
    LEFT OUTER JOIN Weeklyfoodsalesly foodsly ON foodsly.storeid = foods.storeid
                                              AND foodsly.week_date = DECODE(foods.week_date,
                                                                             to_date('24-AUG-09', 'dd-Mon-yy') ,       to_date('25-AUG-08', 'dd-Mon-yy') ,
                                                                             to_date('24-AUG-09', 'dd-Mon-yy')  + 7,   to_date('25-AUG-08', 'dd-Mon-yy') + 7,
                                                                             to_date('24-AUG-09', 'dd-Mon-yy')  + 14,  to_date('25-AUG-08', 'dd-Mon-yy') + 14,
                                                                             to_date('24-AUG-09', 'dd-Mon-yy')  + 21,  to_date('25-AUG-08', 'dd-Mon-yy') + 21,
                                                                             to_date('24-AUG-09', 'dd-Mon-yy')  + 28,  to_date('25-AUG-08', 'dd-Mon-yy') + 28)
    LEFT OUTER  JOIN  StoreDetails             stores  ON     stores.storeid   = Foods.storeid;one exception. In a With statement, I get a recordset containing all of last years data. I could not figure out the Pivot
    to get the Last Year Next Week . That means next week, one year ago. I pulled out a snippet to work with but couldn't get it
    to work:
    The problem came when I tried to pivot the Last year sales. I pulled this snippet out to test with but couldn't make it work.
    with   WeeklyFoodSalesLY  as
    (         SELECT  fk_str_main_id AS storeid
                 ,   TRUNC(busi_date, 'iw') week_date
                 ,   sum(conf_numb2)   conf_numb2
               FROM  my_csh_main csh    
               WHERE busi_date BETWEEN   to_date('31-dec-07', 'dd-Mon-yy')  and  to_date('28-Sep-08', 'dd-Mon-yy') + 7
               and fk_str_main_id = 141  
               GROUP BY  fk_str_main_id , TRUNC(busi_date, 'iw')      
    ,  sales_ly as
    (    SELECT      storeid
                    , week_date
                    , sum(conf_numb2) as net_sales
          from WeeklyFoodSalesLY
          WHERE week_date BETWEEN   to_date('25-Aug-08', 'dd-Mon-yy')  and  to_date('28-Sep-08', 'dd-Mon-yy') 
          GROUP BY  storeid , week_date    
          UNION ALL
          SELECT      storeid
                    , week_date
                    , sum(conf_numb2) as net_sales
          from WeeklyFoodSalesLY
          WHERE week_date BETWEEN   to_date('25-Aug-08', 'dd-Mon-yy') + 7   and  to_date('28-Sep-08', 'dd-Mon-yy') + 7
          GROUP BY  storeid , week_date
          UNION ALL
          SELECT      storeid
                    , week_date
                    , sum(conf_numb2) as net_sales
          from WeeklyFoodSalesLY
          WHERE week_date < to_date('25-Aug-08', 'dd-Mon-yy') 
          GROUP BY  storeid , week_date
    ,  pivoted_sales_ly as
    (    SELECT          storeid
                         week_date
                    ,    MAX(DECODE(week_date, to_date('25-Aug-08', 'dd-Mon-yy'),          net_sales))        as lastyear
                    ,    MAX(DECODE(week_date, to_date('25-Aug-08', 'dd-Mon-yy') + 7,      net_sales))        as lastyearnextweek
                    ,    MAX(DECODE(week_date, to_date('31-dec-07', 'dd-Mon-yy'),          net_sales))        as lastyeartotal
          from sales_ly
          GROUP BY  storeid, week_date
    Select *
    from pivoted_sales_ly;  What am i dont wrong here? Once I get this I can work back into the original query.
    Analytics:
    Boneist gave me some code to try last week:
    Combining refCursors  and Summing
    that created the PTD column.
    I could never get it to work because I was tweaking so much with the Query but is there a way to get the PTD without
    having to add an extra PTD column for each week.
    One other thing:
    I would like to know how to use analytics to roll up report by division into a different cursor.
    Edited by: TheHTMLDJ on Oct 26, 2009 4:50 AM
    Edited by: TheHTMLDJ on Oct 26, 2009 4:59 AM

    TheHTMLDJ wrote:
    The report looks like this: (Data provided should produce these numbers except for maybe the YTD column.With the data you provided, I managed to produce the expected output for storeid 221, barring the YTD column, which had the wrong values. I had to make up the "next week" row for the previous year for storeid 221, btw:
    WITH weekly_values AS (SELECT fk_str_main_id as storeid,
                                  TRUNC(csh.busi_date, 'IW') AS week_date,
                                  NVL(SUM(csh.conf_numb2), 0) AS net_sales,
                                  NVL(SUM(conf_numb49), 0) AS ljs_food_sales,
                                  NVL(SUM(conf_numb44), 0) AS total_drink_sales,
                                  NVL(SUM(csh.conf_numb2) - SUM(conf_numb44)
                                                          - SUM(conf_numb3)
                                                          - SUM(conf_numb4), 0) AS net_food,
                                  NVL(SUM(conf_numb38), 0) AS aw_sales,
                                  NVL(SUM(conf_numb56), 0) AS tb_sales
                           FROM   my_csh_main csh
                           WHERE  (busi_date BETWEEN TRUNC(:v_bop, 'iy') AND TRUNC(:v_bop, 'iw') + 7*(:v_num_weeks - 1) + 6
                                   OR
                                   busi_date BETWEEN TRUNC(:v_bop_ly, 'iy') AND TRUNC(:v_bop_ly, 'iw') + 7*:v_num_weeks  + 6)
                           AND    fk_str_main_id IN (141, 221)
                           GROUP BY csh.fk_str_main_id,
                                    TRUNC(csh.busi_date, 'IW')),
         weekly_totals AS (SELECT storeid,
                                  week_date,
                                  net_sales,
                                  ljs_food_sales,
                                  total_drink_sales,
                                  net_food,
                                  CASE WHEN nvl(net_food, 0) = 0 then 0
                                       ELSE ljs_food_sales / net_food
                                  END AS ljs_percent,
                                  CASE WHEN nvl(net_food, 0) = 0 then 0
                                       ELSE total_drink_sales * (ljs_food_sales / net_food)
                                  END AS ljs_drinks,
                                  aw_sales,
                                  tb_sales,
                                  net_sales - aw_sales
                                            - tb_sales
                                            - ljs_food_sales
                                            - CASE WHEN nvl(net_food, 0) = 0 then 0
                                                   ELSE total_drink_sales * (ljs_food_sales / net_food)
                                              END AS landau_net_sales
                           FROM   weekly_values),
         week_tots_ytd AS (SELECT storeid,
                                  week_date,
                                  net_sales,
                                  ljs_food_sales,
                                  total_drink_sales,
                                  net_food,
                                  ljs_drinks,
                                  aw_sales,
                                  tb_sales,
                                  landau_net_sales,
                                  SUM(decode(week_date, TRUNC(:v_bop_ly, 'iw') + 7*:v_num_weeks, NULL, landau_net_sales)) OVER (PARTITION BY storeid,
                                                                                                                                             trunc(week_date, 'iy')) lns_ytd,
                                  SUM(decode(week_date, TRUNC(:v_bop_ly, 'iw') + 7*:v_num_weeks, NULL, aw_sales)) OVER (PARTITION BY storeid,
                                                                                                                                     trunc(week_date, 'iy')) aws_ytd,
                                  SUM(decode(week_date, TRUNC(:v_bop_ly, 'iw') + 7*:v_num_weeks, NULL, ljs_food_sales)) OVER (PARTITION BY storeid,
                                                                                                                                           trunc(week_date, 'iy')) ljsf_ytd,
                                  SUM(decode(week_date, TRUNC(:v_bop_ly, 'iw') + 7*:v_num_weeks, NULL, tb_sales)) OVER (PARTITION BY storeid,
                                                                                                                                     trunc(week_date, 'iy')) tbs_ytd,
                                  SUM(decode(week_date, TRUNC(:v_bop_ly, 'iw') + 7*:v_num_weeks, NULL, net_sales)) OVER (PARTITION BY storeid,
                                                                                                                                      trunc(week_date, 'iy')) net_ytd,
                                  SUM(net_sales) OVER (PARTITION BY storeid,
                                                                    trunc(week_date, 'iy')) full_net_ytd,
                                  SUM(landau_net_sales) OVER (PARTITION BY storeid,
                                                                           CASE WHEN week_date between TRUNC(:v_bop, 'iw') and TRUNC(:v_bop, 'iw') + 7*(:v_num_weeks -1) THEN 1
                                                                                WHEN week_date between TRUNC(:v_bop_ly, 'iw') and TRUNC(:v_bop_ly, 'iw') + 7*(:v_num_weeks -1) THEN 2
                                                                           END) lns_ptd,
                                  SUM(aw_sales) OVER (PARTITION BY storeid,
                                                                   CASE WHEN week_date between TRUNC(:v_bop, 'iw') and TRUNC(:v_bop, 'iw') + 7*(:v_num_weeks -1) THEN 1
                                                                        WHEN week_date between TRUNC(:v_bop_ly, 'iw') and TRUNC(:v_bop_ly, 'iw') + 7*(:v_num_weeks -1) THEN 2
                                                                   END) aws_ptd,
                                  SUM(ljs_food_sales) OVER (PARTITION BY storeid,
                                                                         CASE WHEN week_date between TRUNC(:v_bop, 'iw') and TRUNC(:v_bop, 'iw') + 7*(:v_num_weeks -1) THEN 1
                                                                              WHEN week_date between TRUNC(:v_bop_ly, 'iw') and TRUNC(:v_bop_ly, 'iw') + 7*(:v_num_weeks -1) THEN 2
                                                                         END) ljsf_ptd,
                                  SUM(tb_sales) OVER (PARTITION BY storeid,
                                                                   CASE WHEN week_date between TRUNC(:v_bop, 'iw') and TRUNC(:v_bop, 'iw') + 7*(:v_num_weeks -1) THEN 1
                                                                        WHEN week_date between TRUNC(:v_bop_ly, 'iw') and TRUNC(:v_bop_ly, 'iw') + 7*(:v_num_weeks -1) THEN 2
                                                                   END) tbs_ptd,
                                  SUM(net_sales) OVER (PARTITION BY storeid,
                                                                    CASE WHEN week_date between TRUNC(:v_bop, 'iw') and TRUNC(:v_bop, 'iw') + 7*(:v_num_weeks -1) THEN 1
                                                                         WHEN week_date between TRUNC(:v_bop_ly, 'iw') and TRUNC(:v_bop_ly, 'iw') + 7*(:v_num_weeks-1) THEN 2
                                                                    END) net_ptd,
                                  LEAD(net_sales) OVER (PARTITION BY storeid,
                                                                     trunc(week_date, 'iy')
                                                        ORDER BY week_date) next_week_net_sales
                           FROM   weekly_totals),
                 foods AS (Select storeid,
                                  CASE WHEN week_date in (:v_bop, :v_bop_ly) then 1
                                       WHEN week_date in (:v_bop + 7, :v_bop_ly + 7) then 2
                                       WHEN week_date in (:v_bop + 14, :v_bop_ly + 14) then 3
                                       WHEN week_date in (:v_bop + 21, :v_bop_ly + 21) then 4
                                       WHEN week_date in (:v_bop + 28, :v_bop_ly + 28) then 5
                                  END AS week_number,
                                  week_date,
                                  ROUND(net_sales - (aw_sales + tb_sales + ljs_drinks + ljs_food_sales), 2) AS landau_net_sales,
                                  ROUND(aw_sales, 2) AS aw_sales,
                                  ROUND(tb_sales, 2) AS tb_sales,
                                  ROUND(ljs_drinks + ljs_food_sales, 2) AS ljs_net_sales,
                                  ROUND(net_sales, 2) AS net_sales,
                                  ROUND(lns_ytd, 2) AS lns_ytd,
                                  ROUND(aws_ytd, 2) AS aws_ytd,
                                  ROUND(ljsf_ytd, 2) AS ljsf_ytd,
                                  ROUND(tbs_ytd, 2) AS tbs_ytd,
                                  ROUND(net_ytd, 2) AS net_ytd,
                                  ROUND(full_net_ytd, 2) AS full_net_ytd,
                                  ROUND(lns_ptd, 2) AS lns_ptd,
                                  ROUND(aws_ptd, 2) AS aws_ptd,
                                  ROUND(ljsf_ptd, 2) AS ljsf_ptd,
                                  ROUND(tbs_ptd, 2) AS tbs_ptd,
                                  ROUND(net_ptd, 2) AS net_ptd,
                                  ROUND(next_week_net_sales, 2) AS next_week_net_sales,
                                  SUM(ROUND(next_week_net_sales, 2)) OVER (PARTITION BY storeid,
                                                                                       TRUNC(week_date, 'iy')) nwns_ptd
                           FROM   week_tots_ytd
                           WHERE  (week_date BETWEEN TRUNC(:v_bop, 'iw') AND TRUNC(:v_bop, 'iw') + 7*(:v_num_weeks - 1)
                                   OR
                                   week_date BETWEEN TRUNC(:v_bop_ly, 'iw') AND TRUNC(:v_bop_ly, 'iw') + 7*(:v_num_weeks - 1))),
         pivoted_foods AS (SELECT storeid,
                                  week_number,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), landau_net_sales)) landau_net_sales,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), aw_sales)) aw_sales,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), tb_sales)) tb_sales,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), ljs_net_sales)) ljs_net_sales,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), net_sales)) net_sales,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), lns_ytd)) lns_ytd,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), aws_ytd)) aws_ytd,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), ljsf_ytd)) ljsf_ytd,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), tbs_ytd)) tbs_ytd,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), net_ytd)) net_ytd,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), full_net_ytd)) full_net_ytd,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), lns_ptd)) lns_ptd,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), aws_ptd)) aws_ptd,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), ljsf_ptd)) ljsf_ptd,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), tbs_ptd)) tbs_ptd,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop, 'iy'), net_ptd)) net_ptd,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop_ly, 'iy'), net_sales)) ly_net_sales,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop_ly, 'iy'), net_ytd)) ly_net_ytd,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop_ly, 'iy'), net_ptd)) ly_net_ptd,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop_ly, 'iy'), next_week_net_sales)) ly_next_week_net_sales,
                                  MAX(DECODE(TRUNC(week_date, 'iy'), TRUNC(:v_bop_ly, 'iy'), nwns_ptd)) ly_nwns_ptd
                           FROM   foods
                           GROUP BY storeid,
                                    week_number),
          storedetails AS (SELECT companyname,
                                  districtname,
                                  storeid,
                                  storename,
                                  storenbr
                           FROM   landau_report_stores
                           WHERE  UPPER(storename) NOT LIKE  '%CLOSED%' 
                           ORDER BY districtname, storenbr),
                 dummy AS (SELECT level col1
                           FROM   dual
                           CONNECT BY level <= 8)
    SELECT pf.storeid,
           DECODE(dummy.col1, 1, 'Net - Landau',
                              2, 'Net - AW',
                              3, 'Net - LJS',
                              4, 'Net - TB',
                              5, 'Total Net',
                              6, 'Last Year Sales',
                              7, 'Increase',
                              8, 'Last Year Next Week') category,
           SUM(CASE WHEN pf.week_number = 1 AND dummy.col1 = 1 THEN pf.landau_net_sales
                    WHEN pf.week_number = 1 AND dummy.col1 = 2 THEN pf.aw_sales
                    WHEN pf.week_number = 1 AND dummy.col1 = 3 THEN pf.ljs_net_sales
                    WHEN pf.week_number = 1 AND dummy.col1 = 4 THEN pf.tb_sales
                    WHEN pf.week_number = 1 AND dummy.col1 = 5 THEN pf.net_sales
                    WHEN pf.week_number = 1 AND dummy.col1 = 6 THEN pf.ly_net_sales
                    WHEN pf.week_number = 1 AND dummy.col1 = 7 THEN pf.net_sales - pf.ly_net_sales
                    WHEN pf.week_number = 1 AND dummy.col1 = 8 THEN pf.ly_next_week_net_sales
           END) week1,
           SUM(CASE WHEN pf.week_number = 2 AND dummy.col1 = 1 THEN pf.landau_net_sales
                    WHEN pf.week_number = 2 AND dummy.col1 = 2 THEN pf.aw_sales
                    WHEN pf.week_number = 2 AND dummy.col1 = 3 THEN pf.ljs_net_sales
                    WHEN pf.week_number = 2 AND dummy.col1 = 4 THEN pf.tb_sales
                    WHEN pf.week_number = 2 AND dummy.col1 = 5 THEN pf.net_sales
                    WHEN pf.week_number = 2 AND dummy.col1 = 6 THEN pf.ly_net_sales
                    WHEN pf.week_number = 2 AND dummy.col1 = 7 THEN pf.net_sales - pf.ly_net_sales
                    WHEN pf.week_number = 2 AND dummy.col1 = 8 THEN pf.ly_next_week_net_sales
           END) week2,
           SUM(CASE WHEN pf.week_number = 3 AND dummy.col1 = 1 THEN pf.landau_net_sales
                    WHEN pf.week_number = 3 AND dummy.col1 = 2 THEN pf.aw_sales
                    WHEN pf.week_number = 3 AND dummy.col1 = 3 THEN pf.ljs_net_sales
                    WHEN pf.week_number = 3 AND dummy.col1 = 4 THEN pf.tb_sales
                    WHEN pf.week_number = 3 AND dummy.col1 = 5 THEN pf.net_sales
                    WHEN pf.week_number = 3 AND dummy.col1 = 6 THEN pf.ly_net_sales
                    WHEN pf.week_number = 3 AND dummy.col1 = 7 THEN pf.net_sales - pf.ly_net_sales
                    WHEN pf.week_number = 3 AND dummy.col1 = 8 THEN pf.ly_next_week_net_sales
           END) week3,
           SUM(CASE WHEN pf.week_number = 4 AND dummy.col1 = 1 THEN pf.landau_net_sales
                    WHEN pf.week_number = 4 AND dummy.col1 = 2 THEN pf.aw_sales
                    WHEN pf.week_number = 4 AND dummy.col1 = 3 THEN pf.ljs_net_sales
                    WHEN pf.week_number = 4 AND dummy.col1 = 4 THEN pf.tb_sales
                    WHEN pf.week_number = 4 AND dummy.col1 = 5 THEN pf.net_sales
                    WHEN pf.week_number = 4 AND dummy.col1 = 6 THEN pf.ly_net_sales
                    WHEN pf.week_number = 4 AND dummy.col1 = 7 THEN pf.net_sales - pf.ly_net_sales
                    WHEN pf.week_number = 4 AND dummy.col1 = 8 THEN pf.ly_next_week_net_sales
           END) week4,
           SUM(CASE WHEN pf.week_number = 5 AND dummy.col1 = 1 THEN pf.landau_net_sales
                    WHEN pf.week_number = 5 AND dummy.col1 = 2 THEN pf.aw_sales
                    WHEN pf.week_number = 5 AND dummy.col1 = 3 THEN pf.ljs_net_sales
                    WHEN pf.week_number = 5 AND dummy.col1 = 4 THEN pf.tb_sales
                    WHEN pf.week_number = 5 AND dummy.col1 = 5 THEN pf.net_sales
                    WHEN pf.week_number = 5 AND dummy.col1 = 6 THEN pf.ly_net_sales
                    WHEN pf.week_number = 5 AND dummy.col1 = 7 THEN pf.net_sales - pf.ly_net_sales
                    WHEN pf.week_number = 5 AND dummy.col1 = 8 THEN pf.ly_next_week_net_sales
           END) week5,
           SUM(CASE WHEN pf.week_number = 5 AND dummy.col1 = 1 THEN pf.lns_ptd
                    WHEN pf.week_number = 5 AND dummy.col1 = 2 THEN pf.aws_ptd
                    WHEN pf.week_number = 5 AND dummy.col1 = 3 THEN pf.ljsf_ptd
                    WHEN pf.week_number = 5 AND dummy.col1 = 4 THEN pf.tbs_ptd
                    WHEN pf.week_number = 5 AND dummy.col1 = 5 THEN pf.net_ptd
                    WHEN pf.week_number = 5 AND dummy.col1 = 6 THEN pf.ly_net_ptd
                    WHEN pf.week_number = 5 AND dummy.col1 = 7 THEN pf.net_ptd - pf.ly_net_ptd
                    WHEN pf.week_number = 5 AND dummy.col1 = 8 THEN pf.ly_nwns_ptd
           END) period_to_date,
           SUM(CASE WHEN pf.week_number = 5 AND dummy.col1 = 1 THEN pf.lns_ytd
                    WHEN pf.week_number = 5 AND dummy.col1 = 2 THEN pf.aws_ytd
                    WHEN pf.week_number = 5 AND dummy.col1 = 3 THEN pf.ljsf_ytd
                    WHEN pf.week_number = 5 AND dummy.col1 = 4 THEN pf.tbs_ytd
                    WHEN pf.week_number = 5 AND dummy.col1 = 5 THEN pf.net_ytd
                    WHEN pf.week_number = 5 AND dummy.col1 = 6 THEN pf.ly_net_ytd
                    WHEN pf.week_number = 5 AND dummy.col1 = 7 THEN pf.net_ytd - pf.ly_net_ytd
                    WHEN pf.week_number = 5 AND dummy.col1 = 8 THEN pf.full_net_ytd
           END) year_to_date,
           stores.companyname,
           stores.districtname,
           stores.storename,
           stores.storenbr 
    FROM   pivoted_foods pf
           LEFT OUTER JOIN storedetails stores ON stores.storeid = pf.storeid,
           dummy
    group by dummy.col1,
             DECODE(dummy.col1, 1, 'Net - Landau',
                              2, 'Net - AW',
                              3, 'Net - LJS',
                              4, 'Net - TB',
                              5, 'Total Net',
                              6, 'Last Year Sales',
                              7, 'Increase',
                              8, 'Last Year Next Week'),
             rollup((pf.storeid,
             stores.companyname,
             stores.districtname,
             stores.storename,
             stores.storenbr))
    order by pf.storeid, dummy.col1;(replace all :v_num_weeks, :v_bop and :v_bop_ly references with your variables)

  • How to fetch data using Substring & Instring

    Hi ,
    I have data like
    a_76488b_2780c
    a_76488b_2780c_
    a_76488b_c2780
    a_76488b_c2780
    a_31487b_5542
    a_76488b_2780
    i want to fetch data like last 4 numeric digit only
    2780
    2780
    2780
    2780
    5542
    2780

    I don't know what you have against rexexp_replace, but this works if you want all the digits after the second underscore:
    WITH t AS
            (SELECT 'a_76488b_2780c' str FROM DUAL
             UNION ALL
             SELECT 'a_76488b_2780c_' FROM DUAL
             UNION ALL
             SELECT 'a_76488b_c2780' FROM DUAL
             UNION ALL
             SELECT 'a_76488b_c2780' FROM DUAL
             UNION ALL
             SELECT 'a_31487b_5542' FROM DUAL
             UNION ALL
             SELECT 'a_76488b_2780' FROM DUAL
    SELECT replace(after_second_und
                  ,replace(translate(after_second_und
                                    ,'0123456789'
                          ,NULL
                  ,NULL
                  ) your_number
    FROM   (SELECT   SUBSTR(str,instr(str,'_',1,2)+1) after_second_und
            FROM t
    YOUR_NUMBER
    2780
    2780
    2780
    2780
    5542
    2780

  • Substr -instr

    What's the best way to remove a word from a string?
    If I have:
    select 'xyz Dell product' product from dual
    union all
    select 'Intell product' product from dual
    and I want to remove the beginning xyz word from the string so that I get:
    select 'Dell product' product from dual
    union all
    select 'Intell product' product from dual

    Hi,
    Is this what you're looking for?
    CREATE TABLE     table_x
    AS
    select 'xyz Dell product' product from dual
    union all
    select 'Intell product' product from dual
    SELECT     product
    ,     CASE
             WHEN  SUBSTR (product, 1, 4) = 'xyz '
             THEN  SUBSTR (product, 5)
             ELSE  product
         END          AS no_xyz
    FROM     table_x
    ;Output:
    PRODUCT          NO_XYZ
    xyz Dell product Dell product
    Intell product   Intell productIf you really want to use INSTR, here's one way to get the same results:
    SELECT     product
    ,     CASE
             WHEN  INSTR (product, 'xyz ') = 1
             THEN  SUBSTR (product, 5)
             ELSE  product
         END          AS no_xyz
    FROM     table_x
    ;This will not remove 'xyz ' from elsewhere in the string, as you specified:
    Rinne wrote:
    ... and I want to remove the beginning xyz word from the string ...Edited by: Frank Kulash on Oct 28, 2010 9:20 AM

  • Replace substr, instr with Reg_exp, is this possible,

    Hi Team,
    I have Small requirement below, i have writen Query For this but was thinking Is there Any better way Of
    doing it, esp With Use To reg_exp,
    i have Some code As shown below
    '23456_TR_ABC_CODE12_JPM-(1)100-(1)150'
    From this code i Only want portion which Is Between 'TR_' And '_CODE12' i.e. ABC,
    given Is my Query And its output
    Select
    substr( substr('23456_TR_ABC_CODE12_JPM-(1)100-(1)150',instr('23456_TR_ABC_CODE12_JPM-(1)100-(1)150','TR_',1,1)+3),
            1,
            instr(
                  substr('23456_TR_ABC_CODE12_JPM-(1)100-(1)150',instr('23456_TR_ABC_CODE12_JPM-(1)100-(1)150','TR_',1,1)+3),
                  '_',1,1
                  -1) "word"
    From dual;
            word
    1     ABC
    kindly let me know If there Is Any better way Of doing it,,,

    Jeneesh, in case there are several TR_ or _CODE12 in the line I'd advice to use non-greedy operators.
    SQL> with t as
      2     (select '23456_TR_ABC_CODE12_JPM_(1)_100_CODE12_ASJ' word from dual)
      3     select regexp_replace(word, '.*TR_<font color="red">(.*)</font>_CODE12.*', '\1') word
      4       from t;
    WORD
    <font color="red">ABC_CODE12_JPM_(1)_100</font>
    SQL>
    It is working with (+) sign.
    SQL> with t as
      2     (select '23456_TR_ABC_CODE12_JPM_(1)_100_CODE12_ASJ' word from dual)
      3     select regexp_replace(word, '.*TR_<font color="blue">(.+?)</font>_CODE12.*', '\1') word
      4       from t;
    WORD
    <font color="blue">ABC</font>
    SQL>
    But I don't understand why it is not working with (*) sign :((
    SQL> with t as
      2     (select '23456_TR_ABC_CODE12_JPM_(1)_100_CODE12_ASJ' word from dual)
      3     select regexp_replace(word, '.*TR_<font color="green">(.*?)</font>_CODE12.*', '\1') word
      4       from t;
    WORD
    <font color="green">ABC_CODE12_JPM_(1)_100</font>Maybe someone can explain?
    Several more examples:
    the first and the last of them are really strange, at least for me:
    SQL> with t as
      2     (select '23456_TR_ABC_CODE12_JPM_(1)_100_CODE12_ASJ' word from dual)
      3     select regexp_replace(word, '^<font color="red">.*</font>TR_<font color="red">(.*?)</font>_CODE12.*$', '\1') word
      4       from t;
    WORD
    <font color="red">ABC_CODE12_JPM_(1)_100</font>
    SQL>
    SQL> with t as
      2     (select '23456_TR_ABC_CODE12_JPM_(1)_100_CODE12_ASJ' word from dual)
      3     select regexp_replace(word, '^<font color="blue">.*?</font>TR_<font color="blue">(.*?)</font>_CODE12.*$', '\1') word
      4       from t;
    WORD
    <font color="blue">ABC</font>
    SQL>
    SQL> with t as
      2     (select '23456_TR_ABC_CODE12_JPM_(1)_100_CODE12_ASJ' word from dual)
      3     select regexp_replace(word, '^<font color="red">.*?</font>TR_<font color="red">(.*)</font>_CODE12.*$', '\1') word
      4       from t;
    WORD
    <font color="red">ABC</font>
    SQL>

  • Using substr / instr to parse a filepath help

    I'm having trouble parsing a string per say using sql. I have a column full of file paths. The file paths are similar to:
    C:\Users\Guest\Pictures\example.jp
    I want to be ablt to grab just the file name of the image. I've been working on several variations of a query using the substr function and the instr function. I've come up with the query below. FILEPATH is the name of the column. So far it gives me the file name, but has the '\' in front of it. Can anyone help me get rid of this. Thanks in advance.
    Select substr(FILEPATH, (instr(FILEPATH,'\',-1,1)))
    from Table_Name
    where ID = '101'

    Hi,
    Here's one way:
    SELECT  SUBSTR ( filepath
                   , INSTR ( filepath
                           , -1
                           , 1
                           ) + 1    -- Note   + 1   at end
                    ) AS file_name_only
    FROM    Table_Name 
    WHERE   id = '101'
    SUBSTR (str, p)        returns the last part of string str, starting at position p.  You were passing the position of the last '\' as p; that's why the '\' was included.  If you add 1 to p, like this:
    SUBSTR (str, p+1)      then you'll be passing the position of the 1st character after the last '\'.

  • How to use Substr & Instr to get data from a file

    hi i have a Scenario
    i am getting a file like this
    1,20,ram,sales
    i am getting a file like this data as a column
    i want to split this data in 4 different column like
    1     20      ram     Sales

    Hi,
    this query will help you.
    select
         SUBSTR(C1,0,INSTR(C1,',')-1),
         SUBSTR(C1,INSTR(C1,',')+1,INSTR(C1,',',1,2)-INSTR(C1,',',1,1)-1),
         SUBSTR(C1,INSTR(C1,',',1,2)+1,INSTR(C1,',',1,3)-INSTR(C1,',',1,2)-1),
         SUBSTR(C1,INSTR(C1,',',1,3)+1,INSTR(C1,',',1,4)-INSTR(C1,',',1,3)-1),
         SUBSTR(C1,INSTR(C1,',',1,3)+1)
    from (
         select '1,20,ram,sales' C1 from dual
    remember that a comma isn't really safe as field separator
    Message was edited by: DecaXD

  • Concat substring and string question

    Hello,
          So I am trying to add together a string \\fafs10\home and the concat of a substring of the first initial of a GivenName and LastName. I keep getting a NaN error.
    Here's what I have:
    =string("\\fafs10\home\") + concat(substring(GivenName,0,1), LastName)
    I want to return this as a default value to my list.
    Any help would be greatly appreciated. Thank you.
    Matthew

    Hi
    are you doing this in a calculated column or within a rule on an InfoPath form or another way?
    you should probably just go with
    concat("\\fafs10\home\",substring(GivenName,0,1),LastName)
    Regards
    Sergio Giusti Sergio Blogs
    Linked
    In Profile
    Whenever you see a reply you think is helpful, click Vote As Helpful.
    Whenever you see a reply you think is the answer to the question, click Mark As Answer.

  • Base Analytics Questions

    I have a Pro account, and have a few questions regarding base analytics:
    1) we can see data for which articles are viewed in a folio and how many times, and also data for overlay views... but is there any way in base analytics to see which overlays are being viewed in each article (match them up)?
    2) under Articles > Videos, it isn't showing any data for us and yet we have a video in our folio.  If you look under All Overlays, it lists Video, and shows data under the Overlay Starts.  Why isn't anything showing under Articles > Videos?
    3) I assume that 360 refers to all image sequences in the folio?  Is there any way to separate image sequences from actual 360's?  If not, where can we make this a suggested change in the future!  I would think a simple checkbox in the Folio Overlay panel in InDesign to designate an actual 360 vs an image sequence used for something else...
    Thanks!
    Greg

    Hi Greg,
    Here are the responses:
    1) Currently the base analytics does not provide you a way to know which overlays from a specific article are being viewed. If you are a SiteCatalyst customer you can see breakdown of overlay starts per article.
    2) This is a bug which needs to be fixed soon in the upcoming release.
    3) Currently you cannot differentiate 360 from image sequences. Thanks for the feedback, we'll evaluate it.

  • Analytics Question

    I am getting ready to release my first app that allows users to share content through the web viewer. Am I able to view any metrics behind web viewer usage with the out of the box dps analytics?
    thanks!
    Hank

    Hi Hank,
    The web viewer is tracked, but the baseline analytics on DPS Portal doesn't differentiate analytics for the web viewer from native viewers. If you have access to Adobe Analytics (SiteCatalyst) then you can differeniate web viewer from native viewers by the Viewer Version.
    Shikha

  • Urgent: Need Help with Substring/Instring

    I am taking a user input P_User_Name.
    P_User_Name is firstname/middlename/lastname
    Some examples:
    'James/Earl/Jones'
    '//Madonna'
    'Alec//Baldwin'
    Can someone tell me the code in PL/SQL to extract first name, middlename, lastname for a given P_User_Name.
    I would really appreciate your help.
    Thanks
    Home722

    Duplicate/triplicate posting... pathetic!!
    Urgent: SQL Gurus: Please Help
    Substring

  • PL-SQL Solve: Using INSTR and SUBSTR

    I am trying to work on this and cannot get a solution. Please help
    You have to use INSTR and SUBSTR to solve
    Question:
    You have the following acceptable value
    Numberic: 0-34
    80-100
    or Non Numberic X S U D- D D+
    Im have to use INSTR and SUBSTR functions to test that the value is a valid (as above) number before TO_NUMBER is called:
    SELECT TO_NUMBER('?? ') //HERE ?? and a space (for 100 etc) is for the values as above
    FROM DUAL
    WHERE ....INSTR(......)<=;
    (Hence if the number is true then number comes back or it says no rows)
    and also id non numberic it should also be tested.
    I am completely unsure about it but tried this
    SELECT TO_NUMBER('34 ')
    FROM DUAL
    WHERE INSTR('0123456789',1,1)<=9 (looking at first number ?)
    AND
    INSTR('0123456789',2,2)<=9
    AND
    INSTR('0123456789',3,3)=0;
    Please help

    We have the following value that we can use:
    Numeric: 0-34 and 80-100 only
    or Non Numberic X S U D- D D+
    Have to use INSTR and SUBSTR functions to test that the value is a valid
    (for now only trying to create a function which can later be put into a procedure.)
    SELECT TO_NUMBER('12 ') //e.g HERE 12 and a space for the values as above
    FROM DUAL
    the where clause looks at all three spaces to make sure values are correct (given number or non-numberic values only)
    (Hence if the number is true then number comes back (meaning true)
    or it says NO rows)
    If value is non numeric, test it to allow non numberic also.
    I am completely unsure about it but tried this
    SELECT TO_NUMBER('34 ')
    FROM DUAL
    WHERE INSTR('0123456789',1,1)<=9 (looking at first number ?)
    AND
    INSTR('0123456789',2,2)<=9
    AND
    INSTR('0123456789',3,3)=0;
    Something like this has to be done.....subst (instr, x,x,) i think mite help.

Maybe you are looking for

  • Weird error message won't do anything else

    has any one ever seen??? net4 : unix domain sockets 1.0/smp for linux net4.0 fat: bogus logical sectors size o umsdos: msdos-read-support failed, mount aborted fat: bogus logical sectors size o fat: bogus logical sectors size o kernel panic: ufs: una

  • Importing FROM external hard drive to new internal hard drive

    How can I move my backed up Itunes library from my external hard drive to my new internal hard drive? I went through the steps listed on the support page, and imported the .xml playlist that I moved to the desktop. Now all my playlists (names only) a

  • Layout not displaying correctly

    Hello! I have an issue with some standard layouts in my portal (SP20). The WPC layout home page is defined to have two columns at widths of 75% and 25% respectively, but once it is asigned the page is displaying in two columns of equal width. In the

  • Max number of colors

    Hi, I am using 3D surface plot in Labview 6.i and I would like to know if it is possible to use more than 256 colors (in the Z-scale). Actually I would like to have very smooth change in the color map and this could be possible if we could use 16bit

  • Gettting itunes music/playlist back same way after new install of XP Pro

    I did a full image copy of my hard to an external drive, then blew away my hard drive and reinstalled XP Pro. I've installed itunes 9.x and now want to bring back into itunes my music from the saved image on the external drive. Can I bring back my mu