Decode a Case statement to insert total text

When the AGE GROUP fields are empty or Null, I need to insert text 'Total '? Can anyone help?

Table
-----------------------------------------------------------------------------------------

SOURCE CODE AGE SUPPORT ACCOUNT
CLUBBEN 0-40 years 3
CLUBBEN 41 to 49 6
CLUBBEN 50-59 years 38
CLUBBEN 60-69 years 205
CLUBBEN 70-79 years 181
CLUBBEN 80 + years 19
CLUBBEN 452
41 to 49 2 CLUBJUNE
CLUBJUNE 50-59 years 21
CLUBJUNE 60-69 years old 100
CLUBJUNE 80 + years 1
CLUBJUNE 124
TOTAL 576
--------------------------------------------------------------------------------------------

Script currently entered
--------------------------------------------------------------------------------------------
SELECT DECODE (GROUPING (F.SOURCE_CODE), 1, 'TOTAL', 0, F.SOURCE_CODE) in the "SOURCE CODE"
CASE
WHEN D.AGE BETWEEN '0' AND '40', THEN ' 0 - 40 years"
WHEN D.AGE BETWEEN '41' AND '49' THEN 41-49 years
WHEN D.AGE BETWEEN '50' AND '59' and THEN 50-59 years
WHEN D.AGE '60' AND '69' and THEN 60-69 years
WHEN D.AGE '70' TO '79' CAN 70-79 years
WHEN D.AGE > = "80" AND "80s".
ELSE "
END AS "AGE GROUP."
COUNT (F.MEMBER_COUNT) 'COUNT '.
OF A3_FACT_NEW F, DIM_AGE D
WHERE F.AGE_KEY = D.AGE
"AND F.JOIN_DATE BETWEEN 25 JUNE 2012 ' AND 30 AUGUST 2012"
AND F.BEN_TYPE = 'first Member '.
AND F.SOURCE_CODE IN ('CLUBBEN', 'CLUBJUNE')
GROUP BY ROLLUP (F.SOURCE_CODE,
CASE
WHEN D.AGE BETWEEN '0' AND '40', THEN ' 0 - 40 years"
WHEN D.AGE BETWEEN '41' AND '49' THEN 41-49 years
WHEN D.AGE BETWEEN '50' AND '59' and THEN 50-59 years
WHEN D.AGE '60' AND '69' and THEN 60-69 years
WHEN D.AGE '70' TO '79' CAN 70-79 years
WHEN D.AGE > = "80" AND "80s".
ELSE "
END)
ORDER BY (F.SOURCE_CODE), (2)
--------------------------------------------------------------------------------------------------------------

Try:

SELECT
  DECODE(GROUPING(F.SOURCE_CODE),1,'TOTAL',0,F.SOURCE_CODE) AS "SOURCE CODE",
  DECODE(GROUPING(
    CASE
      WHEN D.AGE BETWEEN '0' AND '40'
      THEN '0-40 Years'
      WHEN D.AGE BETWEEN '41' AND '49'
      THEN '41-49 Years'
      WHEN D.AGE BETWEEN '50' AND '59'
      THEN '50-59 Years'
      WHEN D.AGE BETWEEN '60' AND '69'
      THEN '60-69 Years'
      WHEN D.AGE BETWEEN '70' AND '79'
      THEN '70-79 Years'
      WHEN D.AGE >= '80'
      THEN '80+ Years'
      ELSE ''
    END ),1,'TOTAL',
  CASE
    WHEN D.AGE BETWEEN '0' AND '40'
    THEN '0-40 Years'
    WHEN D.AGE BETWEEN '41' AND '49'
    THEN '41-49 Years'
    WHEN D.AGE BETWEEN '50' AND '59'
    THEN '50-59 Years'
    WHEN D.AGE BETWEEN '60' AND '69'
    THEN '60-69 Years'
    WHEN D.AGE BETWEEN '70' AND '79'
    THEN '70-79 Years'
    WHEN D.AGE >= '80'
    THEN '80+ Years'
    ELSE ''
  END) AS"AGE BRACKET",
  COUNT(F.MEMBER_COUNT) "COUNT"
FROM
  A3_FACT_NEW F,
  DIM_AGE D
WHERE
  F.AGE_KEY = D.AGE
AND F.JOIN_DATE BETWEEN '25/JUNE/2012' AND '30/AUGUST/2012'
AND F.BEN_TYPE     = 'Prime member'
AND F.SOURCE_CODE IN ('CLUBBEN','CLUBJUNE')
GROUP BY
  ROLLUP(F.SOURCE_CODE,
  CASE
    WHEN D.AGE BETWEEN '0' AND '40'
    THEN '0-40 Years'
    WHEN D.AGE BETWEEN '41' AND '49'
    THEN '41-49 Years'
    WHEN D.AGE BETWEEN '50' AND '59'
    THEN '50-59 Years'
    WHEN D.AGE BETWEEN '60' AND '69'
    THEN '60-69 Years'
    WHEN D.AGE BETWEEN '70' AND '79'
    THEN '70-79 Years'
    WHEN D.AGE >= '80'
    THEN '80+ Years'
    ELSE ''
  END)
