Problem SQL querry (case statement...)

Hi to everyone who reads this.

I have a huge problem (at least for me) with a SQL I try to write in Oracle forms6 (yes I know... old app).

OK, here it goes...

We have a table like:
create table temp (
  year     number 
  month    number,
  konto    number,
  DEBET    number, 
  KREDIT   number,
  date_tr  date,
  ind      number
);
The data in the table are:
 YEAR        MONTH    KONTO      DEBET     KREDIT DATE_TR  IND
---------- ---------- ----- ---------- ---------- -------- -
      2011          1 12101   4674,32           0 05.06.11
      2011          6 12101  -4674,32           0 05.06.11 R
      2011          6 12182   4674,32           0          R
Now, to explain a little what I want to do...
case when p_date > NVL(date_tr,'01012004') then
date_tr is null;
else
ind is null
end
What I need is when p_date is entered by a user SQL should check if the date is less or greater than date_tr...
in this case it should show only the record that has a null value in date_tr cullum.

Or if it isn't... it should display records which is null in ind cullum.


The main problem is that I do not know how to write the case statement in SQLs where clause... maybe someone can give me a hint
or a partial code how do I solve this problem.

Any ideas would be greatly apreciated.

Thanks to you all!
SQL> with temp as
  2  (select 2011 YEAR,1 MONTH, 12101 KONTO, 4674,32 DEBET, 0 KREDIT, to_date('05.06.11','DD.MM.YY') DATE_TR, NULL IND from dual union all
  3  select 2011, 6, 12101, -4674,32, 0, to_date('05.06.11','DD.MM.YY'), 'R' from dual union all
  4  select 2011, 6, 12182,  4674,32, 0, NULL, 'R' from dual
  5  )
  6  SELECT *
  7  FROM   temp
  8  WHERE  CASE
  9           WHEN To_date('&dt, 'DD.MM.YYYY') > (SELECT MAX(date_tr)
 10                                               FROM   temp) THEN To_char(date_tr)
 11           ELSE ind
 12         END IS NULL;
Enter value for dt: 30.06.2011

      YEAR      MONTH      KONTO       4674      DEBET     KREDIT DATE_TR   I
---------- ---------- ---------- ---------- ---------- ---------- --------- -
      2011          6      12182       4674         32          0           R

SQL> /
Enter value for dt: 30.05.2011

      YEAR      MONTH      KONTO       4674      DEBET     KREDIT DATE_TR   I
---------- ---------- ---------- ---------- ---------- ---------- --------- -
      2011          1      12101       4674         32          0 05-JUN-11

SQL>

Tags: Database

