Subquery in case statement

I have the following case statement as a value in a larger select statement. I am getting a "MISSING KEYWORD" error when I try to run this. Can someone tell me what I am missing?
CASE upper(QCTYPE)
        WHEN 'DATE' THEN 'PreviousMonth/PreviousYear'
        WHEN 'RANGE' THEN 'between '||cast(s.LOWVALUE as VARCHAR2(25))||' and '||cast(s.HIGHVALUE as VARCHAR2(25))
        WHEN 'RANGEREAL' THEN 'between '||cast(s.LOWVALUE as VARCHAR2(25))||' and '||cast(s.HIGHVALUE as VARCHAR2(25))
        WHEN 'TABLE' THEN (select v.HARVESTCODE||': '||v.DESCRIPTION AS EXVALUE from INTERPRETATIONVALUES v where v.SHORTNAME = s.SHORTNAME
                                        and v.HARVESTCODE = (select min(c.HARVESTCODE) from INTERPRETATIONVALUES c where c.SHORTNAME = s.SHORTNAME)) as EXVALUESUB
        WHEN 'TEXT' THEN 'beginning with the letter A'
    END AS EXPECTEDVALUEThanks,
Eva

The issue was having my tablename referenced as V. Turns out that does not work. That and I actually didn't have one of the columns I was calling in the table. That doesn't work that great!
Thanks for the "mark as answered" reminder. You are correct, I had been lax on that.