ORDER BY
  (F.SOURCE_CODE),
  (2)

Tags: Database

Similar Questions

  • Nested Case statement

    Hello
    I need help implementation of multiple Case statements in a select statement. I can not it works in a statement.
    SELECT statement
    select distinct 
    allt.fk_jobcode_id as Job_Code, 
    allt.fk_jobcode_descr as Classification,
    sum(allt.number_of_positions) as Number_Allotted,
    sum(number_of_positions) as Sum_Number_Allotted,
         dept.department_name|| ''||nvl2((CASE WHEN div.division_name <> 'N/A'
        THEN div.division_name  
      END),'/'||DIV.DIVISION_NAME||'',null) as Department_Division,
    allt.fund_type as Fund
    from tbl_allotment allt, tbl_department dept, tbl_division div
    where isdeleted <> 'Y'
     and ALLT.FK_DEPARTMENT_ID = dept.department_id
     and ALLT.FK_DIVISION_ID = div.division_id
    group by fk_jobcode_id, fk_jobcode_descr, fund_type, department_name, division_name 
    HAVING (count(fk_jobcode_descr ) = 1)
    order by Classification asc, Department_Division asc
    1st CASE statement combines the Department_Name and Division_Name values, but does not display the Division_Name if the value is "N/a".
         dept.department_name|| ''||nvl2((CASE WHEN div.division_name <> 'N/A'
        THEN div.division_name  
      END),'/'||DIV.DIVISION_NAME||'',null) as Department_Division,
    value: administrator Office/Administration Council
    value: poster of the administrator of the Office/s/o Board in the Office of the Director of the Board

    I need to extend this now using this CASE statement, which displays the text "Public works" If the department_id = 00072.
         (CASE dept.department_id 
         When '00072' then 'Public Works'
         Else dept.Department_Name
         End) as Department_Name, 
    incorrect: road Rehab program/GLP transport program Admin
    Must fill as: work GLP/public transport program Admin
    select distinct 
    allt.fk_jobcode_id as Job_Code, 
    allt.fk_jobcode_descr as Classification,
    sum(allt.number_of_positions) as Number_Allotted,
    sum(number_of_positions) as Sum_Number_Allotted,
         dept.department_name|| ''||nvl2((CASE WHEN div.division_name <> 'N/A'
        THEN div.division_name  
      END),'/'||DIV.DIVISION_NAME||'',null) as Department_Division,
      (CASE dept.department_id 
         When '00072' then 'Public Works'
        ELSE dept.Department_Name|| ''||nvl2((CASE WHEN div.division_name <> 'N/A'
        THEN div.division_name  
           END),'/'||DIV.DIVISION_NAME||'',null)
      END) as Department_Name1,
    allt.fund_type as Fund
    from tbl_allotment allt, tbl_department dept, tbl_division div
    where isdeleted <> 'Y'
     and ALLT.FK_DEPARTMENT_ID = dept.department_id
     and ALLT.FK_DIVISION_ID = div.division_id
    group by fk_jobcode_id, fk_jobcode_descr, fund_type, department_name, division_name, department_id 
    HAVING (count(fk_jobcode_descr ) = 1)
    order by Classification asc, Department_Division asc
    but once I change the select statement I get only value "Public works" for the Department_Division value. Does not meet Division_Name data. Does anyone know how to combine these CASE statements? Thanks for reading this thread also.

    Hello

    I think you want something like this:

    SELECT       ...
    ,       CASE
               WHEN  department_id = '00072'
               THEN  'Public Works'
               ELSE  dept.department_name
           END
           ||
           CASE
               WHEN  div.division_name != 'N/A'
               THEN  '/' || div.division_name
           END          AS department_name1
    ...
    

    Whenever you have a problem, post a small example (CREATE TABLE and only relevant columns, INSERT statements) of all database tables.
    Also post the results you want from this data, as well as an explanation of how you get these results from these data, with specific examples.
    Always tell what version of Oracle you are using.

  • 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).

  • call the statement box inside the case statement

    I am writing a program which requires me to run a statement box inside another case statement. Although this sounds like a simple thing, I need to be able to call the case statement using a sequence structure, where s1 sends the true value to the case, s2, a fake. Then I need to record the results of this instruction box in a text file. Attatched is a simplified version of what I'm trying to do. Please note that the way my program runs may not call the case statement without using a sequence structure, two nesting box instructions is not feasible. I hope someone out there can help me because I was stuck trying to find this for awhile.

    Thank you

    LVStudent wrote:

    [...] I can't call the case statement without using a sequence structure

    Yes you can.  You just need to be smart.

    LVStudent wrote:

    I don't think I can use a state machine to fix this.

    I think you probably can.

    With respect to your original post. What are these s1 and s2 are you talking about?  My opinion on the matter, it is that you want to select a case based on the values of several controls Boolean.  If this is correct, I do this:

    Build your Boolean controls in a table, convert the table number and insert it into the structure of your business.  No button pressed = 0, s1 = only 1, s2 = only 2 both = 3.   This works for Boolean values as much as you want and is an easy way to make a decision that depends on many entries.

  • Select - the Question of the Case statement

    In my query:

    SELECT WR_ITEM_OID, TITLE,
    Title = WHEN 'No chocolate' THEN (select "#X #_G1_" ACE double groupname)
    When title = 'Chocolate' THEN (select "#X #_G2_" ACE double groupname)
    When title = 'Eraser' THEN (select "#X #_G3_" double groupname)
    When title = "Import complete" THEN (select "#X # 101_ _G' As double GroupName")
    When title = 'Export Total' THEN (select "#X # 102_ _G' As GroupName, the double")
    ANOTHER "END as GroupName
    OF WR_ITEM


    I want to change the title of "Export" when title = 'Export Total'

    How would I do that in this query?

    Thank you

    Hello

    Rich75 wrote:
    In my query:

    SELECT WR_ITEM_OID, TITLE,
    Title = WHEN 'No chocolate' THEN (select "#X #_G1_" ACE double groupname)
    When title = 'Chocolate' THEN (select "#X #_G2_" ACE double groupname)
    When title = 'Eraser' THEN (select "#X #_G3_" double groupname)
    When title = "Import complete" THEN (select "#X # 101_ _G' As double GroupName")
    When title = 'Export Total' THEN (select "#X # 102_ _G' As GroupName, the double")
    ANOTHER "END as GroupName
    OF WR_ITEM

    I want to change the title of "Export" when title = 'Export Total'

    How would I do that in this query?

    Thank you

    You can use another expression BOX to turn the title, usually to himself:

    SELECT  WR_ITEM_OID
    ,     CASE
              WHEN  title = 'Export Total'  THEN  'Export'
                                              ELSE  title
         END     AS title
    ,     CASE
              WHEN  title = 'Non Chocolate' THEN  '#X#_G1_'
              When  title = 'Chocolate'         THEN  '#X#_G2_'
              When  title = 'Gum'         THEN  '#X#_G3_'
              When  title = 'Import Total'  THEN  '#X#_G-101_'
              When  title = 'Export Total'  THEN  '#X#_G-102_'
                                                ELSE  NULL
          END      AS GroupName
    FROM       WR_ITEM
    

    There is no need for all scalar subqueries in double; I replaced the air conditioned and have literals.

    I hope that answers your question.
    If not, post a small example of data (CREATE TABLE and INSERT statements) and the results desired from these data.

  • Case statement in the process Page

    Request Express 3.2.1.00.11

    I put the following code in an anonymous block (will return no error) for a process Page;

    BEGIN
    INSERT
      INTO DAD_ASSESSMENT_REQUEST
    ( tenure_id
    , client_id
    )
    VALUES
    ( :P10101_TENURE_ID
    , :P10101_CLIENT_ID 
    );
    END;
    

    But when I put a statement case it throws the following error;

    1 error has occurred
    ORA-06550: line 7, column 3: PL/SQL: ORA-01747: invalid user.table.column, table.column, or column specification ORA-06550: line 3, column 1: PL/SQL: SQL Statement ignored
    

    BEGIN
    INSERT
      INTO DAD_ASSESSMENT_REQUEST
    ( tenure_id
    , client_id
    , (CASE 
       WHEN (:P10101_MEASURE_OPTION = 'AREA') THEN estimated_area_ha 
       WHEN (:P10101_MEASURE_OPTION = 'LINE') THEN estimated_length_km 
     END)
    )
    VALUES
    ( :P10101_TENURE_ID
    , :P10101_CLIENT_ID 
    , :P10101_X
    );
    END;
    

    Why is this code not work and it is possible to use a case statement in a Page Apex process

    Concerning

    Ben

    Benton says:

    Request Express 3.2.1.00.11

    I put the following code in an anonymous block (will return no error) for a process Page;

    1. BEGIN
    2. INSERT
    3. IN DAD_ASSESSMENT_REQUEST
    4. (tenure_id
    5. client_id
    6. )
    7. VALUES
    8. (: P10101_TENURE_ID)
    9. ,: P10101_CLIENT_ID
    10. );
    11. END;

    But when I put a statement case it throws the following error;

    1. 1 error has occurred
    2. ORA-06550: line 7, column 3: PL/SQL: ORA-01747: specification user.table.column, table.column or invalid column ORA-06550: line 3, column 1: PL/SQL: statement ignored
    1. BEGIN
    2. INSERT
    3. IN DAD_ASSESSMENT_REQUEST
    4. (tenure_id
    5. client_id
    6. (CASE
    7. WHEN (: P10101_MEASURE_OPTION = 'SPACE') THEN estimated_area_ha
    8. WHEN (: P10101_MEASURE_OPTION = 'LINE') THEN estimated_length_km
    9. END)
    10. )
    11. VALUES
    12. (: P10101_TENURE_ID)
    13. ,: P10101_CLIENT_ID
    14. ,: P10101_X
    15. );
    16. END;

    Why is this code not work and it is possible to use a case statement in a Page Apex process

    Of course, it is possible to use a CASE statement or expression (it is the latter) in a page APEX process. However, it is not possible to use an invalid syntax while doing so. Part of a clause INSERT INTO can contain only static column names.

    In general the SQL expressions (and also the bind variable, another cause frequent errors of syntax) can be used only in

    • the projections of the SELECT queries and subqueries
    • the right side of the disposals in the SET update clause
    • the lists of VALUES of INSERTs
    • WHERE clause predicates

    ReemaPuri wrote:

    Try this

    INSERT

    IN DAD_ASSESSMENT_REQUEST

    (tenure_id

    client_id

    estimated_area_ha

    estimated_length_km

    )

    VALUES

    (: P10101_TENURE_ID)

    ,: P10101_CLIENT_ID

    , (SELECT CASE WHEN: P10101_MEASURE_OPTION = 'SPACE' AND THEN: P10101_X)

    OTHERWISE, NULL END OF DOUBLE)

    , (SELECT CASE WHEN: P10101_MEASURE_OPTION = 'LINE' THEN: P10101_X)

    OTHERWISE, NULL END OF DOUBLE)

    );

    It's a good idea, but it can be simplified, eliminating unnecessary subqueries:

    insert into dad_assessment_request (
        tenure_id
      , client_id
      , estimated_area_ha
      , estimated_length_km)
    values (
        :p10101_tenure_id
      , :p10101_client_id
      , case
          when :p10101_measure_option = 'AREA' then :p10101_x
        end
      , case
          when :p10101_measure_option = 'LINE' then :p10101_x
        end);
    
  • Join the results of a case statement

    Hello everyone;

    I can't with a query where I use a case statement to create a new column and attempt to reach an additional table on the resulting column.  I am fairly new to this and learn by trial and error, so if you think that it looks a little ugly or ineffective, I won't be offended.  It is used on my interface based on the web of companies, while I am not able to answer some basic questions such as the version of db.

    My need to use the case statement is the result of some strange methods and somewhat contradictory in order to link the product subcodes (sku, fcsku, fnsku), to a product code parent (ProdID).  In this case, the inventory is stored in the first table with one of the few types of premises SKUs, however the dimensional data only links to the ProdID.  I'm trying to instruction box allows to send the parent a second table (ProdID) code if the sku is there, or a third table if it is found on this table.  The main reason (in this case) I have to this ProdID is I can join the dimensional data of a fourth table.

    My current query:

    SELECT / * + USE_HASH (dbin, sku, xProdID, msr) * /.

    dbin.snapshot_date, dbin.bin_id, dbin. ISBN,

    CASE WHEN sku. ProdID IS NULL THEN xProdID.ITEM_AUTHORITY_ID

    Of ANOTHER sku. ProdID

    END ProdIDMaster,

    dbin. OWNER, dbin. QUANTITY, dbin. CONTAINER_ID,

    MSR. HEIGHT, msr. WIDTH, msr. LENGTH, msr. WEIGHT, msr. DIMENSIONAL_UOM, msr. WEIGHT_UOM,

    MSR. HEIGHT * msr. WIDTH * msr. LENGTH as a Volume

    OF D_BIN_ITEMS dbin

    SKU of LEFT JOIN O_FCSKUS ON dbin. ISBN = sku. FCSKU

    LEFT JOIN (SELECT * FROM D_FNSKU_ProdID_MAP)

    WHERE REGION_ID = 1

    AND SNAPSHOT_DAY = TO_DATE('{RUN_DATE_YYYY/MM/DD}','YYYY/MM/DD')) xProdID

    ON dbin. ISBN = xProdID.FULFILLMENT_NETWORK_SKU

    LEFT JOIN (SELECT * FROM D_MP_ProdID_PKG_MEASUREMENTS)

    WHERE REGION_ID = 1 AND MARKETPLACE_ID = 1 AND PACKAGE_TYPE_ID = 1) msr

    ON ProdIDMaster = msr. ProdID

    WHERE dbin. WAREHOUSE_ID = "PHL5."

    AND dbin. Snapshot_date = TO_DATE('{RUN_DATE_YYYY/MM/DD}','YYYY/MM/DD')

    AND dbin.bin_id like '% P-1-R '.

    ORDER BY dbin.bin_id;

    The error I receive:

    Statement 1 is invalid. ORA-00904: "ProdIDMaster": invalid identifier

    ProdIDMaster appears only in two places, an alias for the case statement, as well as a side of the join statement.  If I simply change the alias name, it has no effect, so I can only assume the question is how I refer to results of the case statement. I also tried to reach the word 'CASE', which returned "1 statement is not valid. "ORA-00904:"The MATTER": invalid identifier.

    Any help or advice would be greatly appreciated.

    Post edited by: 8ea4344e-80ea-44e7-b8ac-d482be7245a7 I made a mistake when I posted the code.  The ProdID_ID shown in the join should have been ProdIDMaster.  I need to change this due to the fact that the name of the column used (which is what I based my alias on) may have been considered confidential information. All the other columns names remained unchanged.

    Hello

    8ea4344e-80EA-44e7-B8AC-d482be7245a7 wrote:

    Hello everyone;

    I can't with a query where I use a case statement to create a new column and attempt to reach an additional table on the resulting column.  I am fairly new to this and learn by trial and error, so if you think that it looks a little ugly or ineffective, I won't be offended.  It is used on my interface based on the web of companies, while I am not able to answer some basic questions such as the version of db.

    My need to use the case statement is the result of some strange methods and somewhat contradictory in order to link the product subcodes (sku, fcsku, fnsku), to a product code parent (ProdID).  In this case, the inventory is stored in the first table with one of the few types of premises SKUs, however the dimensional data only links to the ProdID.  I'm trying to instruction box allows to send the parent a second table (ProdID) code if the sku is there, or a third table if it is found on this table.  The main reason (in this case) I have to this ProdID is I can join the dimensional data of a fourth table.

    My current query:

    SELECT / * + USE_HASH (dbin, sku, xProdID, msr) * /.

    dbin.snapshot_date, dbin.bin_id, dbin. ISBN,

    CASE WHEN sku. ProdID IS NULL THEN xProdID.ITEM_AUTHORITY_ID

    Of ANOTHER sku. ProdID

    END ProdIDMaster,

    dbin. OWNER, dbin. QUANTITY, dbin. CONTAINER_ID,

    MSR. HEIGHT, msr. WIDTH, msr. LENGTH, msr. WEIGHT, msr. DIMENSIONAL_UOM, msr. WEIGHT_UOM,

    MSR. HEIGHT * msr. WIDTH * msr. LENGTH as a Volume

    OF D_BIN_ITEMS dbin

    SKU of LEFT JOIN O_FCSKUS ON dbin. ISBN = sku. FCSKU

    LEFT JOIN (SELECT * FROM D_FNSKU_ProdID_MAP)

    WHERE REGION_ID = 1

    AND SNAPSHOT_DAY = TO_DATE('{RUN_DATE_YYYY/MM/DD}','YYYY/MM/DD')) xProdID

    ON dbin. ISBN = xProdID.FULFILLMENT_NETWORK_SKU

    LEFT JOIN (SELECT * FROM D_MP_ProdID_PKG_MEASUREMENTS)

    WHERE REGION_ID = 1 AND MARKETPLACE_ID = 1 AND PACKAGE_TYPE_ID = 1) msr

    ON ProdIDid_ID = msr. ProdID

    WHERE dbin. WAREHOUSE_ID = "PHL5."

    AND dbin. Snapshot_date = TO_DATE('{RUN_DATE_YYYY/MM/DD}','YYYY/MM/DD')

    AND dbin.bin_id like '% P-1-R '.

    ORDER BY dbin.bin_id;

    The error I receive:

    Statement 1 is invalid. ORA-00904: "ProdIDMaster": invalid identifier

    ProdIDMaster appears only in two places, an alias for the case statement, as well as a side of the join statement.  If I simply change the alias name, it has no effect, so I can only assume the question is how I refer to results of the case statement. I also tried to reach the word 'CASE', which returned "1 statement is not valid. "ORA-00904:"The MATTER": invalid identifier.

    Any help or advice would be greatly appreciated.

    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.

    Always say what version of Oracle you are using (for example, 11.2.0.2.0).  Use "SELECT * VERSION of $ v;" to get it.

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

    When you define an alias for column (such as PriodIDMaster) in the SELECT a query clause, you can use the alias in the ORDER byclause in this request, but this is the only place in this same query, where you can use it.  If you want to use anywhere, like in a join condition, then calculate the new column in a subquery; then you can use it anywhere you want in a Super request.

    I see expression BOX (not the same as a declarationBOX) where when ProdIDMaster is defined, but I do not see where it is used.  Are you sure that you posted the code that is causing the problem?

  • 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 does not work

    Dear all,

    We have the database oracle 11g r2 on windows.

    I created under function: -.

    SQL > CREATE OR REPLACE FUNCTION FUNC_TEST (P_TEST VARCHAR2)

    2 RETURN VARCHAR2

    3 AS

    4 V_TEST VARCHAR2 (20);

    5

    6 BEGIN

    7

    8 V_TEST: = P_TEST;

    9

    10

    11 CASES

    12. WHEN V_TEST = UPPER ('%UU%') THEN V_TEST: = REPLACE (V_TEST, 'UU', 'U');

    13. WHEN V_TEST = UPPER ('%OO%') THEN V_TEST: = REPLACE (V_TEST, 'OO', 'O');

    14. WHAT V_TEST = UPPER ('%NN%') THEN V_TEST: = REPLACE(V_TEST,'NN','N');

    15 ELSE V_TEST: = "CHANCHAL";

    END 16 CASES;

    17

    18

    19 V_TEST RETURN;

    20 EXCEPTION SO THAT OTHERS THEN

    21 DBMS_OUTPUT. PUT_LINE (' THERE IS AN ERROR ');

    22 END;

    23.

    The function is created.

    SQL > SHOW ERR

    No errors.

    SQL > SELECT FUNC_TEST('toon') FROM DUAL;

    FUNC_TEST ('TOON')

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

    CHANCHAL

    above function should show 'YOUR' build 'Toon' but its display "CHANCHAL";

    I train hard get the result OK but could not get it.

    Any suggestions would be very helpful.

    Kind regards
    Chanchal wankhade.

    Hello

    Chanchal Wankhade wrote:

    Dear all,

    We have the database oracle 11g r2 on windows.

    I created under function: -.

    SQL > CREATE OR REPLACE FUNCTION FUNC_TEST (P_TEST VARCHAR2)

    2 RETURN VARCHAR2

    3 AS

    4 V_TEST VARCHAR2 (20);

    5

    6 BEGIN

    7

    8 V_TEST: = P_TEST;

    9

    10

    11 CASES

    12. WHEN V_TEST = UPPER ('%UU%') THEN V_TEST: = REPLACE (V_TEST, 'UU', 'U');

    13. WHEN V_TEST = UPPER ('%OO%') THEN V_TEST: = REPLACE (V_TEST, 'OO', 'O');

    14. WHAT V_TEST = UPPER ('%NN%') THEN V_TEST: = REPLACE(V_TEST,'NN','N');

    15 ELSE V_TEST: = "CHANCHAL";

    END 16 CASES;

    17

    18

    19 V_TEST RETURN;

    20 EXCEPTION SO THAT OTHERS THEN

    21 DBMS_OUTPUT. PUT_LINE (' THERE IS AN ERROR ');

    22 END;

    23.

    The function is created.

    SQL > SHOW ERR

    No errors.

    SQL > SELECT FUNC_TEST('toon') FROM DUAL;

    FUNC_TEST ('TOON')

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

    CHANCHAL

    above function should show 'YOUR' build 'Toon' but its display "CHANCHAL";

    I train hard get the result OK but could not get it.

    Any suggestions would be very helpful.

    Kind regards
    Chanchal wankhade.

    I think you meant:

    CASE

    WHEN SUPERIOR (V_TEST) LIKE "% UU" THEN V_TEST: = REPLACE (UPPER (V_TEST), 'UU', 'U');

    WHEN SUPERIOR (V_TEST) LIKE '% OO %' THEN V_TEST: = REPLACE (UPPER (V_TEST), 'OO', 'O');

    ...

    '%' is a wildcard character only on the right-hand side of the LIKE operator. '%' has no particular meaning when you use the = operator.

    You can also use a CASE expression, rather than a CASE statement to this:

    v_test: = UPPER (p_test);

    v_test: = BOX

    WHEN v_test LIKE "% UU" THEN REPLACE (v_test, 'UU', 'U')

    WHEN v_test LIKE "% UU" THEN REPLACE (v_test, 'OO', 'o')

    ...

    END;

    Whatever it is, if v_test contains different models (for example "HOOVER VACUUM"), only 1 of them will be changed.  Is that what you want?

    If this isn't the case, after a few examples of data (CREATE TABLE and INSERT statements) and the results desired from these sample data.  Explain how you get these results from these data.

    See the FAQ forum: https://forums.oracle.com/message/9362002#9362002

  • using a Case statement to display the values of column even as table headers

    I have a database that has been set up kind of tricky, in that some tables are made up of columns that are field names and one link to another table that contain the data for these field... also names there ID displays as many lines instead of as a row of data. I would like to write a query to extract that information and display it as one line for each ID.

    example: table 1

    ID, name, itemresponse and itemcode, Description

    Dee Jones Childrens Hospital 123 SITE location

    123 dee Jones, Date of appointment next NEXTAPPT 22/04/2014

    123 dee Jones was on bedrest NOSHOW reason do not show

    I would like to have the display of data as a record on this same patient, instead of as 3 separate registers (with the Description column in table 1

    Table 2

    ID, name, place, Date of the next meeting, show no reason

    123 dee Jones Childrens Hospital 22/04/2014 was the bed rest

    I have included the code I worked on below my question, for me to get the desired results should I use the Case statement and it's the right way to do it below?

    (SELECT
    PAT.ID "PATIENT ID."
    PAT. FIRST | » '|| PAT. "PATIENT FIRST NAME."
    PAT. "PATIENT DOB," DATE_OF_BIRTH
    DS. Date 'Date of inpatients ',.
    Dai. ITEM_CODE "Item Code",
    Dai. DESCRIPTION "Description."
    Siad. ITEM_RESPONSE,

    Max (CASE WHEN dai. ITEM_CODE IS "SITE" THEN DAI. Decription
    ANOTHER NULL
    END) AS "name of the hospital."

    Max (CASE WHEN DAI.) ITEM_CODE IS 'NEXTAPPT' THEN DAI. ITEM_Description
    ANOTHER NULL
    END) AS 'next Appt Date. "

    Max (CASE WHEN DAI.) ITEM_CODE IS "LASTDATE" THEN DAI. Description
    ANOTHER NULL
    END) AS 'last visit Date.

    Max (CASE WHEN DAI.) ITEM_CODE IS 'NOSHOW' THEN DAI. Description
    ANOTHER NULL
    END) THAT "the reason has not shown".

    Of
    (PAT.ID "IDENTIFYING PATIENT",
    PAT. FIRST | » '|| PAT. "PATIENT FIRST NAME."
    PAT. "PATIENT DOB," DATE_OF_BIRTH
    DS. Date 'Date of inpatients ',.
    Dai. ITEM_CODE "Item Code",
    Dai. DESCRIPTION "Description."
    Siad. ITEM_RESPONSE

    Of

    dm_sessions Ds,
    dm_session_assessments dsa,
    DAS dm_admin_sections,
    dm_session_assessment_items Siad,
    Dai dm_admin_items,
    Po Patient_Orders,
    Inventory inv,
    Patients_Table PAT
    dm_admin_categories CAD

    where dsa.session_Id = ds.session_Id
    and PAT.ID = ds. Patient_ID
    and dsa.excluded_assessment =' no
    and dac.category_code = 'OPEN'

    and dsa.section_id = das.section_id
    and das.category_id = dac.category_id
    and das.section_code = 'northwest'
    and dai.section_id = das.section_id
    and dsai.item_id = dai.item_id
    and dsai.session_assessment_Id = dsa.session_assessment_Id
    and dsai.excluded =' no
    - and Dai.ITEM_ID = Dsai.ITEM_ID
    and Dai.ITEM_CODE IN ('SITE', 'NEXTAPPT', 'LASTDATE', 'NOSHOW')
    and po.patient_ID = ds.patient_ID
    and in. NDC_NO = inv. NDC_NO
    and inv. TDRUG_ABBREV = in. DRUG_ABBREV

    PAT.ID GROUP, PAT. FIRST | » '|| PAT. LAST, PAT. DATE_OF_BIRTH, ds. Date, dai. ITEM_CODE, dai. DESCRIPTION, Siad. ITEM_RESPONSE))

    Hello

    DATE is not a very good name for a column.  Use something like EVENT_DATE, that will not be confused with a keyword from Oracle, instead.

    I don't see something like this column in the CREATE TABLE statement.  He will always be the same value on all lines?

    Here's a way to get the results you requested:

    SELECT id

    first name | ' ' || LastName AS name

    Date of birth

    MAX (CASE WHEN item_code = "NEXTAPPT", THEN item_response END) AS next_appointment,

    MAX (CASE WHEN item_code = "LASTDATE" THEN item_response END) AS last_visit_date,

    MAX (CASE WHEN item_code = "SITE" THEN item_response END) AS location

    FROM mytable1

    GROUP BY id, firstname, lastname, dob

    ;

    Of course, I can't test it very well until you post INSERT statements for the sample data.

    I guess that 2 or more lines of the entry have the same id, then they will necessarily be first name last name, the name and date of birth, too.  No doubt they are standardized in your actual tables.

  • Select with case statement and a formula of the IIR

    Hi, I was looking to get help regarding a statement writing box with a statement select statement all. I tried to reproduce a formula IIf access well that just wanted to check that the query I wrote is correct, any advice would be appreciated.

    [code]

    Select *.

    Of

    (

    Select name, month, duration, volume, time_spent, date1, date2,.

    -case when 'date' > = 'date1' then '1' other '0' end as departure,.

    -case when 'date' < = "date2" then '1' other '0' as end ending

    Of

    (

    Select *.

    of call_1 cd

    inner join call_2 ON cd.name = cl.queue cl

    )

    )

    ;

    [/ code]

    I want to know is where I have my ' select name, etc, I would change that to select * to make it easier instead of typing all the field_names outside, but I don't know how to do and also what follows is 2 IIF formulas from an access database for the start of the final case statements so I just wanted check I wrote it correctly.

    [code]

    departure: IIf ([date] > = [date1], 1, 0)

    [/ code]

    [code]

    ending: IIf ([date] < = [date2], 1, 0)

    [/ code]

    Any advice would be appreciated.

    Hello

    Whenever you have any questions, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the results desired from these data.
    Explain, using specific examples, how you get these results from these data.

    If the output depends on what anyone outside the application itself (for example, when it is run) and then include a few different examples and the results you want of each given the same sample data. For example, "if I run on November 19, 2013, while the results should be... because... but if I run between November 21 and November 27, then the results should be... because...". »
    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002

    318f20b8-a3d0-4FB4-bb0f-73785250b7d4 wrote:

    Hi, I was looking to get help regarding a statement writing box with a statement select statement all. I tried to reproduce a formula IIf access well that just wanted to check that the query I wrote is correct, any advice would be appreciated.

    [code]

    Select *.

    Of

    (

    Select name, month, duration, volume, time_spent, date1, date2,.

    -case when 'date' > = 'date1' then '1' other '0' end as departure,.

    -case when "date."<= 'date2'="" then="" '1'="" else="" '0'="" end="" as="">

    Of

    (

    Select *.

    of call_1 cd

    inner join call_2 ON cd.name = cl.queue cl

    )

    )

    ;

    [/ code]

    I want to know is where I have my ' select name, etc, I would change that to select * to make it easier instead of typing all the field_names outside, but I don't know how to do and also what follows is 2 IIF formulas from an access database for the start of the final case statements so I just wanted check I wrote it correctly.

    [code]

    departure: IIf([date]>=[date1],1,0)

    [/ code]

    [code]

    ending: IIf ([date]<>

    [/ code]

    Any advice would be appreciated.

    DATE is not a function of Oracle; in fact, it's a terrible name for a column or function, because it blends with the DATE data type.

    In Oracle, the function SYSDATE returns the date and time, according to the clock on the database server.  So, SYSDATE can return a value as November 19, 2013 06:33:15.   If you want to midnight the same day (i.e., November 19, 2013 00:00:00) and then use TRUNC (SYSDATE).

    String literals (for example the string which consists 5 characters d, a, t, e, and 1) go inside the single quotes. Numbers and the names of columns only.

    If you want to include all the columns, more some calculated values, in a SELECT clause, then you must use a name table or alias before the *.  (See select_2 below.  To do this, assign the alias j to display online.)

    Maybe you wanted to say something like:

    Select *-it's select_1

    de)

    Select j. *-it's select_2

    , case when SYSDATE > = date1 then 1 or 0 end as departure

    , case when SYSDATE<= date2="" then="" 1="" else="" 0="" end="" as="">

    de)

    Select *-it's select_3

    of call_1 cd

    inner join call_2 ON cd.name = cl.queue cl

    ) j

    )

    ;

    As mentioned in a previous answer, you should be careful about the use of "SELECT *" in production code.

    In select_3, it would be better if you explicitly listed the columns you need.  It can improve efficiency and maintenance.  In this request, queue and the name will be always the same, so you probably don't want to include both of them in the result set, in any case.

    In select_1 and select_2, it is acceptable to use "SELECT *", assuming that select_3 is fixed.

    Moreover, there is no point in using subqueries here.  You can get the same results simply in a single query, without any subqueries.

  • case statement pl

    x: = abs (mod(sys.dbms_crypto.randomnumber,2));

    dbms_output.put_line (x);

    case x

    When 1 then

    insert into employees values (employe_id) (l_employee_id);

    box 0 then null;

    end case;

    I have the code above to insert records in a random way. Infuratingly I put in an action for every possible case, and while I could achieve hhe even in an if statement, some cases are more complex. How can I implement a do nothing instruction in a case statement?

    Thank you

    CASE [expression]

    WHEN condition_1 THEN result_1

    WHEN condition_2 CAN result_2

    ...

    WHEN condition_n THEN result_n

    ON THE OTHER

    Null;  -(Do Nothing)

    END

  • Help in the CASE statement with amount

    Hello Experts

    I amw Group on

    Oracle Database 11 g Enterprise Edition Release 11.1.0.7.0 - 64 bit Production
    With partitioning, OLAP, Data Mining and Real Application Testing options

    I'm unable to solve this Case statement.

    My requirement is that if the value of the attribute is greater than 50, then add 1 to the sum for attribute 4 the sum should be 4


    WITH T AS
    (
    Select MATH_CONV_SCR 70, 68 MATH_PERC, writ_conv_scr 66, 67 writ_per Union double all the
    Select MATH_CONV_SCR 70, MATH_PERC 48, writ_conv_scr 66, 67 writ_per Union double all the
    Select MATH_CONV_SCR 70, 68 MATH_PERC, writ_conv_scr 66, 67 double writ_per
    )
    (
    Select)
    CASE
    WHEN MATH_CONV_SCR > 50 THEN 1
    WHEN MATH_PERC > 50 THEN 1
    WHEN WRIT_CONV_SCR > 50 THEN 1
    WHEN WRIT_PER > 50 THEN 1
    TOTAL END)
    T
    )


    The expected answer is
    4
    3
    4
    for the respective folders


    Please give me the solution

    Thank you

    RB

    Hello

    Rb2000rb65 wrote:
    Hello Experts

    I amw Group on

    Oracle Database 11 g Enterprise Edition Release 11.1.0.7.0 - 64 bit Production...

    Thanks for posting the version and sample data; It is very useful.

    ... Select)
    CASE
    WHEN MATH_CONV_SCR > 50 THEN 1
    WHEN MATH_PERC > 50 THEN 1
    WHEN WRIT_CONV_SCR > 50 THEN 1
    WHEN WRIT_PER > 50 THEN 1
    TOTAL END)
    T

    Don't forget that the WHEN of a CASE expression clauses are mutually exclusive. If none of them are evaluated to TRUE, then all of the following are not even tried.

    )

    The expected answer is
    4
    3
    4
    for the respective folders

    Please give me the solution

    Here's one way:

    SELECT  CASE WHEN math_conv_scr > 50 THEN 1 ELSE 0 END
          + CASE WHEN math_perc      > 50 THEN 1 ELSE 0 END
          +     CASE WHEN writ_conv_scr > 50 THEN 1 ELSE 0 END
          + CASE WHEN writ_per      > 50 THEN 1 ELSE 0 END     AS total
    FROM       t;
    
  • 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
    

Maybe you are looking for