Help with Nesting DECODE statements

Hello. I'm trying to write a sql script to generate data report for payments. I need to incorporate specific conditions to SUPPRESS BLANK ADDRESS field lines. I am pulling Address_Line1, Address_Line2, Address_Line3, plus CITY, STATE, ZIP as Address_Line4. If any fields are blank, I need to suppress the blank line and move the other lines up.
Here are my IF, THEN, ELSE conditions. There are 9 possible scenarios that I have identified. Can you please help me write this in SQL with the DECODE function?
IF CITY||STATE||ZIP IS NULL               
THEN AD1='12115 Rainbow Road', AD2='Hartford Heights, MO 60226'               
ELSE               
     IF ADDRESS_LINE1 IS NULL          
          IF ADDRESS_LINE2 IS NULL     
               IF ADDRESS_LINE3 IS NULL
               THEN USE HOME ADDRESS
               ELSE vAD1=ADDRESS_LINE3, vAD2=CITY||STATE||ZIP
               ENDIF
          ELSE     
               IF ADDRESS_LINE3 IS NULL
               THEN vAD1=ADDRESS_LINE2, vAD2 = CITY||STATE||ZIP
               ELSE vAD1=ADDRESS_LINE2, vAD2=ADDRESS_LINE3, vAD3=CITY||STATE||ZIP
               ENDIF
          ENDIF     
     ELSE          
          IF ADDRESS_LINE2 IS NULL     
               IF ADDRESS_LINE3 IS NULL
               THEN vAD1=ADDRESS_LINE1, vAD2=CITY||STATE||ZIP
               ELSE vAD1=ADDRESS_LINE1, vAD2=ADDRESS_LINE3, vAD3=CITY||STATE||ZIP
               ENDIF
          ELSE     
               IF ADDRESS_LINE3 IS NULL
               THEN vAD1=ADDRESS_LINE1, vAD2=ADDRESS_LINE2, vAD3=CITY||STATE||ZIP
               ELSE vAD1=ADDRESS_LINE1, vAD2=ADDRESS_LINE2, vAD3=ADDRESS_LINE3, vAD4=CITY||STATE||ZIP
               ENDIF
          ENDIF     
     ENDIF          
ENDIF               
This what I've got so far...
DECLARE
  vADR1
  vADR2
  vADR3
  vADR4