Similar Messages

  • Subquery inside CASE statement

    I am trying to use a subquery within a Case statement with a 'where' condition that binds to the parent query. Like this:
    Select T.idperson , CASE WHEN  T.type = 'C' THEN (SELECT name from Customers where C.idcustomer = T.idperson)
    ELSE
    (SELECT name from Providers where idprovider = T.idperson)
    END Name
    from myTable T This works for me in SQL Server but in ORACLE the subquery does not return any rows, i guess its the binding to parent query part. Any thoughts of another way I could get the result I want?
    Thanks,
    JuanDiego

    Hi, Juandiego,
    If it's not returning any rows, then you must not have any rows in the table.
    If you have any rows in the table, you will either get
    (a) an error, or
    (b) some rows (even if the columns are NULL).
    There is another way to get the results you want.
    I probably wouldn't use scalar sub-queries. I would outer-join all the tables, like this:
    Select  T.idperson
    ,       CASE
                WHEN  T.type = 'C'
                THEN  c.name
                ELSE  p.name
            END  AS Name
    from             myTable   t
    LEFT OUTER JOIN  Customers c  ON c.idcustomer = T.idperson
    LEFT OUTER JOIN  Providers p  ON p.idprovider = T.idperson
    ;

  • Case statement with Subquery

    Case statement with Subquery
    select case when condition then
    i want to execute qr1
    else qr2
    How to implement this ?

    Hi,
    Welcoem to the forum!
    972471 wrote:
    Case statement with SubqueryHow does a sub-query, or a query, figure in this question?
    Be more specific. Post a complete script that you tried, and what output you expected from it. If you got an error, post the complete error message, including line numbers.
    See the forum FAQ {message:id=9360002}
    select case when condition then
    i want to execute qr1
    else qr2
    How to implement this ?Here's one way:
    BEGIN
        CASE
            WHEN  1 = 1
            THEN
                dbms_output.put_line ('Condition was TRUE');
            ELSE
                dbms_output.put_line ('Condition was FALSE or UNKNOWN');
        END CASE;
    END;
    /If the condition involves a query, then you may have to select the results into a variable first, and then use the variable in the CASE statement.

  • Case statement in a multiple query

    Hi everyone,
    This is my first time to use case statement in a multiple query. I have tried to implement it but i got no luck.. Please see below
    set define off
    SELECT g.GROUP_NAME as Market
    ,t.NAME as "Template Name"
    ,t.TEMPLATE_ID as "Template ID"
    ,(SELECT created
    FROM material
    where template_id = t.template_id) as "Date Created"
    *,(SELECT DESTINATION_FOLDER_ID,*
    CASE DESTINATION_FOLDER_ID
    WHEN NULL THEN 'Upload'
    ELSE 'HQ'
    END
    from log_material_copy
    where destination_material_id in (select material_id
    from material
    where template_id = t.template_id ))as "Origin"
    ,(select material_id
    from log_material_copy
    where destination_material_id in (select material_id
    from material
    where template_id = t.template_id)) as "HQ/Upload ID"
    ,(SELECT COUNT (mse.ID)
    FROM MATERIAL_SEND_EVENT mse, material m, creative c
    WHERE mse.MATERIAL_ID = m.MATERIAL_ID
    AND mse.MATERIAL_TYPE_ID = m.MATERIAL_TYPE_ID
    AND m.ASSET_ID = c.id
    AND c.TEMPLATE_ID = t.TEMPLATE_ID) as Sent
    ,(SELECT COUNT (de.ID)
    FROM download_event de, material m, creative c
    WHERE de.MATERIAL_ID = m.MATERIAL_ID
    AND de.MATERIAL_TYPE_ID = m.MATERIAL_TYPE_ID
    AND m.ASSET_ID = c.id
    AND c.TEMPLATE_ID = t.TEMPLATE_ID) as Download
    ,(SELECT 'https://main.test.com/bm/servlet/' || 'UArchiveServlet?action=materialInfo&materialId=' || DESTINATION_MATERIAL_ID || '&materialFolderId=' || DESTINATION_FOLDER_ID
    from log_material_copy
    where destination_material_id in (select material_id
    from material
    where template_id = t.template_id)) as "URL to template on MPC layer"
    --, t.AVAILABLE_FOR_TRANSFER as "Available for transfer"
    FROM template t, layout l, groups g
    WHERE t.LAYOUT_ID = l.LAYOUT_ID
    AND l.ORGANIZATION_ID = g.IP_GROUPID
    AND g.IP_GROUPID in ( 1089, 903, 323, 30, 96, 80, 544, 1169, 584, 785, 827, 31, 10, 503, 1025 )
    ORDER BY g.GROUP_NAME ASC;
    The one in bold is my case statement.. Please let me know what is wrong with this.
    Regards,
    Jas

    I think you're getting the idea, but:
    You're still selecting 2 columns in the (scalar) subquery. Did you read the link I posted for you?
    "a) scalar subqueries - *a single row, single column query that you use in place of a "column"*, it looks like a column or function."
    You must move that query outside, join to template.
    Something like:
    NOT TESTED FOR OBVIOUS REASONS SO YOU'LL PROBABLY NEED TO TWEAK IT A BIT
    select g.group_name as market,
           t.name as "Template Name",
           t.template_id as "Template ID",
           m.created  as "Date Created",
           lmc.destination_folder_id,
           case lmc.destination_folder_id
             when null then 'Upload'
             else 'HQ'
           end as "Origin"
           (select material_id
              from log_material_copy
             where destination_material_id in
                   (select material_id
                      from material
                     where template_id = t.template_id)) as "HQ/Upload ID"
           (select count(mse.id)
              from material_send_event mse, material m, creative c
             where mse.material_id = m.material_id
               and mse.material_type_id = m.material_type_id
               and m.asset_id = c.id
               and c.template_id = t.template_id) as sent
           (select count(de.id)
              from download_event de, material m, creative c
             where de.material_id = m.material_id
               and de.material_type_id = m.material_type_id
               and m.asset_id = c.id
               and c.template_id = t.template_id) as download
           (select 'https://main.test.com/bm/servlet/' ||
                   'UArchiveServlet?action=materialInfo&materialId=' ||
                   destination_material_id || '&materialFolderId=' ||
                   destination_folder_id
              from log_material_copy
             where destination_material_id in
                   (select material_id
                      from material
                     where template_id = t.template_id)) as "URL to template on MPC layer"
    --, t.AVAILABLE_FOR_TRANSFER as "Available for transfer"
      from template t
      ,    layout l
      ,    groups group by
      ,    MATERIAL M
      ,    LOG_MATERIAL_COPY LMC
    where t.layout_id = l.layout_id
       and l.organization_id = g.ip_groupid
       and M.TEMPLATE_ID = t.template_id
       and LMC.destination_material_id in ( select material_id
                                            from   material
                                            where  template_id = t.template_id
       and g.ip_groupid in (1089,
                            903,
                            323,
                            30,
                            96,
                            80,
                            544,
                            1169,
                            584,
                            785,
                            827,
                            31,
                            10,
                            503,
                            1025)
    order by g.group_name asc;

  • CASE statement in SQL Server

    I am working on a project for ambulance response times. In
    the following query which is in my coldfusion code, I am using a
    CASE statement on a subquery to count the ambulance response times
    in bins. An ambulance should arrive at an emergency incident in
    less than 8:59 (539 seconds) or else it is considered late. In my
    coldfusion Transact-SQL code I am:
    1.) doing a subquery.
    2.) counting the 'event numbers' based on the time it took
    for the ambulance to arrive.
    3.) only counting Lee County ambulances and excluding A6 type
    calls (non-emergencies).
    4.) grouping it by the dateparts.
    SELECT DATENAME("M", I.I_tTimeDispatch) as mths,
    (DATEPART("yyyy", I.I_tTimeDispatch)) AS yr,
    COUNT(CASE WHEN (DATEDIFF("S",I.I_tTimeDispatch,
    I.I_tTimeArrival)) BETWEEN 0 AND 539 THEN evnt END) AS OnTime,
    COUNT(CASE WHEN (DATEDIFF("S",I.I_tTimeDispatch,
    I.I_tTimeArrival)) BETWEEN 540 AND 1028 THEN evnt END) AS Late,
    COUNT(CASE WHEN (DATEDIFF("S",I.I_tTimeDispatch,
    I.I_tTimeArrival)) > 1028 THEN evnt END) AS Outlier
    FROM (SELECT I_EventNumber AS evnt, I_tTimeDispatch,
    I_tTimeArrival, I_kTypeInfo, I_Agency FROM dbo.IIncident) as I
    INNER JOIN dbo.ITypeInfo AS T ON I.I_kTypeInfo =
    T.ITI_TypeInfo_PK
    WHERE I.I_Agency='LC'
    AND T.ITI_TypeID NOT LIKE 'A6*'
    GROUP BY (DATEPART("M", I.I_tTimeDispatch)), (DATENAME("M",
    I.I_tTimeDispatch)), (DATEPART("yyyy", I.I_tTimeDispatch))
    ORDER BY (DATEPART("yyyy", I.I_tTimeDispatch)) ASC,
    (DATEPART("M", I.I_tTimeDispatch)) ASC
    Here is my problem!
    I go into Microsoft Access to verify my statistics and I get
    different counts. For instance, in April 2008 my coldfusion query
    returns 3,944 on-time ambulance responses. My Access query for the
    same time period using only Lee County ambulances and excluding A6
    non-emergencies returns only 3,805 responses. This is an undercount
    of 139 responses. Even for my other time bins I am getting an
    undercount.
    Here is my Access SQL for the on time response bin (<539
    seconds or 8:59):
    SELECT Count(dbo_IIncident.I_EventNumber) AS
    CountOfI_EventNumber
    FROM dbo_IIncident INNER JOIN dbo_ITypeInfo ON
    dbo_IIncident.I_kTypeInfo = dbo_ITypeInfo.ITI_TypeInfo_PK
    WHERE (((dbo_IIncident.I_Agency)="lc") AND
    ((dbo_ITypeInfo.ITI_TypeID) Not Like "a6*") AND
    ((dbo_IIncident.I_tTimeDispatch) Between #4/1/2008# And #5/1/2008#)
    AND
    ((DateDiff("s",[dbo_IIncident]![I_tTimeDispatch],[dbo_IIncident]![I_tTimeArrival]))
    Between 0 And 539));
    How could two queries that are supposed to be doing the same
    thing return such different results?
    To clear up any confusion I am temporarily posting the page.
    Please look at it because it may help you visualize the problem.
    http://lcfcfn01/Secure/GTandLT_8_59.cfm

    Thank you for your quick reply.
    I thought about that, but it isn't what is causing the
    discrepancy in the numbers. This is because Access is hitting the
    SQL Server through ODBC. The time stamps in SQL Server are ODBC
    datetime stamps so they look like this: 4/19/2008 6:20:18 PM
    When my query uses the date #5/1/2008# it is like saying May
    1, 2008 00:00:00. Please correct me if I am wrong. The query won't
    return any results from May 1, 2008 because it stops at zero
    hundred hours. I believe it will only go to April 30, 2008 23:59:59
    and then stop there.
    I do try and play with the date ranges and the 'seconds'
    (<539 or >539) parameter and I consistently get different
    results from what my coldfusion page is telling me.
    David

  • Case Statement in sub query

    Hi, I have two issues, 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
    from 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 (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
    --and (trunc(cc.discover_date) <> (rl.year_of_incident))
    group by 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 only want the cla_case_no that maps to the max year_of_incident ie There should only be a single cla_case_no corresponding to the max year_of_incident.
    To get around this I did the following which is not very efficient and I'm hoping 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
    from 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 (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
    --and (trunc(cc.discover_date) <> (rl.year_of_incident))
    group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
    ) z
    Now comes the second issue: The above is actually a subquery that will link to a bigger table via cla_case_no ccx
    SELECT
    ie ,(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
    from 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 (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
    --and (trunc(cc.discover_date) <> (rl.year_of_incident))
    group by 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
    FROM etc
    Now only certain cc.cla_case_no from the subquery will corresp to the ccx_cla_case_no from the main table and the other entries will be null.
    What I require is that if the subquery returns a result that IS NOT NULL to return 'Y' ELSE 'N' instead of the varies cla_case_no's and (null) entries in the Non_MW_Loss_Past_5 column
    Thanks!!!
    Banner:
    Oracle Database 11g Release 11.2.0.2.0 - 64bit Production
    PL/SQL Release 11.2.0.2.0 - Production
    "CORE 11.2.0.2.0 Production"
    TNS for Linux: Version 11.2.0.2.0 - Production
    NLSRTL Version 11.2.0.2.0 - Production

    Hi,
    It looks like you have another copy of this question:
    Case Statement and sub query
    That's probably not your fault, but you should mark the other copy as "Answered" right away, and then you'll only have to look for replies in one place.
    885178 wrote:
    ... Now a cla_case_no can map to several year_of_incident. I only want the cla_case_no that maps to the max year_of_incident ie There should only be a single cla_case_no corresponding to the max year_of_incident.If you know there will only be one, then you can use LAST, and you don't need GrOUP BY
    To get around this I did the following which is not very efficient and I'm hoping 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
    from 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 (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
    --and (trunc(cc.discover_date) <> (rl.year_of_incident))
    group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
    ) zHere'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 you'd post some sample data (CREATE TABLE and INSERT statements) and the results you want from that data, then I could test this.
    Now comes the second issue: The above is actually a subquery that will link to a bigger table via cla_case_no ccx
    SELECT
    ie ,(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
    from 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 (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
    --and (trunc(cc.discover_date) <> (rl.year_of_incident))
    group by 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
    FROM etc
    Now only certain cc.cla_case_no from the subquery will corresp to the ccx_cla_case_no from the main table and the other entries will be null.
    What I require is that if the subquery returns a result that IS NOT NULL to return 'Y' ELSE 'N' instead of the varies cla_case_no's and (null) entries 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 sub-query:
    NVL2 ((SELECT ...), 'Y', 'N')You could also use an EXISTS sub-query:
    CASE
        WHEN  EXISTS (SELECT ...)
        THEN  'Y'
        ELSE  'N'
    END

  • Why doesn't this case statement work?

    SELECT
    case when PRODUCTS.PRODUCT_NAME in
    select case when rank() over(order by ( sum(ag.RX_CNT) ) desc) < 6 then ( p.PRODUCT_NAME ) else 'XXXX' END RankedProduct
    FROM
    PAP_MONTHLYTIME_DIM m,
    PAP_PRESCRIPTIONS_DEMOG_AGG ag,
    PRODUCTS p,
    PAP_ENROLLMENT_FLAGS_DIM f
    WHERE
    ( m.MONTHLYTIME_DIM_ID = ag.MONTHLYTIME_DIM_ID )
    AND ( ag.ENROLLMENT_FLAGS_DIM_ID = f.ENROLLMENT_FLAGS_DIM_ID )
    AND ( p.PRODUCT_ID = ag.PRODUCT_DIM_ID )
    AND ( f.ACTIVE_FLAG = 'Y' )
    AND m.CALENDAR_YEAR_MONTH = '2007-04'
    GROUP BY
    m.MONTH_END_DATE,
    p.PRODUCT_NAME
    then
    ( PRODUCTS.PRODUCT_NAME ) else 'All Other' end as ProdNm,
    PRODUCTS.PRODUCT_NAME,
    sum(PAP_PRESCRIPTIONS_DEMOG_AGG.RX_CNT)
    FROM
    PRODUCTS,
    PAP_PRESCRIPTIONS_DEMOG_AGG
    WHERE
    ( PRODUCTS.PRODUCT_ID=PAP_PRESCRIPTIONS_DEMOG_AGG.PRODUCT_DIM_ID )
    GROUP BY
    PRODUCTS.PRODUCT_NAME
    The first case statement is not working properly. First off - I know I can do this without the subquery in the case statement, but it's then tied to the Month and I don't want that.
    The result set of the subquery contains valid product_names that match EXACTLY (I added LTRIM RTRIM just in case), but the ProdNm field still evaluates to "All Other" for them. If I change the subquery to something basic and remove the rank function, it works, but of course I need that function. My understanding is that it shouldn't matter what function is in the subquery. I thought Oracle would get the result set of the subquery first, then evaluate the case statement based on the result set (the subquery is obviously not correlated).
    Any ideas?
    Thanks.

    My understanding is that it shouldn't matter what function is in the subquery. I thought Oracle would get the result set of the subquery first, then evaluate the case statement based on the result set (the subquery is obviously not correlated). It looks like the queries ARE somehow correlated. Consider the two simplified queries:
    michaels>  SELECT   ename, deptno,
             CASE
                WHEN deptno IN (
                       SELECT CASE
                                 WHEN ROW_NUMBER () OVER (ORDER BY NULL) < 3
                                    THEN deptno
                                 ELSE 1000
                              END
                         FROM dept)
                   THEN 'Found'
                ELSE 'NOT Found'
             END FOUND
        FROM emp
    ORDER BY deptno
    ENAME          DEPTNO FOUND   
    CLARK              10 NOT Found
    KING               10 NOT Found
    MILLER             10 NOT Found
    JONES              20 NOT Found
    FORD               20 NOT Found
    ADAMS              20 NOT Found
    SMITH              20 NOT Found
    SCOTT              20 NOT Found
    WARD               30 NOT Found
    TURNER             30 NOT Found
    ALLEN              30 NOT Found
    JAMES              30 NOT Found
    BLAKE              30 NOT Found
    MARTIN             30 NOT Found
    michaels>  SELECT   ename, deptno,
             CASE
                WHEN deptno IN (
                       SELECT *
                         FROM (SELECT CASE
                                         WHEN ROW_NUMBER () OVER (ORDER BY NULL) < 3
                                            THEN deptno
                                         ELSE 1000
                                      END
                                 FROM dept))
                   THEN 'Found'
                ELSE 'NOT Found'
             END FOUND
        FROM emp
    ORDER BY deptno
    ENAME          DEPTNO FOUND   
    CLARK              10 Found   
    KING               10 Found   
    MILLER             10 Found   
    JONES              20 Found   
    FORD               20 Found   
    ADAMS              20 Found   
    SMITH              20 Found   
    SCOTT              20 Found   
    WARD               30 NOT Found
    TURNER             30 NOT Found
    ALLEN              30 NOT Found
    JAMES              30 NOT Found
    BLAKE              30 NOT Found
    MARTIN             30 NOT FoundSo in the first query ROW_NUMBER() evaluates to NULL (not sure why) so the condition will never be satisfied! This is easily proofed when actually testing for nullity:
    WHEN ROWNUM() OVER (ORDER BY NULL) IS NULL THEN
    ...will always show 'Found'!
    Inlining the subquery »materializes« it, and ROW_NUMBER() gets the desired value.

  • APEX_ITEM.MD5_CHECKSUM and Case Statements

    I have an updateable report
    select apex_item.display_and_save(3,parameter) ||
    APEX_ITEM.MD5_CHECKSUM(parameter,value_char,value_number,to_char(value_date,'dd.mm.yyyy'),param_set) ||
    apex_item.hidden(2,rownum) parameter
    ,value_char value_char_current
    ,APEX_ITEM.TEXT(4,value_char,20,128) value_char_new
    ,case when parameter in ('OBJINCID', 'HISTTRANSFERID') then '<span style="color:#000000;font-weight:bold">'||value_number||'</span>'
                else to_char(value_number) end  value_number_current
    ,APEX_ITEM.TEXT(5,value_number,10) value_number_new
    ,case when parameter = 'TRANSFERDAY' then '<span style="color:#000000;font-weight:bold">'||to_char(value_date,'dd.mm.yyyy')||'</span>'
                else to_char(value_date,'dd.mm.yyyy') end value_date_current
    ,APEX_ITEM.DATE_POPUP2(6,value_date,'DD.MM.YYYY','15','10','style=""',null,null,null,null,null,'both',null,'MONTH_AND_YEAR') value_date_new
    ,apex_item.display_and_save(7,param_set) param_set
    ,apex_item.display_and_save(8,param_type) param_type
    ,APEX_ITEM.TEXT(9,jobname) jobname
    ,system system
    ,config config
    ,APEX_ITEM.TEXT(10,parameter) parameter_hid
    ,APEX_ITEM.TEXT(11,value_number) vnhid
    ,APEX_ITEM.DATE_POPUP2(12,value_date,'DD.MM.YYYY','15','10','style=""',null,null,null,null,null,'both',null,'MONTH_AND_YEAR') vdhid
    from etl_job_param
    where config = substr(:P285_CONFIG,instr(:P285_CONFIG,'_')+1)
    and system = :P285_SYSTEMI would like to change my sql for the value_date_new col to
    ,case when parameter = 'TRANSFERDAY' then
    APEX_ITEM.DATE_POPUP2(6,NULL,'DD.MM.YYYY','15','10','style="" disabled="disabled"',null,null,null,null,null,'both',null,'MONTH_AND_YEAR')
    else
    APEX_ITEM.DATE_POPUP2(6,value_date,'DD.MM.YYYY','15','10','style=""',null,null,null,null,null,'both',null,'MONTH_AND_YEAR')  end value_date_newI think I need to change my checksum so if parameter = 'TRANSFERDAY' then use the value from the vdhid column and not value_date_new.
    It is not allowing me to use column alias in the checksum
    How do I do this ?
    Gus

    Well, you could either put the CASE statement in the checksum parameter as well (duplicating logic = bad) or use a factored subquery with the case statment in and alias that column, then reference it in the main query (logic in one place = good).
    Cheers
    Ben

  • SQL Expression in decode function or case statement?

    Can I put SQL expressions in decode function or case statement?
    For example,
    select le.profile, decode( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile, 0, 'N', 'Y')
    from element le;
    or
    select le.profile, case WHEN ( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile) = 0 THEN 'N'
    ELSE 'Y'
    from element le;
    None of the above work.
    Can anyone tell me how to make it work?
    Is there any workaround?
    Thanks,
    J

    You simply needed and END to your CASE statement;
    SQL> with profile_data as (
       select 'XXXX_AFTER' name, 1 object_id from dual),
         element as (
       select 1 profile from dual union all
       select 2 from dual)
    select le.profile,
       case WHEN ( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile) = 0
       THEN 'N'
       ELSE 'Y'
       END new_col
    from element le
       PROFILE N
             1 Y
             2 N

  • CASE not found while executing CASE statement on Submit Form

    Hi to all APEX users and developers.
    I have several APEX applications and they are all working well, but recently I got one strange exception when I try to submit page:
    Session: Fetch session header information
    ...metadata, fetch page info
    ...Validate item page affinity.
    ...Validate hidden_protected items.
    Add error onto error stack
    ...Error data:
    ......message: Error processing request.
    ......additional_info: ORA-06592: CASE not found while executing CASE statement
    ......display_location: ON_ERROR_PAGE
    ......is_internal_error: true
    ......apex_error_code: APEX.UNHANDLED_ERROR
    ......ora_sqlcode: -6592
    ......ora_sqlerrm: ORA-06592: CASE not found while executing CASE statement
    .....error_backtrace: ORA-06512: at "APEX_040100.WWV_FLOW", line 9273
    ......component.type: APEX_APPLICATION_AUTH
    ......component.id: 41350431648668800
    ......component.name: MNRFR
    ...Show Error on Error Page
    ......Performing rollback
    Processes - point: AFTER_ERROR_HEADER
    Processes - point: BEFORE_ERROR_FOOTER
    End Page Processinga
    Page has more than 120 items (most of them are hidden), so my first thought is that page has problem with posting so many items, but APEX error message doesn't hel me at all. Any help would be very appreciated :)
    Almir

    Hi Almir,
    actually it is the 100 page item limit. See (http://download.oracle.com/docs/cd/E17556_01/doc/user.40/e15517/limits.htm)
    I also had a look into the source code and the code at the position where the error gets raised only supports 100 page items.
    Can you have a look into the generated HTML code and look if you have a page items which is mapped to p_t101 or a higher number ?
    I will file a bug to show a better error message.
    Regards
    Patrick
    My Blog: http://www.inside-oracle-apex.com
    APEX Plug-Ins: http://apex.oracle.com/plugins
    Twitter: http://www.twitter.com/patrickwolf

  • Case statement

    hi.
    i have a case statement query. i wonder if in the select statement, can i do computation using different parameter from the main report to subreport?
    for example, (qty * parameter)
    if parameter = 0, i uses $P{abc} to multiply qty
    if parameter > 0, i uses $P{xyz} to multiply qty
    in this case, i've writen a sql (as below) but its does not execute.
    select ....., (QTY *
    case $P{QTY}
    when 0 then ' * $P{abc}'
    else ' $P{xyz}'
    end
    from....
    hence, what should i do in order to get the right parameter to multiply with? pls guide. thanks.

    I'm not sure about these parameter placeholders which are specific to whatever report tool you are using, but the structure would be (assuming the parameter value would never be less than zero):
    qty * case when $P{QTY} = 0 then $P{abc} else $P{xyz} end

  • Case Statement in Answers Filter

    Hi,
    I'm trying to build a query in Answers that filters the data returned based on the current month number. If the current month is 1 then we want to show all 12 months, otherwise we want to show months less than the current month. If I use the following:
    CASE WHEN extract(month from current_date) = 1 THEN "Calculated Values Monthly"."MONTH" >= 1 ELSE "Calculated Values Monthly"."MONTH" < extract (month from current_date) END
    I get the error below. All works fine until I add the CASE.
    Error Codes: YQCO4T56:OPR4ONWY:U9IM8TAC:OI2DL65P
    Location: saw.views.evc.activate, saw.httpserver.processrequest, saw.rpc.server.responder, saw.rpc.server, saw.rpc.server.handleConnection, saw.rpc.server.dispatch, saw.threadpool, saw.threadpool, saw.threads
    Odbc driver returned an error (SQLExecDirectW).
    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 43113] Message returned from OBIS. [nQSError: 27002] Near <>=>: Syntax error [nQSError: 26012] . (HY000)
    Can you use a case statement in a filter? If not, what are the other options, if any?
    Thx

    ziekc wrote:
    Hi,
    I'm trying to build a query in Answers that filters the data returned based on the current month number. If the current month is 1 then we want to show all 12 months, otherwise we want to show months less than the current month. If I use the following:
    CASE WHEN extract(month from current_date) = 1 THEN "Calculated Values Monthly"."MONTH" >= 1 ELSE "Calculated Values Monthly"."MONTH" < extract (month from current_date) END
    I get the error below. All works fine until I add the CASE.
    Error Codes: YQCO4T56:OPR4ONWY:U9IM8TAC:OI2DL65P
    Location: saw.views.evc.activate, saw.httpserver.processrequest, saw.rpc.server.responder, saw.rpc.server, saw.rpc.server.handleConnection, saw.rpc.server.dispatch, saw.threadpool, saw.threadpool, saw.threads
    Odbc driver returned an error (SQLExecDirectW).
    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 43113] Message returned from OBIS. [nQSError: 27002] Near <>=>: Syntax error [nQSError: 26012] . (HY000)
    Can you use a case statement in a filter? If not, what are the other options, if any?
    ThxAccording to your requirements, when the month is January, you want all 12 months; any other number and you want months from 1 to "1 less than the current month number." Yes? Okay, here's what to do:
    In the filter on your date column, click on the filter button, convert it to SQL and enter this:
    MONTH(tablename.date_column) BETWEEN 1 AND CASE WHEN MONTH(CURRENT_DATE) = 1 THEN 12 ELSE MONTH(CURRENT_DATE)-1 END
    Here's the logic:
    a) When the current month is January, then the case statement will yield 12 and the filter will be:
    WHERE MONTH(tablename.date_column) BETWEEN 1 AND 12 ...or all 12 months.
    b) When the current month is any other month, say June, then the case statement will yield one less than the what the month number is, or in this case 5.
    WHERE MONTH(tablename.date_column BETWEEN 1 AND 5 ...or all the months from 1 to 5 (Jan through May)
    This will give you what you want...

  • CASE statement in Calculated column

    Hi Frzz,
    I have below requirement in Calculated column with CASE statement. Could some one help me how to achieve this with case statement.
    String  =   0Hello
                    01Hello
                    012Hello
                    0123Hello
    If  1st Character of the string is '0' then  -  0Hello
        1st 2 characters of the String is '01'  -   22Hello
        1st 3 characters of the String is '01'  -   333Hello
        1st 4 characters of the String is '01'  -  4444Hello
    Thank you.
    Best Regards,
    Krishna.

    Hi Krishna ,
    Using IF and MATCH to do that: ( I took one of the conditions you specified )
    IF(match("STRING1",'??0??'),'333Hello',"STRING1")
    Output:
    Regards,
    Krishna Tangudu

  • CASE statement in PL/SQL

    Hi PL/SQL experts,
    I'm going a bit loopy here, so could someone please point out what I'm 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('We need a space in the postcode please');
    GOTO exit;
    ELSIF LENGTH(SUBSTR(input_post_code,instr(input_post_code,' ')+1)) > 3
    THEN
    DBMS_OUTPUT.PUT_LINE('Last part of postcode can only be 3 characters');
    GOTO exit;
    ELSE
    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 postcode in the format A9')
    -- ELSE
    -- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format A9');
    -- GOTO exit;
    -- 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 postcode in the format AA9')
    -- ELSE
    -- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format AA9');
    -- GOTO exit;
    -- 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 postcode in the format A99')
    -- ELSE
    -- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format A99');
    -- GOTO exit;
    -- 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 postcode in the format A9A')
    -- ELSE
    -- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format A9A');
    -- GOTO exit;
    -- 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 postcode in the format AA99')
    -- ELSE
    -- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format AA99');
    -- GOTO exit;
    -- 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 postcode in the format AA9A')
    -- ELSE
    -- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format AA9A');
    -- GOTO exit;
    -- END IF;
    END;
    -- Check last part of 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 postcode is in the correct format, 9AA');
    ELSE
    DBMS_OUTPUT.PUT_LINE('End part of the postcode is in the wrong format!');
    END IF;
    <<exit>>
    DBMS_OUTPUT.PUT_LINE('Please try again');
    END;
    However, I'm getting the following error:
    LINE/COL ERROR
    37/6 PLS-00103: Encountered the symbol "WHEN" when expecting one of
    the following:
    := . ( % ;
    On a second note, can I not have the ELSE structure embedded within the case (currently commented out)?
    Thanks very much in advance.
    Dev

    Fixed code:
    CREATE OR REPLACE procedure 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('We need a space in the postcode please');
    GOTO exit;
    ELSIF LENGTH(SUBSTR(input_post_code,instr(input_post_code,' ')+1)) > 3
    THEN
    DBMS_OUTPUT.PUT_LINE('Last part of postcode can only be 3 characters');
    GOTO exit;
    ELSE
    last_part_postcode := SUBSTR(input_post_code,instr(input_post_code,' ')+1);
    END IF;
    CASE
    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 postcode in the format A9');
    -- ELSE
    -- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format A9');
    -- GOTO exit;
    -- 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 postcode in the format AA9');
    -- ELSE
    -- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format AA9');
    -- GOTO exit;
    -- 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 postcode in the format A99');
    -- ELSE
    -- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format A99');
    -- GOTO exit;
    -- 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 postcode in the format A9A');
    -- ELSE
    -- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format A9A');
    -- GOTO exit;
    -- 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 postcode in the format AA99');
    -- ELSE
    -- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format AA99');
    -- GOTO exit;
    -- 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 postcode in the format AA9A');
    -- ELSE
    -- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format AA9A');
    -- GOTO exit;
    -- END IF;
    END CASE;
    -- Check last part of 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 postcode is in the correct format, 9AA');
    ELSE
    DBMS_OUTPUT.PUT_LINE('End part of the postcode is in the wrong format!');
    END IF;
    <<exit>>
    DBMS_OUTPUT.PUT_LINE('Please try again');
    END;
    /As VG2 pointed out you were missing some semicolons.
    Also, CASE input_post_code was replaced with just CASE since you are doing a searched case.
    Also, END at the end of the case statement was replaced with END CASE.

  • Case statement in advanced sql

    how to use case statement in advanced sql.Example if quarter=1 i need to select jan,fev,and march months from table

    this is the case statement i am using :
    case when 1=@{quarter} then substring(cast((etxnmis.month_year) as char) from 5 for 2) IN ('03', '04', '05') else null end
    and i get the below error
    : HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 27002] Near <IN>: Syntax error [nQSError: 26012] . (HY000)

Maybe you are looking for