Similar Questions

  • Problem of the CASE statement

    Hi all

    I need to replace the value selected,

    Here's the query... make a mistake... make me ORA-00923: KEYWORD not found or provided.

    Could you please correct me.

    SELECT sper.assettxt =
    CASE
    WHEN (select description of flv fnd_lookup_values where flv.lookup_type = 'ZZCUS_SPER_ASSET_CODES'
    and flv.description = sper.assettxt);
    (select the meaning of flv fnd_lookup_values where flv.lookup_type = 'ZZCUS_SPER_ASSET_CODES'
    and flv.description = sper.assettxt)
    NVL (resp_cat. (Text, 'Total');
    NVL (SUM (DECODE (sper.author_create_ta_interval,' < = 24 h, sper.total, 0)), 0) day1.
    NVL (SUM (DECODE (sper.author_create_ta_interval,' < = 48 but > 24', sper.total, 0)), 0) day2.
    NVL (SUM (DECODE (sper.author_create_ta_interval,' < = 72 but > 48', sper.total, 0)), 0) day 3,.
    NVL (SUM (DECODE (sper.author_create_ta_interval,' > 72 h, sper.total, 0)), 0) day4.
    NVL (SUM (DECODE (sper.author_create_ta_interval, "N/a", sper.total, 0)), 0) 'open ',.
    NVL (SUM (sper.total), 0) "Grand Total".
    (SELECT "point proven" TEXT data)
    OF THE DOUBLE
    UNION
    SELECT "data updated item.
    OF THE DOUBLE
    UNION
    SELECT "other answers.
    Resp_cat FROM DUAL)
    LEFT OUTER JOIN
    (SELECT assettxt,
    s.sper_status_text,
    s.author_create_ta_interval,
    Total COUNT (*)
    OF s zzcus.zzcus_sper_data
    WHERE 1 = 1
    AND s.sper_dates =: P_SR_DATES
    AND s.sper_month = substr(:P_SR_DATES,5,2)
    AND s.customer_id =: P_CUSTOMER_ID
    - AND s.task_inquiry_type AS ' descriptive data Challenge %.
    AND s.assettxt <>'! MAD
    S.assettxt, s.sper_status_text, s.author_create_ta_interval)
    SPER partition by (SPER. ASSETTXT)
    ((CASE
    WHEN sper.sper_status_text = "data confirmed."
    THEN
    "Question confirmed the data.
    WHEN sper.sper_status_text = "updating data"
    THEN
    "Item updated data.
    ON THE OTHER
    "Other answers.
    END) = resp_cat.text)
    GROUP BY ROLLUP (resp_cat.text), sper.assettxt
    ORDER BY sper.assettxt, (CASE resp_cat.text
    WHEN 'confirmed data element' THEN 1
    WHEN 'updated data element' THEN 2
    WHEN "Others" THEN 3
    END)




    I need to replace sper.assettxt with the meaning of fnd_lookup_values table.




    -

    Well, it is interpretation since I'm guessing by your intention with the CASE statement.

    Do you mean

    CASE
    WHEN EXISTS
       (
          SELECT
             description
          FROM
             fnd_lookup_values flv
          WHERE
             flv.lookup_type  ='ZZCUS_SPER_ASSET_CODES'
          AND flv.description =sper.assettxt
       )
       THEN
       (
          SELECT
             meaning
          FROM
             fnd_lookup_values flv
          WHERE
             flv.lookup_type  ='ZZCUS_SPER_ASSET_CODES'
          AND flv.description =sper.assettxt
       )
    END, 
    

    If yes then it is much better represented as

       (
          SELECT
             meaning
          FROM
             fnd_lookup_values flv
          WHERE
             flv.lookup_type  ='ZZCUS_SPER_ASSET_CODES'
          AND flv.description =sper.assettxt
       )
    

    Assuming that the code returns 0 - 1 line (no more and you will get an error).

  • Case statement is not spend.

    Hello

    I'm having a strange problem with a case statement.  I'm passing is a Boolean value that is set to false, but the case statement never switches to the loop of "false".  I have attached a word doc who did the screen has a step of this unique through being shot.  The photo of the top has the case highlighted with the probe #8 beside him indicate 'False. '  In the lower screen shot is the very next step that highlights the outside circle while ' loop', but the case statement is always 'True '.  Anyone seen this before?

    Gary Tyrna

    Hey Mello,

    Well, I see what you're saying and you are right, the problem is the case statement works when highlight of execution.  But I have a main case statement in the loop that either allows the state machine to execute a loop or hides them and that case is triggered for no steps to true or false.  So that was the reason why I sent the first e-mail because I could see this case change, but not the smallest.  But if this is the fix well I'll run with it and lets see case statement switch the highlight of the performance.

    Thanks again.

    Gary Tyrna

  • PL/SQL and Case &lt; href &gt; statement

    I have just a bit of code in Pl/SQL, and since I had to put this Case statement with javascript I can't get the correct syntax. Can someone please help with the ' and ' in this statement?

    Thank you

    {code}

    v_query: = "SELECT";

    v_query: =.
    v_query | Case when sd.sd_id = 1 then ' < a href = "javascript:fnc_tabAddUser (v1); ' > '
    || "< img src =" #WORKSPACE_IMAGES #sd.png ">"
    || "< /a >."
    end as open_sd | "category", | ' s1.sub_cat_1,'| '. S2.sub_cat_2,' | "program", | "sd.sd_name," | "sd.sd_date," | "s2.synopsis," | "sd.sd_id,";

    {code}

    Basically, most of the v_query is her go and where clauses but here is simply the select clause. I can post the whole code if it helps. Everything works fine until I entered the Case statement and I believe that my syntax is off. Also, I tried to read and find similar code and some mentioned using Htp.p but I don't know if necessary.

    Published by: Res Ipsa on June 22, 2011 11:49

    Published by: Res Ipsa on June 22, 2011 11:49

    Published by: Res Ipsa on June 22, 2011 11:50

    There was also a problem with your order of. There was no space for text is released which looks like this:

    '', 1) > 0ORDER BY 1 DESC 
    

    This gives a shot. I don't bother to do the tables, so it may not always work.

    DECLARE
       v_query   VARCHAR2 (4000);
       v_search_string VARCHAR2(4000);
    
    BEGIN
        v_search_string :=
        '
            '||:p1_search_string||'
             
               transform((TOKENS, "{", "}", " "))
               transform((TOKENS, "{", "}", " ; "))
               transform((TOKENS, "{", "}", "AND"))
               transform((TOKENS, "{", "}", "ACCUM"))
             
           
          
        ';
    
       v_query := 'SELECT   ';
       IF :p1_search_string IS NOT NULL
       THEN
          v_query := v_query || 'score (1) relevance, ';
       ELSE
          v_query := v_query || '''100%'' relevance, ';
       END IF;
    
       v_query :=
          v_query || 'Case when sd.sd_id = 1 then '''''
                  || 'end as open_sd, category, s1.sub_cat_1, s2.sub_cat_2, '
                  || 'program, sd.sd_name, sd.sd_date, s2.synopsis, sd.sd_id, ';
    
       IF :p1_search_string IS NOT NULL
       THEN
          v_query :=
                v_query
             || 'NVL2 '
             || '(:p1_search_string, '
             || 'ctx_doc.snippet '
             || '(''docsx'', '
             || 'ROWID, '
             || 'NVL (:p1_search_string, ''%''), '
             || ''''', '
             || ''''' '
             || '), '
             || 'NULL '
             || ') snippet ';
       ELSE
          v_query := v_query || 'NULL snippet ';
       END IF;
    
       v_query := v_query
          || 'FROM from pri_cat p
              left join sub_cat_1 s1
              on p.cat_id = s1.cat_id
              left join sub_cat_2 s2
              on s1.sub_1_id = s2.sub_1_id
              left join select_program pro
              on s2.pro_id = pro.pro_id
              left join supplemental_direct sd
              on s2.sd_id = sd.sd_id  ';
    
       IF :p1_search_string IS NOT NULL
       THEN
          v_query := v_query || 'WHERE contains (synopsis, ''' || v_search_string || ''', 1) > 0';
       END IF;
    
       v_query := v_query || ' ORDER BY 1 DESC ';
       return(v_query);
    END;
    

    See you soon,.
    Janet Tyson

  • In PL/SQL CASE statement

    Hi experts, PL/SQL,.

    I'm going a little crazy here, so could someone please point out what I am doing wrong with this case statement:

    Test procedure is:

    CREATE or REPLACE procedure SCOTT.postcode_validate_2 (input_post_code VARCHAR2) as


    alphabet_string VARCHAR2 (52): = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxwyz";
    number_string VARCHAR2 (10): = '012345789';
    last_part_postcode VARCHAR2 (3): = NULL;

    BEGIN


    IF INSTR (input_post_code,' ') = 0
    THEN
    DBMS_OUTPUT. Put_line ("' it takes a space in the zip code please");
    GOTO end;
    ELSIF LENGTH (SUBSTR (input_post_code, instr (input_post_code,' ') + 1)) > 3
    THEN
    DBMS_OUTPUT. Put_line ("the last part of the postcode can be 3 characters");
    GOTO end;
    ON THE OTHER

    last_part_postcode: = SUBSTR (input_post_code, instr (input_post_code,' ') + 1);
    END IF;


    CASE input_post_code

    WHEN (length (substr (input_post_code, 1, instr(input_post_code,' ')-1)) = 2)
    AND instr (alphabet_string, substr(input_post_code,1,1)). = 0
    AND instr (number_string, substr(input_post_code,2,1)). = 0)
    THEN
    DBMS_OUTPUT. Put_line ('we have a valid postal code in the format A9')
    -OTHER
    -DBMS_OUTPUT. Put_line ("sorry, but this is an incorrect postal code!") Format A9');
    -GOTO end;
    -END IF;

    WHEN (length (substr (input_post_code, 1, instr(input_post_code,' ')-1)) = 3)
    AND instr (alphabet_string, substr(input_post_code,1,1)). = 0
    AND instr (alphabet_string, substr(input_post_code,2,1)). = 0
    AND instr (number_string, substr(input_post_code,3,1)). = 0)
    THEN
    DBMS_OUTPUT. Put_line ('we have a valid postal code in the format AA9')
    -OTHER
    -DBMS_OUTPUT. Put_line ("sorry, but this is an incorrect postal code!") AA9 format');
    -GOTO end;
    -END IF;

    WHEN (length (substr (input_post_code, 1, instr(input_post_code,' ')-1)) = 3)
    AND instr (alphabet_string, substr(input_post_code,1,1)). = 0
    AND instr (number_string, substr(input_post_code,2,1)). = 0
    AND instr (number_string, substr(input_post_code,3,1)). = 0)
    THEN
    DBMS_OUTPUT. Put_line ('we have a valid postal code in the A99 format')
    -OTHER
    -DBMS_OUTPUT. Put_line ("sorry, but this is an incorrect postal code!") A99 format');
    -GOTO end;
    -END IF;

    WHEN (length (substr (input_post_code, 1, instr(input_post_code,' ')-1)) = 3)
    AND instr (alphabet_string, substr(input_post_code,1,1)). = 0
    AND instr (number_string, substr(input_post_code,2,1)). = 0
    AND instr (alphabet_string, substr(input_post_code,3,1)). = 0)
    THEN
    DBMS_OUTPUT. Put_line ('we have a valid postal code in the format A9A')
    -OTHER
    -DBMS_OUTPUT. Put_line ("sorry, but this is an incorrect postal code!") A9A format');
    -GOTO end;
    -END IF;

    WHEN (length (substr (input_post_code, 1, instr(input_post_code,' ')-1)) = 4)
    AND instr (alphabet_string, substr(input_post_code,1,1)). = 0
    AND instr (alphabet_string, substr(input_post_code,2,1)). = 0
    AND instr (number_string, substr(input_post_code,3,1)). = 0
    AND instr (number_string, substr(input_post_code,4,1)). = 0)
    THEN
    DBMS_OUTPUT. Put_line ('we have a valid postal code in the format AA99')
    -OTHER
    -DBMS_OUTPUT. Put_line ("sorry, but this is an incorrect postal code!") AA99 format');
    -GOTO end;
    -END IF;

    WHEN (length (substr (input_post_code, 1, instr(input_post_code,' ')-1)) = 4)
    AND instr (alphabet_string, substr(input_post_code,1,1)). = 0
    AND instr (alphabet_string, substr(input_post_code,2,1)). = 0
    AND instr (number_string, substr(input_post_code,3,1)). = 0
    AND instr (alphabet_string, substr(input_post_code,4,1)). = 0)
    THEN
    DBMS_OUTPUT. Put_line ('we have a valid postal code in the format AA9A')
    -OTHER
    -DBMS_OUTPUT. Put_line ("sorry, but this is an incorrect postal code!") AA9A format');
    -GOTO end;
    -END IF;
    END;


    -Check the last part of the format, should be AA9
    IF (instr (number_string, substr(input_post_code,1,1))! = 0)
    AND instr (alphabet_string, substr(input_post_code,2,1)). = 0
    AND instr (alphabet_string, substr(input_post_code,3,1)). = 0)
    THEN
    DBMS_OUTPUT. Put_line ('end part of the code postal is the appropriate format, 9AA');
    ON THE OTHER
    DBMS_OUTPUT. Put_line ("' end part of the postcode is in a wrong format!");
    END IF;

    < < output > >
    DBMS_OUTPUT. Put_line ("Please try again");
    END;
    /

    However, I get the following error:

    LINE/COL ERROR
    -------- -----------------------------------------------------------------
    37/6 PLS-00103: encountered the symbol "WHEN" what awaits an of
    What follows:
    := . ( % ;

    On a second note, can't be incorporated into the (currently commented) case ELSE structure?

    Thank you very much in advance.

    Dev

    The initial error was just a problem with your procedure. The other has to do with how you use the CASE statement.
    The code seems a bit long for what you're trying to do. Could you describe what your needs are?

  • I have problem with value NULL when the use CASE statement please help this question

    I have problem with value NULL when the use CASE statement please help this question


    Table: digital_val

    SNO cl C2

    1 San1 11

    2 22 San2

    Actual result: expected to A         B

    A            B                                                                           11        22

    11 NULL

    22 NULL



    query:

    Select case when c1 = "san1" then c2,.

    case If c1 = "san2" then c2 B

    of digital_val

    I'm more curious why, when you select 2 rows, you expect a result of row?

    WITH digital_val

    AS (SELECT 1 AS 'Sno', 'San1"C1, c2 FROM DUAL 11)

    UNION ALL

    2 SELECT AS 'Sno', 'San2"C1, c2 FROM DUAL 22)

    SELECT CASE WHEN c1 is "San1" THEN END AS A c2.

    CASE WHEN c1 = "San2" THEN END AS B c2

    OF digital_val;

    With no other input, if you select 2 rows, you get 2 rows.  One of the other solutions use a max function, but is this really what you want, does not specify?

  • case in PL/SQL where clause statement

    • Hello
    • I have a research procedure where the user can search on the city, street, zip code.
    • I need meet the 6 different combinations as I don't know what value the user will pass.
    • It should be checked if the user passes the value or not, and then look on values passed.
    • But the procedure below is out.it the slightest mistake can be transformed with if-then-else, but case statement seems much cleaner in the code.
    • procedure searchaddress (pc_town IN SEARCHADDRESS. CITY % TYPE,

    pc_street IN SEARCHADDRESS. TYPICAL STREET %,

    pc_postcode IN SEARCHADDRESS. POSTCODE % TYPE,

    pResultSet to ref_cursor) as

    Start

    Open the pResultset for

    Select searchaddress_pk from searchaddress

    where

    case

    When pc_town is null then ((street = pc_street) and (ZipCode = pc_postcode))

    When pc_street is null then ((town = pc_town) and (ZipCode = pc_postcode))

    When pc_postcode is null then ((town = pc_town) and (street = pc_street))

    When pc_town is null and pc_street is null then (postal code = pc_postcode)

    When pc_street is null and pc_postcode is null then (city = pc_town)

    When pc_town is null and pc_postcode is null then (street = pc_street)

    end

    end searchaddress;

    Thank you

    CenterB

    A basic concept that is important to understand - sliders aren't 'sets of results' (sets of data in memory). A cursor is a series of executable steps (see plans run to cursor). It's like a program. An extraction run the program and output data. In general, which is repeated until the cursor (aka program) is no longer found the corresponding lines.

    A ref cursor is a pointer or a handle for a such slider, passed to a caller, allowing the appellant to interface directly with this slider/program.

    The slider more effective to create a search would be one without superfluous code and predicates and filters. Consider the following approach - a beefier PL/SQL procedure, do not try and play little tricks with SQL predicates in an attempt to create one (and probably not optimal) SQL.

    (not tested/compiled code)

    create or replace procedure SearchAddress(
      town     IN SEARCHADDRESS.TOWN%TYPE,
      street   IN SEARCHADDRESS.STREET%TYPE,
      postcode IN SEARCHADDRESS.POSTCODE%TYPE,
      refCur out ref_cursor
    ) is
      curHandle integer;
      res integer;
      searchSQL varchar2(1000);
    begin
      -- create base query
      searchSQL := 'select searchaddress_pk from searchaddress where 1 = 1 ';
    
      -- add dynamic predicates as needed
      if town is not null then
        searchSQL := searchSQL || 'and town = :town';
      end if;
    
      if street is not null then
        searchSQL := searchSQL || ' and street = :street';
      end if;
    
      if postcode is not null then
        searchSQL := searchSQL || ' and postcode = :postcode';
      end if;
    
      -- create a DBMS_SQL cursor
      curHandle := DBMS_SQL.open_cursor;
      DBMS_SQL.parse( curHandle, searchSQL, DBMS_SQL.NATIVE );
    
       -- bind the dynamic bind variables added as predicate values
      if town is not null then
        DBMS_SQL.Bind_Variable( curHandle, 'town', town );
      end if;
    
      if street is not null then
        DBMS_SQL.Bind_Variable( curHandle, 'street', street );
      end if;
    
      if postcode is not null then
        DBMS_SQL.Bind_Variable( curHandle, 'postcode', postcode );
      end if;
    
      -- execute cursor (to create the cursor program) and pass it back as a ref cursor
      res := DBMS_SQL.Execute( curHandle );
      refCur := DBMS_SQL.To_RefCursor( curHandle );
    end;
    
  • Case statement in the SQL Query prompt dashboard

    Hi Experts,

    I use the following case statement to generate default values in the prompt dashboard based on a condition (the other guest-generated values). Below are two of the query I've tried and the syntax error. I tried all the possibilities (like giving apostrophes, double quotes, don't quote not etc.), but could not understand the question. Don't know what exactly I'm missing here.

    Query 1:

    SELECT

    CASE

    WHEN @{PRType} = monthly THEN 'Participant pay '. "" Amount of PayRate "="15000"

    WHEN @{PRType} = daily THEN 'Participant pay '. "" Amount of PayRate "="650"

    WHEN @{PRType} = hourly THEN 'Participant pay '. "" Amount of PayRate "="1"

    ELSE ' 0'

    END

    OF 'PA-Participant pay. '

    Query 2:

    SELECT

    CASE

    WHEN @{PRType} = monthly THEN "15000"

    WHEN @{PRType} = daily THEN "650"

    @{PRType} = schedule THEN '1'

    ELSE ' 0'

    END

    OF 'PA-Participant pay. '

    This is the query that is generated in the log with error message file.

    1 log message:


    SELECT CASE WHEN Hourly = Monthly THEN "Participant Payroll"."Payrate Amount" = '15000' WHEN Hourly = Daily THEN "Participant Payroll"."Payrate Amount" = '650' WHEN Hourly = Hourly THEN "Participant Payroll"."Payrate Amount" = '1' ELSE '0' END FROM "PA-Participant Payroll"

    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error occurred. [nQSError: 43113] The message returned by OBIS. [nQSError: 27002] Near <>=: Syntax error [nQSError: 26012]. (HY000)



    Comment 2:

    ;CASE WHEN To_Char (Hourly) = 'Hourly' THEN 1 ELSE 0 END 

    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error occurred. [nQSError: 43113] The message returned by OBIS. [nQSError: 27002] Near <>(): syntax error [nQSError: 26012]. (HY000)

    Sorry, I wasn't clear... you must always put this CASE statement in a valid SQL SELECT statement.

    SELECT

    CASE

    When ' @{PRType}' = 'Monthly' THEN '15000'

    When ' @{PRType}' = 'Daily' THEN '650'

    ELSE '0' END

    OF 'PA-Participant pay. '

  • Problem with Case statement

    I try to use a case statement in a report of the Apex, but I get an error message.

    It comes to my sql

    CASE WHEN EXISTS (select stg.extkey, distsubtrpgrpid

    of udm_da da

    udm_di di

    udm_distsubtrpgrp stg

    where di.disttransgrpid = stg.disttransgrpid

    and da.daid = di.daid

    and da.suid = su.suid)

    THEN

    Select stg.extkey

    of udm_da da.

    udm_di di,

    udm_distsubtrpgrp stg

    where di.disttransgrpid = stg.disttransgrpid

    and da.daid = di.daid

    and da.suid = ' | Qul SUID. '

    Stg.extkey group, distsubtrpgrpid

    Union

    Select stg.extkey

    of stg udm_distsubtrpgrp

    where distsubtrpgrpid! = - 1

    and distsubtrpgrpid = ' | NVL (su.distsubtrpgrpid-1). '

    order by 1

    WHEN su.distsubtrpgrpid IS NOT NULL

    THEN

    Select stg.extkey, distsubtrpgrpid

    of stg udm_distsubtrpgrp

    where distsubtrpgrpid! = - 1

    and distsubtrpgrpid = ' | NVL (su.distsubtrpgrpid-1). '

    order by 1

    ON THE OTHER

    NULL VALUE

    END DISTSUBTRPGRP_1

    Any help appreciated

    Gus

    GusC wrote:

    Yes you are right.

    My sql must be

    CASE WHEN su.distsubtrpgrpid IS NOT NULL

    THEN

    Select stg.extkey, distsubtrpgrpid

    of stg udm_distsubtrpgrp

    where distsubtrpgrpid! = - 1

    and distsubtrpgrpid = su.distsubtrpgrpid

    ON THE OTHER

    NULL VALUE

    END DISTSUBTRPGRP_1

    But the report won't save

    Gus

    No, your SQL is not that.

    The select statement that you put online here, aside from being not not in brackets, is the selection of two values, but you can provide two values for the result column.

    Without knowing who the SQL statement, we can highlight only the obvious questions.

  • Problem with CASE statements

    Hi all

    I have a calculation in a report of Discoverer Desktop with 29 instructions BOX and still 2 case statements more to add. The problem is that my report freezes and fails to run due to the excessive number of case statements. I need to put the 31 statemnts cases in the report, how do I do this? Is there another way to do it without causing the report to freeze?

    Below is part of my instructions box just to show you what I'm doing:

    CASE WHEN Ex = 'MM' AND THEN of OTHER NULL result (', ')
    CASES WHERE Ex = "PP" AND result IN (40,35,30,25) THEN NULL OTHERWISE
    CASE WHEN Ex = he's ' AND result IN (' a ',' B', 'C', d ') THEN NULL OTHERWISE
    -CASE WHEN Ex = 'IO' AND THEN of OTHER NULL result ('Distinction', 'Merit')
    -CASE WHEN Ex IN ('ZZ', 'WW', 'SS', 'KK') AND ('PASS', 'P') THEN to ANOTHER NULL result...

    The last statement should be BOX WHEN... THEN NULL OTHERWISE 1 END END END END...

    Help, please. Thank you.

    Hello
    You have reached the limit of characters in a calculation.

    Try to set into a single statement as follows:

    CASE WHEN Ex = 'MM' AND THEN of OTHER NULL result (', ')
    WHEN Ex = "PP" AND as a result (40,35,30,25) THEN NULL OTHERWISE
    WHEN Ex = he's ' AND result IN (' a ',' B', 'C', d ') THEN NULL OTHERWISE
    WHEN Ex = 'IO' AND THEN of OTHER NULL result ('Distinction', 'Merit')
    WHEN Ex IN ('ZZ', 'WW', 'SS', 'KK') AND ('PASS', 'P') THEN to ANOTHER NULL result...

    The last statement should be BOX WHEN... THEN NULL OTHERWISE 1 END

    Another option is to combine all the statements of NULL in a series of gold options like this:

    BOX WHEN
    (Ex = 'MM' AND the result in (', am')) OR
    (Ex = "PP" AND result IN (40,35,30,25)) OR
    (Ex = he's ' AND translated BY (' a ',' B', 'C', ')) OR
    (Ex = 'IO' AND as a result ('Distinction', "according to merit")) OR
    (Ex IN ("ZZ", "WW", "SS", "KK") AND result IN ('PASS', 'P')) THEN ANOTHER NULL...

    Best wishes
    Michael

  • Help with making SQL query references to column aliases in the Case statement

    I need help with a sql query that I'm trying. I can go about it the wrong way, but I would be grateful if I could get any suggestions on possible solutions. This is my query:


    SELECT DISTINCT spriden_pidm, spriden_id id, spriden_last_name | ',' | spriden_first_name name,

    CASE
    WHEN rcresar_comm_code_01 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_01
    WHEN rcresar_comm_code_02 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_02
    WHEN rcresar_comm_code_03 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_03
    WHEN rcresar_comm_code_04 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_04
    WHEN rcresar_comm_code_05 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_05
    WHEN rcresar_comm_code_06 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_06
    WHEN rcresar_comm_code_07 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_07
    WHEN rcresar_comm_code_08 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_08
    WHEN rcresar_comm_code_09 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_09
    WHEN rcresar_comm_code_10 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_10
    END acg_elig_comm_code

    CASE
    WHEN acg_elig_comm_code = ' 268' THEN 'rigorous HS course. "
    WHEN acg_elig_comm_code = '269' THEN ' 2 or several AP or IB"
    WHEN acg_elig_comm_code = '270' THEN 'NOC as possible ".
    END comm_code_description

    OF spriden, rcresar, rcrapp1

    WHERE (rcresar_comm_code_01 IN ('268 ', '269', ' 270')

    OR rcresar_comm_code_02 ('268 ', '269', ' 270')

    OR rcresar_comm_code_03 ('268 ', '269', ' 270')

    OR rcresar_comm_code_04 ('268 ', '269', ' 270')

    OR rcresar_comm_code_05 ('268 ', '269', ' 270')

    OR rcresar_comm_code_06 ('268 ', '269', ' 270')

    OR rcresar_comm_code_07 ('268 ', '269', ' 270')

    OR rcresar_comm_code_08 ('268 ', '269', ' 270')

    OR rcresar_comm_code_09 ('268 ', '269', ' 270')

    OR rcresar_comm_code_10 ('268 ', '269', ' 270'))


    Rcresar_aidy_code = & aidy_code

    AND rcrapp1_aidy_code = rcresar_aidy_code

    AND rcrapp1_curr_rec_ind = 'Y '.

    AND rcrapp1_seq_no = rcresar_seq_no


    AND spriden_pidm = rcresar_pidm

    AND rcrapp1_pidm = rcresar_pidm


    AND spriden_change_ind IS NULL

    ORDER BY name


    The second case statement is where I don't know exactly what it takes to get what I want.

    Output should be like:
    spriden_pidm name ID acg_elig_comm_code comm_code_description
    «0000000000', ' 1111111111 ","John Doe","268", «rigorous HS race"»

    If I take the second case statement it works great except that I do not have my comm_code description column. My question is how can I use my first statement value box to determine this column? I think that I need a case statement as I have, but I don't know how to reference the value of acg_elig_comm_code. Any help would be greatly appreciated. Thank you.

    Published by: blackhole82 on January 20, 2009 09:20

    Hello

    You cannot use the alias column in the query, even where it is set (except in the ORDER BY clause).
    You can set the alias in a subquery and then use it in a great query, like this:

    WITH  sub_q  AS
    (
        SELECT DISTINCT spriden_pidm,spriden_id id, spriden_last_name||', '||spriden_first_name name,
            CASE
                WHEN rcresar_comm_code_01 IN ('268','269','270') THEN rcresar_comm_code_01
                WHEN rcresar_comm_code_02 IN ('268','269','270') THEN rcresar_comm_code_02
                WHEN rcresar_comm_code_03 IN ('268','269','270') THEN rcresar_comm_code_03
                WHEN rcresar_comm_code_04 IN ('268','269','270') THEN rcresar_comm_code_04
                WHEN rcresar_comm_code_05 IN ('268','269','270') THEN rcresar_comm_code_05
                WHEN rcresar_comm_code_06 IN ('268','269','270') THEN rcresar_comm_code_06
                WHEN rcresar_comm_code_07 IN ('268','269','270') THEN rcresar_comm_code_07
                WHEN rcresar_comm_code_08 IN ('268','269','270') THEN rcresar_comm_code_08
                WHEN rcresar_comm_code_09 IN ('268','269','270') THEN rcresar_comm_code_09
                WHEN rcresar_comm_code_10 IN ('268','269','270') THEN rcresar_comm_code_10
            END acg_elig_comm_code   -- Originally posted with , here (error)
        FROM spriden, rcresar, rcrapp1
        WHERE (rcresar_comm_code_01 IN ('268','269','270')
                OR rcresar_comm_code_02 IN ('268','269','270')
                OR rcresar_comm_code_03 IN ('268','269','270')
                OR rcresar_comm_code_04 IN ('268','269','270')
                OR rcresar_comm_code_05 IN ('268','269','270')
                OR rcresar_comm_code_06 IN ('268','269','270')
                OR rcresar_comm_code_07 IN ('268','269','270')
                OR rcresar_comm_code_08 IN ('268','269','270')
                OR rcresar_comm_code_09 IN ('268','269','270')
                OR rcresar_comm_code_10 IN ('268','269','270'))
        AND rcresar_aidy_code = &aidy_code
        AND rcrapp1_aidy_code = rcresar_aidy_code
        AND rcrapp1_curr_rec_ind = 'Y'
        AND rcrapp1_seq_no = rcresar_seq_no
        AND spriden_pidm = rcresar_pidm
        AND rcrapp1_pidm = rcresar_pidm
        AND spriden_change_ind IS NULL
    )
    SELECT    sub_q.*,
              CASE
                  WHEN acg_elig_comm_code = '268' THEN 'Rigorous HS course'
                  WHEN acg_elig_comm_code = '269' THEN '2 or more AP or IB'
                  WHEN acg_elig_comm_code = '270' THEN 'ACG possible'
              END comm_code_description
    FROM      sub_q
    ORDER BY  name
    

    Furthermore, you might think to rearrange your table, so that you do not have 10 columns (rcresar_comm_code_01, rcresar_comm_code_02,...) that essentially do the same thing. The usual way to handle this kind of one-to-many relationship is to have all rcresar_comm_codes in a separate table, one per line, with a pointer to the table where you have them now.

    Published by: Frank Kulash, January 20, 2009 11:35
    Syntax error has been corrected

  • Case statement

    Hi all

    I wrote the code that gives the same number below:

    Select

    Count (case when dm_recd_load_dt < add_months(sysdate,-12) then 1 else 0 end) as count_recd_ly,

    Count (case when dm_recd_load_dt < end (sysdate) then 1 else 0) as count_recd

    of cr_customer

    where nvl (primary_cust_flag, 'Y') = 'Y '.

    and the brand = 'The Commission'

    Hello

    Whenever you have a problem, please post a small example data (CREATE TABLE and only relevant columns, INSERT statements) of all the tables involved, so that people who want to help you can recreate the problem and test their ideas.

    Also post the exact results you want from this data, as well as an explanation of how you get these results from these data, with specific examples.

    Post earlier in this forum.

    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: Re: 2. How can I ask a question on the forums?

    2849981 wrote:

    Hi all

    I wrote the code that gives the same number below:

    Select

    Count (case when dm_recd_load_dt< add_months(sysdate,-12)="" then="" 1="" else="" 0="" end)="" as="">

    Count (case when dm_recd_load_dt< (sysdate)="" then="" 1="" else="" 0="" end)="" as="">

    of cr_customer

    where nvl (primary_cust_flag, 'Y') = 'Y '.

    and the brand = 'The Commission'

    Remember that ACCOUNT (exp) done: it returns the number of lines where the expression exp is not NULL.  Neither 1 nor 0 have the value NULL, then

    COUNT (CASE WHEN... THEN 0 OTHERWISE 1 END)

    is equivalent to

    COUNT (*)

    Perhaps you meant

    Count (case when dm_recd_load_dt)< add_months="" (sysdate,-12)="" then="" 1="" end) ="" as="">

    Count (case when dm_recd_load_dt)<            (sysdate)    ="" then="" 1="" end) ="" as="">

    She a CASE expression is not a clause ELSE, ELSE NULL by default.

    Moreover, these are CASES expressions.  A CASE statement is something else, found in PL/SQL, but not in SQL.

  • Syntax of the case statement / WHEN

    The table I use a depreciation per fiscal year and the fiscal period.  I try to have the amount of depreciation to go to 2 different columns based on the fiscal year and the fiscal year.  I'm doing it with a nested case statement. I know that is not correct, because I get the message ORA-00905.  I'm relatively new to sql and it is contribtuing to my problem as well.  Here is the code I have and suggestions / corrections would be appreciated.  Thanks for the help...

    SELECT

    lao PDR. DEPTID as DEPTID,

    lao PDR. ASSET_ID as ASSET_NO,

    PA. Descr as DESCRIPTION,

    lao PDR. ACCOUNT_AD as AD_ACCT,

    PDL. DE_ACCT, to take into ACCOUNT

    lao PDR. ADEATH as AMT_DEPR,

    PDL. JOURNAL_ID as JRNL_ID,

    PDL. JOURNAL_DATE as JRNL_DT,

    lao PDR. FISCAL_YEAR as FY,

    lao PDR. ACCOUNTING_PERIOD AP,

    CASE

    WHEN RDP. FISCAL_YEAR = 2014 THEN

    WHEN RDP. PERIOD ACCOUNTANT = 11 THEN pdr. DEPR

    END AS CURR_MONTH,

    CASE

    WHEN RDP. FISCAL_YEAR <>2014

    WHEN RDP. ACCOUNTING PERIOD <>11 THEN pdr. DEPR

    END AS PRIOR_MONTH

    OF PS_DEPR_RPT pdr

    INNER JOIN PS_DIST_LN pdl

    THE pdl. BOOK = pdr. BOOK

    AND pdl. BUSINESS_UNIT = pdr. BUSINESS_UNIT

    AND pdl. FISCAL_YEAR = pdr. FISCAL_YEAR

    AND pdl. ACCOUNTING_PERIOD = pdr. ACCOUNTING_PERIOD

    AND pdl. ASSET_ID = pdr. ASSET_ID

    AND pdl. CF_SEQNO = pdr. CF_SEQNO

    INNER JOIN PS_ASSET PA

    WE pa. ASSET_ID = pdl. ASSET_ID

    AND pa. BUSINESS_UNIT = pdl. BUSINESS_UNIT

    WHERE

    lao PDR. BUSINESS_UNIT = "A0465.

    AND pdr. BOOK = 'RUN '.

    AND ((pdr. FISCAL_YEAR = 2014 AND pdr. ACCOUNTING_PERIOD = 11) OR (pdr. FISCAL_YEAR = 2014 AND pdr. ACCOUNTING_PERIOD = 10))

    Hello

    2713822 wrote:

    Thank you... I appreciate the information you provide when you answer these questions.  I always try to get the amount (from the same column) for 2 rows in different columns.  I tried the LAST_VALUE and LAG but it took a long time for the queries to run, I'm looking for another way to do the same.  I'm only using SQL to retrieve data.  I don't have the ability to create or insert.

    I looked the information above and the CASE statement to look like this:

    CASE

    WHEN RDP. FISCAL_YEAR = 2014

    AND pdr. ACCOUNTING_PERIOD = 11

    THEN the RDP. ADEATH AS CURR_MONTH

    ON THE OTHER

    lao PDR. ADEATH AS MONTHS PREVIOUS

    END

    But I'm now getting an "ORA-00905: lack of keyword" message.

    What I'm trying to do is to draw 2 lines 1 to 2014 / 11 and another for 2014 / 10.  The amount for the period 2014 / 11 should go in the current column and the amount for the period 2014 / 10 should go in the previous column.

    Before current assets management

    01 AB01 50.01 50.03

    ....

    If you want to give an alias for a column, then you can say "AS nome_alias" after that tell you what that is in this column.

    'AS nome_alias' applies to the entire column.  Cannot use 'alias_name' in the middle of an expression, for example, in the middle of a CASE expression, before the END keyword.

    If you want to have 2 separate output columns, curr_month and prior_month to your output, you must then 2 separate columns in your SELECT clause.  for example:

    SELECT pdr.branch

    pdr.asset

    CASE

    WHEN pdr.fiscal_year = 2014

    AND pdr.accounting_period = 11

    THEN pdr.depr

    END AS curr_month

    CASE

    WHEN...

    THEN...

    END AS prior_month

    PDR

    ;

    If post you some sample data (CREATE TABLE and INSERT statements), the results and explanations, I could show you how to complete the... sections.

    To find out what version of Oracle you have, use

    SELECT *.

    SINCE the release of v$.

    The output can be messy, like this:

    BANNER

    -------------------------------------------------------------------

    CON_ID

    ----------

    12 c Oracle database Release 12.1.0.1.0 - 64 bit Production

    0

    PL/SQL Release 12.1.0.1.0 - Production

    0

    CORE Production 12.1.0.1.0

    0

    AMT for 64-bit Windows: Version 12.1.0.1.0 - Production

    0

    NLSRTL Version 12.1.0.1.0 - Production

    0

    The important thing is the number 5 parts on the first line; 12.1.0.1.0 in the example above.

  • Case statement in query sub

    Hi, I have two questions, here is my initial code:

    Select
    CC.name_id_no
    cc.discover_date
    cc.cla_case_no
    max (rl.year_of_incident) Non_Loss_Past_5
    rl.timestamp
    of cla_case cc, rbn_loss rl
    where cc.name_id_no = rl.customer_no
    and rl.year_of_incident < trunc (cc.discover_date)
    and rl.type_of_loss < 1000
    and rl.timestamp < trunc (cc.discover_date)
    and (cc.question_class = 20
    or cc.question_class = 25)
    and < 1095 (trunc (cc.discover_date)-(rl.year_of_incident))
    - and (trunc (cc.discover_date) <>(rl.year_of_incident))
    Group of cc.cla_case_no, name_id_no, cc.discover_date, rl.timestamp

    Now a cla_case_no can map to several year_of_incident. I want only the cla_case_no that maps to the max year_of_incident, that is to say it should only be a single cla_case_no corresponding to the max year_of_incident.

    To work around this problem, I did the following is not very effective and I hope that it can be improved:

    Select distinct z.cla_case_no from)

    Select
    CC.name_id_no
    cc.discover_date
    cc.cla_case_no
    max (rl.year_of_incident) Non_MW_Loss_Past_5
    rl.timestamp
    of cla_case cc, rbn_loss rl
    where cc.name_id_no = rl.customer_no
    and rl.year_of_incident < trunc (cc.discover_date)
    and rl.type_of_loss < 1000
    and rl.timestamp < trunc (cc.discover_date)
    and (cc.question_class = 20
    or cc.question_class = 25)
    and < 1095 (trunc (cc.discover_date)-(rl.year_of_incident))
    - and (trunc (cc.discover_date) <>(rl.year_of_incident))
    Group of cc.cla_case_no, name_id_no, cc.discover_date, rl.timestamp
    ) z

    Now comes the second question: the above is actually a subquery that will link to a larger table via cla_case_no ccx

    SELECT

    This is to say, (select distinct z.cla_case_no of)

    Select cc.name_id_no, cc.discover_date, cc.cla_case_no, max (rl.year_of_incident) Non_MW_Loss_Past_5, rl.timestamp
    of cla_case cc, rbn_loss rl
    where cc.name_id_no = rl.customer_no
    and rl.year_of_incident < trunc (cc.discover_date)
    and rl.type_of_loss < 1000
    and rl.timestamp < trunc (cc.discover_date)
    and (cc.question_class = 20
    or cc.question_class = 25)
    and < 1095 (trunc (cc.discover_date)-(rl.year_of_incident))
    - and (trunc (cc.discover_date) <>(rl.year_of_incident))
    Group of cc.cla_case_no, name_id_no, cc.discover_date, rl.timestamp
    ) z
    where z.cla_case_no = ccx.cla_case_no
    ) Non_MW_Loss_Past_5

    Etc.

    Now only some cc.cla_case_no the subquery to be match to the ccx_cla_case_no of the main table and the other entries will be void.

    What I am asking, is that if the subquery returns a result that IS NOT NULL to return some ELSE 'Y' "n" instead of her ranges from cla_case_no entries (null) in the Non_MW_Loss_Past_5 column

    Thank you!!!

    Banner:
    Oracle Database 11 g Release 11.2.0.2.0 - 64 bit Production
    PL/SQL Release 11.2.0.2.0 - Production
    "CORE 11.2.0.2.0 Production."
    AMT for Linux: Version 11.2.0.2.0 - Production
    NLSRTL Version 11.2.0.2.0 - Production

    Hello

    Looks like you have another copy of this issue:
    Case statement and query sub
    This probably isn't your fault, but you must mark the other copy as "Answered" right away, and then you only have to look for answers in one place.

    885178 wrote:
    ... Now a cla_case_no can map to several year_of_incident. I want only the cla_case_no that maps to the max year_of_incident, that is to say it should only be a single cla_case_no corresponding to the max year_of_incident.

    If you know there is only one, then you can use last, and you don't need GrOUP BY

    To work around this problem, I did the following is not very effective and I hope that it can be improved:

    Select distinct z.cla_case_no from)

    Select
    CC.name_id_no
    cc.discover_date
    cc.cla_case_no
    max (rl.year_of_incident) Non_MW_Loss_Past_5
    rl.timestamp
    of cla_case cc, rbn_loss rl
    where cc.name_id_no = rl.customer_no
    and rl.year_of_incident<>
    and rl.type_of_loss<>
    and rl.timestamp<>
    and (cc.question_class = 20
    or cc.question_class = 25)
    and (trunc (cc.discover_date)-(rl.year_of_incident))<>
    -(trunc (cc.discover_date) <> (rl.year_of_incident))
    Group of cc.cla_case_no, name_id_no, cc.discover_date, rl.timestamp
    ) z

    Here's one way:

    SELECT       MIN (cla_case_no) KEEP (DENSE_RANK LAST ORDER BY r1.year_of_incident)
                         AS latest_cla_case_no
    FROM       cla_case     cc
    ,             rbn_loss      rl
    WHERE     cc.name_id_no          = rl.customer_no
    AND       rl.year_of_incident     > TRUNC (cc.discover_date) - 1095
    AND       rl.year_of_incident      < TRUNC (cc.discover_date)
    AND       rl.type_of_loss     < 1000
    AND       rl.timestamp          < TRUNC (cc.discover_date)
    AND       cc.question_class     IN (20, 25)
    ;
    

    If post you some examples of data (CREATE TABLE and INSERT statements) and outcomes from these data, I was able to test this.

    Now comes the second question: the above is actually a subquery that will link to a larger table via cla_case_no ccx

    SELECT

    This is to say, (select distinct z.cla_case_no of)

    Select cc.name_id_no, cc.discover_date, cc.cla_case_no, max (rl.year_of_incident) Non_MW_Loss_Past_5, rl.timestamp
    of cla_case cc, rbn_loss rl
    where cc.name_id_no = rl.customer_no
    and rl.year_of_incident<>
    and rl.type_of_loss<>
    and rl.timestamp<>
    and (cc.question_class = 20
    or cc.question_class = 25)
    and (trunc (cc.discover_date)-(rl.year_of_incident))<>
    -(trunc (cc.discover_date) <> (rl.year_of_incident))
    Group of cc.cla_case_no, name_id_no, cc.discover_date, rl.timestamp
    ) z
    where z.cla_case_no = ccx.cla_case_no
    ) Non_MW_Loss_Past_5

    Etc.

    Now only some cc.cla_case_no the subquery to be match to the ccx_cla_case_no of the main table and the other entries will be void.

    What I am asking, is that if the subquery returns a result that IS NOT NULL to return some ELSE 'Y' "n" instead of her ranges from cla_case_no entries (null) in the Non_MW_Loss_Past_5 column

    NVL2 (x, 'Y', 'N')
    

    Returns 'Y' if x is NULL, and it returns "n" If x is not NULL. X can be a scalar subquery:

    NVL2 ((SELECT ...), 'Y', 'N')
    

    You can also use an EXISTS subquery:

    CASE
        WHEN  EXISTS (SELECT ...)
        THEN  'Y'
        ELSE  'N'
    END
    
  • Use the CASE statement in a query of LOV

    Hello

    I wrote a select statement to be used in my list of values query, and it works fine when I run with SQL Developer. But when I put it in the LOV I get the LOV query is invalid error message. Here's the query I use:
    select 
        case when nt.COMMON_NAME is not null then nt.COMMON_NAME || ' (' || nt.TAXON_NAME || ')'
            else nt.TAXON_NAME
        end display_species
       ,case when nt.COMMON_NAME is not null then nt.COMMON_NAME || ' (' || nt.TAXON_NAME || ')'
            else nt.TAXON_NAME
        end return_species
    FROM NBN_TAXON nt
    WHERE lower(nt.INPUT_CATEGORY) = decode(lower(:P312_TAXON_GROUP_ADD), 'fish', 'fishes', lower(:P312_TAXON_GROUP_ADD))     
    order by 1;
    If the CASE statement is the source of the problem?

    Hello

    Try to remove the colon semi at the end.

    See you soon

    Ben

Maybe you are looking for