BEGIN
SELECT
DECODE(
END;
/

Hi,
Here is the code along with some sample data. I substitute the "+" symbol for "/" becuase some addresses where using "c/o" in the address line.
The address fields are null, as shown by the query below (ref query results below):
SQL> select address_line1
  2  from po.po_vendor_sites_all
  3  where address_line1='3457 SOLUTIONS CENTER'
  4  and address_line2 is null;
ADDRESS_LINE1
3457 SOLUTIONS CENTERHere is the script:
WITH got_delimited_list AS
SELECT address_line1 || ' +' ||
address_line2 || ' +' ||
address_line3 || ' +' ||
city ||', ' || state ||'  ' || zip AS delimited_list
FROM po.po_vendor_sites_all
SELECT REGEXP_SUBSTR (delimited_list, '[^+]+', 1, 1) AS vad1
, REGEXP_SUBSTR (delimited_list, '[^+]+', 1, 2) AS vad2
, REGEXP_SUBSTR (delimited_list, '[^+]+', 1, 3) AS vad3
, REGEXP_SUBSTR (delimited_list, '[^+]+', 1, 4) AS vad4
FROM got_delimited_list;Sample of the data records returned:
VAD1
VAD2
VAD3
VAD4
3457 SOLUTIONS CENTER
CHICAGO, IL  60677-3004
VAD1
VAD2
VAD3
VAD4
5172 EAGLE WAY
CHICAGO, IL  60678-1517
VAD1
VAD2
VAD3
VAD4
RETAILERS' OCCUPATION TAX
SPRINGFIELD, IL  62796-0001
VAD1
VAD2
VAD3
VAD4
DEPT. NUMBER 478150
P.O. BOX 790100
ST LOUIS, MO  63179-9933
VAD1
VAD2
VAD3
VAD4
P.O. BOX 62251
BALTIMORE, MD  21264-2251
VAD1
VAD2
VAD3
VAD4
P.O. BOX 660481
DALLAS, TX  75266-0481
VAD1
VAD2
VAD3
VAD4
3525 PIEDMONT RD.
BUILDING FIVE
SUITE 300
ATLANTA, GA  30305

Similar Messages

  • Help with nested if statements

    Can anyone see what is wrong with this?  The parameter value is 3.  I commented out 4-12 to narrow down the issue.  If I run it like this, I get 0.  However if I comment out 1 and 2 and just leave the logic for 3, I get the actual numbers.  Where is it getting stuck?
    Crystal Reports 11
    if {?Period} = 1 then
    if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} = 1 then
    if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate) then
    If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
    then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
    else
    if {?Period} = 2 then
    if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} <= 2  then
    if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate) then
    If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
    then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
    else
    if {?Period} = 3 then
    if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} <= 3  then
    if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate) then
    If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
    then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
    //else
    //if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate) then
    //if {?Period} = 4 then
    //if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} <= 4  then
    //If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
    //then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
    //else
    //if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate) then
    //if {?Period} = 5 then
    //if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} <= 5  then
    //If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
    //then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
    //else
    //if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate) then
    //if {?Period} = 6 then
    //if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} <= 6  then
    //If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
    //then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
    //else
    //if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate) then
    //if {?Period} = 7 then
    //if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} <= 7  then
    //If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
    //then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
    //else
    //if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate) then
    //if {?Period} = 8 then
    //if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} <= 8  then
    //If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
    //then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
    //else
    //if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate) then
    //if {?Period} = 9 then
    //if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} <= 9  then
    //If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
    //then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
    //else
    //if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate) then
    //if {?Period} = 10 then
    //if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} <= 10  then
    //If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
    //then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
    //else
    //if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate) then
    //if {?Period} =11 then
    //if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} <= 11  then
    //If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
    //then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
    //else
    //if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate) then
    //if {?Period} = 12 then
    //if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} <= 12  then
    //If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
    //then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
    else 0

    Kim,
    You never get there. Parenthesis will help you what is happening.
    if {?Period} = 1 then
    (  if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} = 1
        then
        (  if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate)
           then
           (  If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
              then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
              else
              if {?Period} = 2
              then
              (  if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} <= 2
                then
                (  if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate)
                   then
                    (   If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
                        then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
                        else
                        if {?Period} = 3
                        then
                        (  if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} <= 3
                           then
                           (  if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate)
                              then
                              (  If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
                                 then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
                                 else...
    All your statements are looking for true values. You need an else statement for each failed if-then test.
    if {?Period} = 1
    then
    (   // 1
       if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} = 1
       then
      (   // 2
         if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} = year(currentdate)
         then
         (    // 3
             If {FC_COST_AREA_ACT_BUD_UNION.TYPE} = "ACT"
             then {FC_COST_AREA_ACT_BUD_UNION.OTHER_SUPPLIES_AND_MAINTENANCE}
             else  
             //what happens if it {FC_COST_AREA_ACT_BUD_UNION.TYPE} not ="ACT"
         )   // 3
         else 
         //what happens if {FC_COST_AREA_ACT_BUD_UNION.FISCAL_YEAR} not= year(currentdate)
      )   // 2
      else
      //what happens if {FC_COST_AREA_ACT_BUD_UNION.PERIOD_NUMBER} not=1
    )   //1
    // the next else is for the first failed if-then test
    else 
    if {?Period} = 2 then...   
    Debi
    The numbers next to the parenthesis are to help you follow them
    Edited by: Debi Herbert on Apr 13, 2011 4:40 PM

  • Need help with the session state value items.

    I need help with the session state value items.
    Trigger is created (on After delete, insert action) on table A.
    When insert in table B at least one row, then trigger update value to 'Y'
    in table A.
    When delete all rows from a table B,, then trigger update value to 'N'
    in table A.
    In detail report changes are visible, but the trigger replacement value is not set in session value.
    How can I implement this?

    You'll have to create a process which runs after your database update process that does a query and loads the result into your page item.
    For example
    SELECT YN_COLUMN
    FROM My_TABLE
    INTO My_Page_Item
    WHERE Key_value = My_Page_Item_Holding_Key_ValueThe DML process will only return key values after updating, such as an ID primary key updated by a sequence in a trigger.
    If the value is showing in a report, make sure the report refreshes on reload of the page.
    Edited by: Bob37 on Dec 6, 2011 10:36 AM

  • Nested decode statement

    Hi i was trying to get the below information.
    I wanted to know is it possible to use nested decode function. also is it possible to use a condition in a decoded function.
    how could i frame the below mentioned information into a nested decode statement.
    IF LSGD = '00000000' then TC = "NS"
    IF LSGD <> '00000000' and RP = 0 and RC =0 OR RU=0 then TC ="NU"
    IF LSGD <> '00000000' and RP = 0 and RC >0 then TC="RR"
    If RU > 1 and INT=0, then TC="TT"
    If PERC is >=66% then TC="RR"
    If PERC is < 66% then TC="TT"
    thanks for your help

    Hi Guido,
    This is the case statement i have constructed. would appriciate if you could assist further to make it more accurate.
    select
    case when LSGD = '00000000' then 'NS'
    when LSGD <> '00000000' then
    case when RP=0 then case when RC=0 then 'NU' else case when RC >0 then 'RR' else 'TT' end end
    else
    case when RU=1 then 'NU' when RU>1 and INT=0 then 'TT' else 'RR' end
    end
    when PERC >=0.66 then 'RR'
    when PERC < 0.66 then 'TT'
    end TC
    from my table
    Thanks in advance

  • Help with this update statement..

    Hi everyone,
    I am trying to update a column in a table .I need to update that column
    with a function that takes patient_nbr and type_x column values as a parameter.
    That table has almost "300,000" records. It is taking long time to complete
    almost 60 min to 90 min.
    Is it usual to take that much time to update that many records?
    I dont know why it is taking this much time.Please help with this update statement.
    select get_partner_id(SUBSTR(patient_nbr,1,9),type_x) partner_id from test_load;
    (it is just taking 20 - 30 sec)
    I am sure that it is not the problem with my function.
    I tried the following update and merge statements .Please correct me if i am wrong
    in the syntax and give me some suggestions how can i make the update statement fast.
    update test_load set partner_id = get_partner_id(SUBSTR(patient_nbr,1,9),type_x);
    merge into test_load a
    using (select patient_nbr,type_x from test_load) b
    on (a.patient_nbr = b.patient_nbr)
    when matched
    then
    update
    set a.partner_id = get_partner_id(SUBSTR(b.patient_nbr,1,9),b.type_x);
    there is a index on patient_nbr column
    and the statistics are gathered on this table.

    Hi Justin,
    As requested here are the explain plans for my update statements.Please correct if i am doing anything wrong.
    update test_load set partner_id = get_partner_id(SUBSTR(patient_nbr,1,9),type_x);
    "PLAN_TABLE_OUTPUT"
    "Plan hash value: 3793814442"
    "| Id  | Operation          | Name             | Rows  | Bytes | Cost (%CPU)| Time     |"
    "|   0 | UPDATE STATEMENT   |                  |   274K|  4552K|  1488   (1)| 00:00:18 |"
    "|   1 |  UPDATE            |        TEST_LOAD |       |       |            |          |"
    "|   2 |   TABLE ACCESS FULL|        TEST_LOAD |   274K|  4552K|  1488   (1)| 00:00:18 |"
    merge into test_load a
    using (select patient_nbr,type_x from test_load) b
    on (a.patient_nbr = b.patient_nbr)
    when matched
    then
    update
    set a.partner_id = get_partner_id(SUBSTR(b.patient_nbr,1,9),b.type_x);
    "PLAN_TABLE_OUTPUT"
    "Plan hash value: 1188928691"
    "| Id  | Operation            | Name             | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |"
    "|   0 | MERGE STATEMENT      |                  |   274K|  3213K|       |  6660   (1)| 00:01:20 |"
    "|   1 |  MERGE               |        TEST_LOAD |       |       |       |            |          |"
    "|   2 |   VIEW               |                  |       |       |       |            |          |"
    "|*  3 |    HASH JOIN         |                  |   274K|    43M|  7232K|  6660   (1)| 00:01:20 |"
    "|   4 |     TABLE ACCESS FULL|        TEST_LOAD |   274K|  4017K|       |  1482   (1)| 00:00:18 |"
    "|   5 |     TABLE ACCESS FULL|        TEST_LOAD |   274K|    40M|       |  1496   (2)| 00:00:18 |"
    "Predicate Information (identified by operation id):"
    "   3 - access("A"."patient_nbr"="patient_nbr")"Please give some suggestions..
    what's the best approach for doing the updates for huge tables?
    Thanks

  • Having Trouble with nested Case Statements

    Hi Folks,
    I'm having trouble getting my head round nested case statements. For the life of me I cannot see what I'm missing here (unless my approach is all wrong).
    Any help much appreciated.
    Script:
    set serveroutput on format wrapped
    set feedback off
    set linesize 150
    DECLARE
    /* Set supported version here */
    ora_version VARCHAR2(4);
    unsupp_version EXCEPTION;
    /* Archive Log Info */
    db_log_mode VARCHAR2(12);
    BEGIN
    SELECT SUBSTR(VERSION, 1, 4)
    INTO ora_version
    FROM v$instance;
    SELECT log_mode
    INTO db_log_mode
    FROM v$database;
    CASE
    WHEN ora_version = '10.2' THEN
    DECLARE
    TYPE t_db IS RECORD(
    dflsh VARCHAR2(3),
    dcscn NUMBER);
    v_db t_db;
    BEGIN
    CASE
    WHEN db_log_mode = 'ARCHIVELOG' THEN
    EXECUTE IMMEDIATE 'SELECT INITCAP(flashback_on), current_scn FROM v$database'
    INTO v_db;
    DBMS_OUTPUT.PUT_LINE(' Flashback On : ' || v_db.dflsh);
    DBMS_OUTPUT.PUT_LINE(' Current SCN : ' || v_db.dcscn);
    DBMS_OUTPUT.PUT_LINE(' Log Mode : ' || db_log_mode);
    DBMS_OUTPUT.PUT_LINE(' Version : ' || ora_version);
    END;
    ELSE
    DBMS_OUTPUT.PUT_LINE(' Log Mode : ' || db_log_mode);
    DBMS_OUTPUT.PUT_LINE(' Version : ' || ora_version);
    END CASE;
    END;
    WHEN ora_version = '9.2' THEN
    DECLARE
    TYPE t_db IS RECORD(
    dcscn NUMBER);
    v_db t_db;
    BEGIN
    CASE
    WHEN db_log_mode = 'ARCHIVELOG' THEN
    EXECUTE IMMEDIATE 'SELECT current_scn FROM v$database'
    INTO v_db;
    DBMS_OUTPUT.PUT_LINE(' Current SCN : ' || v_db.dcscn);
    DBMS_OUTPUT.PUT_LINE(' Log Mode : ' || db_log_mode);
    DBMS_OUTPUT.PUT_LINE(' Version : ' || ora_version);
    END;
    ELSE
    DBMS_OUTPUT.PUT_LINE(' Log Mode : ' || db_log_mode);
    DBMS_OUTPUT.PUT_LINE(' Version : ' || ora_version);
    END CASE;
    END;
    ELSE
    RAISE unsupp_version;
    END CASE;
    EXCEPTION
    WHEN unsupp_version THEN
    DBMS_OUTPUT.PUT_LINE('');
    DBMS_OUTPUT.PUT_LINE(' Unsupported Version '||ora_version||' !');
    DBMS_OUTPUT.PUT_LINE('');
    END;
    set linesize 80
    set feedback on
    set serveroutput off
    Gives errors:
    END;
    ERROR at line 31:
    ORA-06550: line 31, column 7:
    PLS-00103: Encountered the symbol ";" when expecting one of the following:
    case
    ORA-06550: line 37, column 1:
    PLS-00103: Encountered the symbol "WHEN"
    ORA-06550: line 50, column 28:
    PLS-00103: Encountered the symbol ";" when expecting one of the following:
    case
    Edited by: milkyjoe on 28-Apr-2010 05:38

    Hi,
    Never write, much less post, unformatted code.
    Indent the code to show the extent of multi-line structures like BEGIN and CASE.
    For example:
    DECLARE
         /* Set supported version here */
         ora_version       VARCHAR2 (4);
         unsupp_version       EXCEPTION;
         /* Archive Log Info */
         db_log_mode      VARCHAR2 (12);
    BEGIN
         SELECT     SUBSTR(VERSION, 1, 4)
         INTO     ora_version
         FROM     v$instance;
         SELECT     log_mode
         INTO     db_log_mode
         FROM     v$database;
         CASE
             WHEN  ora_version = '10.2' THEN
              DECLARE
                  TYPE t_db IS RECORD(
                             dflsh     VARCHAR2(3),
                             dcscn      NUMBER);
                  v_db t_db;
              BEGIN
                  CASE
                      WHEN db_log_mode = 'ARCHIVELOG' THEN
                       EXECUTE IMMEDIATE 'SELECT INITCAP(flashback_on), current_scn FROM v$database'
                                           INTO v_db;
                       DBMS_OUTPUT.PUT_LINE(' Flashback On : ' || v_db.dflsh);
                       DBMS_OUTPUT.PUT_LINE(' Current SCN : ' || v_db.dcscn);
                       DBMS_OUTPUT.PUT_LINE(' Log Mode : ' || db_log_mode);
                       DBMS_OUTPUT.PUT_LINE(' Version : ' || ora_version);
                  END;
    ...The code above is what you posted, with some whitespace added.
    The error is much clearer; the last CASE statement concludes with END, but CASE blocks always have to conclude with END CASE .
    Why are you using a nested BEGIN block in the code above? Are you plannning to add an EXCEPTION handler later?
    When posting formatted text on this site, type these 6 characters:
    \(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Help with Select Decode Round

    Hi, If anyone could help me with this. I need to round all these numbers in the DECODE but without repeating ROUND.
    This way works:
    SELECT title "Title", category "Category",retail "Current Price",
    DECODE(category,
    'COMPUTER',(ROUND(retail * 1.1,2)),
    'FITNESS', (ROUND(retail * 1.15,2)),
    'SELF HELP', (ROUND(retail * 1.25,2)),
    (ROUND(retail * 1.03,2)))"Revised Price"
    FROM books
    ORDER BY category, title;
    But I need something more like this:
    SELECT title "Title", category "Category",retail "Current Price",
    DECODE (ROUND((category,
    'COMPUTER',retail * 1.1,
    'FITNESS', retail * 1.15,
    'SELF HELP', retail * 1.25,
    retail * 1.03)"Revised Price"),2)
    FROM books
    ORDER BY category, title;
    so that ROUND is not repeated more than once..they all need to be rounded to two decimal places.
    any help would be great. Thanks

    your second is close. You need to round the decode statement. like this:
    SELECT title "Title", category "Category",retail "Current Price",
           ROUND(DECODE(category, 'COMPUTER',retail * 1.1,
                                  'FITNESS', retail * 1.15,
                                  'SELF HELP', retail * 1.25,
                                  retail * 1.03),2) "Revised Price"
    FROM books
    ORDER BY category, title;Note that the alias (I assume) revised Proce needs to go outside of both the decode and the round.
    John

  • Help with a CASE statement

    Please help me with a CASE Statement:
    - When ID = 15, 16, 17, 18 then "Bad"
    - when ID = 19, then "Average"
    - when ID = 21, then "Good"
    - else "Null"
    Thank you!!

    Well the 1st thing to do would be to correct my poor spelling... change    Delault : to Default :
    Don't know why you would get an error stating "The result of selection formula must be a boolean". It's working fine on my machine.
    If your ID field is numbers stored text you have a couple different options...
    1) Convert the ID to a number...
    Select  ToNumber({home.noone_ID})
    2) Wrap the ID values in double quotes...
       Case "15", "16", "17", "18" :
          "BAD"
    Even if this were your problem... the error should be something other than the boolean thing...
    Jason

  • PHP help with nested repeat region

    Hopefully someone can help me out with this one.
    I basically have some SQL returning results of a search page,
    where you can search on various keywords by checking boxes, and it
    returns Employers that match those keywords :
    mysql_select_db($database_myDatabase, $myDatabase);
    if (isset($_GET['ckbox'])){
    // get profile keys
    $ckbox = array_keys($_GET['ckbox']);
    // sql string
    $sql = 'SELECT Employers.*, EmployerContacts.* FROM
    EmployerContacts
    INNER JOIN Employers ON EmployerContacts.EmployerID =
    Employers.EmployerID
    INNER JOIN EmployerProfiles ON EmployerProfiles.EmployerID =
    EmployerContacts.EmployerID
    WHERE EmployerProfiles.ProfileID IN(' . implode(',',
    $ckbox).')
    GROUP BY Employers.EmployerID
    ORDER BY Employers.EmployerID DESC';
    $rsContacts = mysql_query($sql) or die(mysql_error());
    $row_rsContacts = mysql_fetch_assoc($rsContacts);
    @$totalRows = mysql_num_rows($rsContacts);
    else
    echo 'You did not check any profiles.';
    ?>
    The results are drawing fields from the Employers table and
    EmployerContacts table, hopefully to look like :
    Employer1
    Employer1.Contact 1
    Employer1.Contact 2
    Employer1.Contact 3
    Employer2
    Employer2.Contact 1
    Employer2.Contact 2
    Employer2.Contact 3
    etc
    However, I can only seem to get it to repeat the Employers,
    and show just the first Contact for each, like this :
    Employer1
    Employer1.Contact 1
    Employer2
    Employer2.Contact 1
    etc
    So I guess I'm looking for help with looping through the
    Contacts for each Employer.
    The code currently looks like this :
    [code attached]
    Hope that makes sense.
    Many thanks.

    Iain71,
    The DW Repeat Region cannot be nested because both loops use
    the same
    variable names (e.g. $RepeatSelectionCounter_1).
    You will have to manually edit the code, and DW may not
    recognize it
    after you do, but you should be able to get it working fairly
    easily. I
    think that you just need to change the variable names in the
    inner loop
    so that they do not conflict with the similar names in the
    outer loop
    (e.g. rename $RepeatSelectionCounter_1 to
    $RepeatSelectionCounter_2).
    Does that make sense?
    HTH,
    Randy
    > I basically have some SQL returning results of a search
    page, where you can
    > search on various keywords by checking boxes, and it
    returns Employers that
    > match those keywords :

  • Help with a return statement please!

    hey, just hoping somone can help me with this return statement
    i have to add a method called "specialReport" this method takes a year as it's parameter. if the parameter is not a year between 1930 and 1969 inclusive it displays an error message, "not a valid year"
    if the parameter is a valid year, then it compares the parameter with the year field. if they are the same, and is the movie has been rented at least five times, the method will display the message "a good year for movies" if the years are different or the movie has NOT been rented at least five times, the method displays the message "try a different year"
    btw, the Year field is : yearReleased
    all help is very much appreciated!

    public void specialReport(int year){
       //add functionality to process here
       return;
    }

  • Looking for some help with building insert statements...

    Hi, I am using some sql to build some insert statements for me to update a set of tables in our qa environments. The scripts that I have created were working great until someone added a column to some of the tables in the qa env which in turn makes my scripts break because I was simply building the statment to do someting like this...
    insert into dest_table (select * from source_table@dblink);
    But now when the coumns in the tables do not match it breaks...
    This is the dynamic create script I use, can anyone help or suggest a better way to be able to build update statements update to qa tables when the columns are mismatched?
    spool insert.sql
    select
    'insert into ' || table_name || ' (select * from ' || table_name || '@prod );' || chr(10) || ' commit;'
    from user_tables
    where table_name in
    (select * from refresh_tablesl)
    any help is greatly appreciated,
    Thanks.

    See my reply to your duplicate post
    looking for help building dynamic insert statements...

  • Help with multiple case statements

    Hello,
    I am new to BO.  I am on XI 3.0.  SQL 2005.  In Designer, I am trying to create a measure in a financial universe that would end up being multiple case statements within one select.  This is what I tried to do, but it doesn't work with the two case statements.  Can I use an ELSE leading into the second CASE WHEN somehow?  How can I accomplish this?  Sorry for my ignorance!
    CASE WHEN dbo.ClientBudgetYear.DateStage1Approved > 01/01/1900 AND dbo.ClientBudgetMonth.Month = 12 THEN dbo.ClientBudgetMonth.Stage1Sales END
    CASE WHEN  dbo.ClientBudgetYear.DateStage1Approved > 01/01/1900 AND dbo.ClientBudgetMonth.Month = 11 THEN dbo.ClientBudgetMonth.Stage1Sales END
    Any Suggestions?
    Thanks,
    Holly

    Holly,
    I don't know enough about your data or requirement to provide a solution, however, the construct that you post will not work because it causes you to build an object with multiple case statements when only one case statement per object is permitted.  From what I see in your code I would be inclined to combine the two statements into one as such:
    CASE WHEN dbo.ClientBudgetYear.DateStage1Approved > 01/01/1900 AND dbo.ClientBudgetMonth.Month in (11,12) THEN dbo.ClientBudgetMonth.Stage1Sales else null END
    Thanks,
    John

  • Help with a select statement from a SQL Server within a DTS !!

    Hello Gurus!
    I help with the script bellow, when I run it within DTS (in SQL Sever 2000), I got the error Invalid number/or not a valid month.
    Please bellow with the WHERE CLASUE '08/01/2001' AND '03/09/2002'
    And in the other hand I change this forma to '01-AUG-01' AND
    '03-MAR-2002', the DTS start and run witha successful messages, but it does not returns row, which is wrong.
    Somebady please help!
    Thanks Gurus!
    GET Total ANIs with Trafic By Area Code
    select
         substr(b.ct_num, 0,3) as Area_Codes,
         COUNT(DISTINCT B.CT_NUM) AS ANIS
    from
         wasabi.v_trans A,
         wasabi.V_Sur_Universal B,
         wasabi.V_Sub C,
         wasabi.V_Trans_Typ D
    where
         D.Trans_typ = A.Trans_Typ AND
         A.Sur_ID = B.Sur_ID AND
         C.Sub_ID = A.Sub_ID AND
         a.trans_stat != 'X' AND     
         a.Trans_DTTM >= '08/01/2001'AND
         a.Trans_DTTM < '03/09/2002 AND
         B.AMA3 = 'PHONE1'
         AND C.SUB_ID not in (100117)
    GROUP BY
         substr(b.ct_num, 0,3)
    ORDER BY
         Area_Codes

    I think that you need a "to_date" function eg
    change '08/01/2001' to to_date('08/01/2001','dd/mm/yyyy')

  • Please Help: Trouble with nested CASE statement and comparing dates

    Please tell me why the query below is always returning the bold null even when the start_date of OLD is greater than or equal to the start_date of NEW.
    What I want to do is get the difference of the start_dates of two statuses ( Start_date of OLD - Start_date of NEW) if
    1. end_date of NEW is not null
    2. start_date of OLD is greater than start_date of NEW
    else return null.
    select id,
    case when max(end_date) keep (dense_rank last order by decode(request_wflow_status,'New',1,0),start_date) is null then
    null
    else
              case when max(decode(status,'OLD',start_date,null)) > max(decode(status,'NEW',start_date,null))
              then max(decode(status,'OLD',start_date,null)) - max(decode(status,'NEW',start_date,null))
    else
    null
    end
    end result
    from cc_request_status where id =1
    group by id;

    Avinash,
    Thank you for your help.. Here is a more description of my problem..
    Here is a sample of data I have for a table with four columns (id,status,start_date,end_date)
    What I need to do is to get difference of the start dates of the maximum available dates, if data is valid. The pseducode is as follows:
    IF end_date of New status is null
    THEN return null
    ELSE
    IF start_date of old >= start_date of new
    THEN return (start_date of old - start_date of new)
    ELSE return null
    I used the following query but always return the bold null
    select id,
    (case when max(end_date) keep (dense_rank last order by decode(status,'new',1,0),start_date) is null then
    null
    else
              (case when max(decode(status,'old',start_date,null)) >=
              max(decode(status,'new',start_date,null))
              then max(decode(status,'old',start_date,null)) - max(decode(status,'new',start_date,null))
    else
    null
    end)
    end) result
    from tbl where id =1
    Based on the below sample, I expected to get the following result; 14-Mar-07 - 16-Feb-07 which is the difference of the maximum start_dates of the two statuses. However the query is not working.. Please help me.. Thank you..
    Id    Status    start_date      end_date
    1     new      03-Feb-07      07-Feb-07
    1     new      16-Feb-07      21-Feb-07
    1     old      '10-Mar-07      12-Mar-07
    1     old      '14-Mar-07      16-Mar-07

  • Help with nested select query

    Here is my code , i dont have any experience with writing the nested sql , i got the below code , which needs to be modified adding a case statement which will show util_rate for 2 conditions
    1. given in where condition ( to_char(time_stamp,'HH24') >= '09' and to_char(time_stamp,'HH24') <= '19')
    2.Apposite to the condition above (to_char(time_stamp,'HH24') < '09'and to_char(time_stamp,'HH24') > '19')
    could anybody please help me in adding the 2nd condition to the below query , please share any documentation you have for learning how to write nested sql,
    Thank you
    select device,time_stamp,
    sum(ActiveHRS)/sum(AvailHRS) as RATE
    from
    select device,time_stamp,
    case when usage >0 then 1
    else 0 end as ActiveHRS,
    '1' as AvailHRS
    from
    select device,time_stamp,
    sum(case
    when state = 9 then 0
    when state = 0 then 0
    else 1 end) as usage
    from A
    where to_char(time_stamp,'HH24') >= '09'
    and to_char(time_stamp,'HH24') <= '19'
    and to_char(time_stamp, 'D') in (2,3,4,5,6)
    group by device, time_stamp
    group by device,time_stamp

    Here is the DDL :
    desc rumi_all
    Name     Null   Type    
    RUMI_DEVICE      VARCHAR2(20)
    STATE         NUMBER(38) 
    TIME_STAMP      DATE     
    LOCATION       VARCHAR2(50) Here is some sample data :
    RUMI_DEVICE   STATE   TIME_STAMP    LOCATION
    10.45.28.86     0     15-JUN-10      WA-102
    10.45.28.51     0     15-JUN-10      WA-102
    10.45.28.63     0     15-JUN-10      WA-102
    10.45.29.47     9     15-JUN-10      WA-120I used the below query to get the util for the hours 9am - 7pm
    select rumi_device,time_stamp,
    sum(ActiveHRS)/sum(AvailHRS) as RATE
    from
    select rumi_device,time_stamp,
    case when usage >0 then 1
    else 0 end as ActiveHRS,
    '1' as AvailHRS
    from
    select rumi_device,time_stamp,
    sum(case
    when state = 9 then 0
    when state = 0 then 0
    else 1 end) as usage
    from rumi_all
    where to_char(time_stamp,'HH24') >= '09'
    and to_char(time_stamp,'HH24') <= '19'
    and to_char(time_stamp, 'D') in (2,3,4,5,6)
    group by rumi_device, time_stamp
    group by rumi_device,time_stamp
    Acquired Out for the above query :
    RUMI_DEVICE      TIME_STAMP     UTIL_RATE
    10.45.29.47     15-JUN-10       0.234
    10.45.28.63     15-JUN-10             0.123
    10.45.29.47     15-JUN-10             0.987
    10.45.29.47     16-JUN-10             0.23Desired output :I want 2 extra columns which shows util rate for the above condition i.e 9am - 7pm(core hours) and also opposite to that condition after 7pm - before 9am(say, non core hours)
    Desired out put :
    RUMI_DEVICE TIME_STAMP  Core_Util_Rate   Non_Core_UtilRate
    10.45.29.4    15-JUN-10       0.234              0.003
    10.45.28.63   15-JUN-10     0.123              0.001
    10.45.29.47   15-JUN-10              0.987              0.023
    10.45.29.47   16-JUN-10              0.23                  0Hope this helps in answering my question , and also could you please share some document to learn writing nested sql's , i always had tough time in understanding this :-(

Maybe you are looking for