Case statement assistance
I am using a case statement in the following script, so that I can return two seperate columns in my item master query, one for Frozen Cost and one for Actual Cost. I am having trouble with the else close. If the records for frozen are other then frozen, I do not want to have them returned as part of the records and same thing for Actual, if they are not actual I do not want them returned. Any help is appreciated. I am running on 10.2.0.4.0 for the database and for the app layer we are at 11.5.10.2.
SELECT MSI.INVENTORY_ITEM_ID INVENTORY_ITEM_ID
, msi.segment1 ITEM
, MSI.DESCRIPTION DESCRIPTION
, msi.inventory_item_status_code INVENTORY_ITEM_STATUS_CODE
, msi.item_type ITEM_TYPE
, max (mirb.revision) REVISION
, MSI.PRIMARY_UOM_CODE PRIMARY_UOM_CODE
, msi.shippable_item_flag SHIPPABLE_ITEM_FLAG
, NVL (CIC.ORGANIZATION_ID, '0') ORGANIZATION_ID
,CASE when cct.cost_type in 'Frozen' then cic.item_cost else 0 end as FROZEN_COST
,CASE when cct.cost_type in 'ACTUAL' then cic.item_cost else 0 end as ACTUAL
, cct.cost_type COST_TYPE
FROM CST_ITEM_COSTS CIC
, CST_COST_TYPES CCT
, MTL_SYSTEM_ITEMS MSI
, MTL_DEFAULT_CATEGORY_SETS MDC
, MFG_LOOKUPS LU1
, mtl_item_revisions_b mirb
WHERE CCT.COST_TYPE_ID(+) = CIC.COST_TYPE_ID
AND CIC.INVENTORY_ITEM_ID(+) = MSI.INVENTORY_ITEM_ID
AND CIC.ORGANIZATION_ID (+) = MSI.ORGANIZATION_ID
AND MSI.COSTING_ENABLED_FLAG = 'Y'
AND msi.inventory_item_id = mirb.inventory_item_id
AND msi.organization_id = mirb.organization_id
AND LU1.LOOKUP_CODE(+) = MSI.PLANNING_MAKE_BUY_CODE
/* the outside join below, speeds up query, not sure why */
AND LU1.LOOKUP_TYPE(+) = 'MTL_PLANNING_MAKE_BUY'
and cct.cost_type = 'Frozen'
AND msi.organization_id = 4
group by CIC.ROWID, MSI.INVENTORY_ITEM_ID, MSI.SEGMENT1, MSI.DESCRIPTION, MSI.INVENTORY_ITEM_STATUS_CODE,
MSI.PRIMARY_UOM_CODE, MSI.SHIPPABLE_ITEM_FLAG, CIC.ORGANIZATION_ID, MSI.ITEM_TYPE, CIC.ITEM_COST, CIC.MATERIAL_COST, cct.cost_type
Hi,
The CASE expressions look good to me, except that you need parentheses after the keyword IN. (See 'Frozen' below.)
If you're only comparing to one item, as you are here, you can use = instead of IN. (See 'ACTUAL' below.)
, CASE
WHEN cct.cost_type IN ('Frozen' )
THEN cic.item_cost
ELSE 0
END AS frozen_cost
, CASE
WHEN cct.cost_type = 'ACTUAL'
THEN cic.item_cos
ELSE 0
END AS actualRemember to capitalize things inside quotes carefully. The code above is looking for 'Frozen' with only one capital letter, the 'F', but 'ACTUAL' with all capital letters. Is that what you want?
It looks like you're doing a GROUP BY in this query. If so, you'll have to copy these CASE expresssions into the GROUP BY clause, or use them only inside an aggregate function. For example:
, SUM ( CASE
WHEN cct.cost_type IN ('Frozen' )
THEN cic.item_cost
ELSE 0
END
) AS frozen_cost
, SUM ( CASE
WHEN cct.cost_type = 'ACTUAL'
THEN cic.item_cos
ELSE 0
END
) AS actualSince your WHERE clause includes the condition
cct.cost_type = 'Frozen'I would expect the 1st CASE expression to include all rows, and the 2nd to include no rows. If that's the situation, why have CASE expressions at all?
I hope that answers your question.
If not, post a simplified query, involviong only these CASE exoressions and the GROUP BY.
Post CREATE TABLE and INSERT statements for a little sample data (include only the columns and tables in the simplified query). Also post the results you want from that sample data.
Edited by: Frank Kulash on Aug 12, 2010 5:10 PM
Corrected bad nesting: "END" should be before ")".
Similar Messages
-
SQL Expression Field - Combine Declared Variable With Case Statement
Hello All, I have been using Crystal & Business Objects for a few months now and have figured out quite a bit on my own. This is the first real time I have struggled with something and while I could do this as a Formula Field I would like to know how to do this as a SQL Expression. Basically I want to create a SQL Expression that uses a CASE statement but I wanted to make the code a little more efficient and employ a variable to hold a string and then use the variable in the CASE statement. The expression editor accepts the CASE statement OK but I don't know how to declare the variable. Please assist with the syntax?
This is what I have:
CASE
WHEN u201CDatabaseu201D.u201DFieldu201D = u2018Hu2019 THEN u2018Hedgeu2019
WHEN u201CDatabaseu201D.u201DFieldu201D = u2018Pu2019 THEN u2018PVIu2019
ELSE u2018Noneu2019
END
This is what I want:
DECLARE strVar AS VARCHAR(25)
strVar = u201CDatabaseu201D.u201DFieldu201D
CASE
WHEN strVar = u2018Hu2019 THEN u2018Hedgeu2019
WHEN strVar = u2018Pu2019 THEN u2018PVIu2019
ELSE u2018Noneu2019
ENDHi Todd,
Please use the following for loop; your problem will be solved.
Local StringVar str := "";
Local NumberVar strLen := Length ({Database.Field});
Local NumberVar i;
For i := 1 To strLen Do
if {Database.Field} <i> = "H" then str := "Hedge"
else if {Database.Field} <i> = "P" then str := "PVI"
else str := "None"; exit for
str
Let me know once done!
Thank you,
Ashok -
Unable to return a number from CASE statement in plsql
Hello all!
Back in November of last year, I received some assistance from user "jarola" regarding changing the color of a column item in a query based on the resulting value. This discussion yielded;
declare
l_query varchar2(4000) := '';
begin
l_query := '
select i.INVENTORY_ID,
io.ORDER_QUANTITY,
CASE
WHEN i.QUANTITY_AVAILABLE=0
THEN ''<span style="color:red;"> ''|| i.QUANTITY_AVAILABLE ||''</span>''
ELSE to_char(i.QUANTITY_AVAILABLE)
END,
i.QUANTITY_AVAILABLE qty_avail_hold,
i.STRAIN_CODE,
i.STRAIN_NAME,
i.GENOTYPE,
i.AGE,
i.***,
(lower(substr(i.ROOM_NUMBER,1,instr(i.ROOM_NUMBER,''-'',1,1)-1))) ROOM_NUMBER
from SM_INVENTORY i,
SM_INVENTORY_ORDER io
where io.ORDER_ID (+)= :F111_MODIFYING_ORDER
and io.INVENTORY_ID (+)= i.INVENTORY_ID';
return l_query;
end;Problem is, sorting (report attributes for column set to "sort") on column i.QUANTITY_AVAILABLE is broken as even though the data in the table is number(12,0), the CASE statement returns it as alpha. I'm having difficulty getting this CASE statement to return a number as these modifications cause generic compilation errors.
ELSE i.QUANTITY_AVAILABLE
ELSE to_number(i.QUANTITY_AVAILABLE)
Google/sqlplus docs have yielded little. Might anyone offer advice?
Thanks!!!
PaulHello Varad! Thanks for the reply.
Sorry, but I copied some old information in my code example. the CASE statement I'm actually using is;
CASE
WHEN i.QUANTITY_AVAILABLE=0
THEN ''<span style="color:red;"> ''|| i.QUANTITY_AVAILABLE ||''</span>''
ELSE to_char(i.QUANTITY_AVAILABLE)
END AS QUANTITY_AVAILABLESo I'm already returning the result as labeled "QUANTITY_AVAILABLE".
What I did was click on the pencil/paper icon to the left of the alias of my column in the "Column Attributes" pane in "Report Attributes" to get to the "Column Attributes" page and plugged #QUANTITY_AVAILABLE# into the HTML Expression field of the "Column Formatting" pane of the "Column Attributes" page for the "QUANTITY_AVAILABLE" column.
No joy, sort still broken...
Isn't the problem that fundamentally, the CASE statement returns an alpha? I wouldn't expect the sort to ever work until it can return a number (which it appears SQL, used this way, it cannot do).
So, I considered that END AS QUANTITY_AVAILABLE might be the problem as it's named the same as the column, so I changed it (and the HTML Expression entry) to END AS QUANTITY_AVAIL (changed the name).
Sort as alpha still happening.
Any other thoughts? -
Using Field/Column Date Value In Case Statement
I have code that the first part works (the part that evaluates null). However, it appears the second part doesn't work. The error I get is:
Data Value out of range
Can you use the a table column value in a Case statement? What I'm trying to do is: where all mbr06..values are 12/31/9999, then null; if mbr06.. values are equal to or less than today's date, then put the value in mbr02.mbr02_cancel_proc_date..
Note, the table MBR02 does have dates that go back as far as 11/17/1858..could that be an issue?
Thanks for any assistance..
cast((case
when mbr06.mbr06_exp_date = to_date('31-dec-9999') then null
when mbr06.mbr06_exp_date <= sysdate then mbr02.mbr02_cancel_proc_dateThe error is due to the CAST, not to the CASE.
Cause: Value from cast operand is larger than cast target size.Post at least the whole cast ...
Max
http://oracleitalia.wordpress.com -
Hello, just starting to learn PL/SQL. I have a query which runs just fine and it has a case statement. When I take that query and place it into a procedure and compile, it gives me a compile error stating "ENCOUNTERED symbol CASE when expecting... providing a list of symbols.
This procedure is part of a package, so I am only showing the procedure portion...
PROCEDURE QRY_USERS (oRows OUT t_cursor)
IS
BEGIN
OPEN o_Rows FOR
select ou.User_id,
ou.AD_Login_ID,
ou.First_name,
ou.Last_Name,
oct.CODE_DESC,
case when ou.deleted is null then 'N'
else 'Y' end Deleted
from tst_ops_users ou, tst_ops_code_tbl oct
where oct.CODE_TYPE ='USER_TYPE'
and oct.code_name = ou.USER_ROLE_TYPE;
END QRY_USERS;
So is there some different syntax with case statements or is there some other erro causing this?
Thanks a bunch for your assistance!!!Which version of the database are you working on? In 8i the PL/SQL engine did not support the use of CASE in SQL statements (along with a whole bunch of other fresh additions to SQL syntax).
If that is your problem you could try this, which ought to use the SQL engine instead...
PROCEDURE QRY_USERS (oRows OUT t_cursor)
IS
BEGIN
OPEN oRows FOR
'select ou.User_id,
ou.AD_Login_ID,
ou.First_name,
ou.Last_Name,
oct.CODE_DESC,
case when ou.deleted is null then ''N''
else ''Y'' end Deleted
from tst_ops_users ou, tst_ops_code_tbl oct
where oct.CODE_TYPE =''USER_TYPE''
and oct.code_name = ou.USER_ROLE_TYPE';
END QRY_USERS;although you can obviate the need for CASE by using either DECODE or NVL2...
nvl2(ou.deleted , 'Y', 'N') AS deletedCheers, APC -
Use of decode/case statements
I am trying to use a decode or case statement to check for a particular field code of 'SIP' and if that is the value I only want half of the production figure used in the calculations for that field ('SIP').
The following code works well without the items commented out:
SELECT ALL
FINDER_WIS.PRODUCTION_HDR.END_TIME as prod_date,
'SCD' as district,
--decode(FINDER_WIS.FACILITY_FIELD_X.FIELD_CODE,'SIP',FINDER_WIS.PRODUCTION_DATA.VOLUME/2,FINDER_WIS.PRODUCTION_DATA) AS TEST_SIP,
/*case when FINDER_WIS.FACILITY_FIELD_X.FIELD_CODE = 'SIP'
then FINDER_WIS.PRODUCTION_DATA.VOLUME/2
else FINDER_WIS.PRODUCTION_DATA.VOLUME
end as fieldtest,*/
round(SUM(NVL(FINDER_WIS.PRODUCTION_DATA.VOLUME,0))) as total_oil,
ROUND(SUM(NVL(FINDER_WIS.PRODUCTION_DATA.VOLUME,0)) / TO_NUMBER(TO_CHAR(FINDER_WIS.PRODUCTION_HDR.END_TIME,'DD'))) AS BOPD
FROM FINDER_WIS.PRODUCTION_HDR,
FINDER_WIS.PRODUCTION_DATA,
FINDER_WIS.FACILITY,
FINDER_WIS.REPORTING_GROUP,
FINDER_WIS.REPORTING_GROUP_DETAIL,
FINDER_WIS.FACILITY_FIELD_X,
SELECT distinct FINDER_WIS.FACILITY.FACILITY_S
FROM FINDER_WIS.PRODUCTION_HDR,
FINDER_WIS.PRODUCTION_DATA,
FINDER_WIS.FACILITY,
FINDER_WIS.REPORTING_GROUP,
FINDER_WIS.REPORTING_GROUP_DETAIL
WHERE (FINDER_WIS.PRODUCTION_HDR.ACTIVITY_TYPE = 'ALLOCATED'
AND FINDER_WIS.PRODUCTION_HDR.TIME_PERIOD_TYPE = 'MONTH'
AND FINDER_WIS.PRODUCTION_HDR.STATE_TYPE = 'STANDARD'
AND FINDER_WIS.PRODUCTION_HDR.EXISTENCE_TYPE = 'ACTUAL'
AND FINDER_WIS.PRODUCTION_DATA.MATERIAL_TYPE='OIL'
and FINDER_WIS.REPORTING_GROUP.REPORTING_GROUP_TYPE = 'ASSET_TEAM'
AND FINDER_WIS.REPORTING_GROUP.REPORTING_GROUP_ID ='SCD'
and finder_wis.production_HDR.SOURCE = 'NEWWIS'
AND FINDER_WIS.PRODUCTION_HDR.START_TIME BETWEEN :startdate_var AND :enddate_var)
AND ((FINDER_WIS.PRODUCTION_DATA.PRODUCTION_HDR_S=FINDER_WIS.PRODUCTION_HDR.PRODUCTION_HDR_S)
and (FINDER_WIS.PRODUCTION_HDR.FACILITY_S = FINDER_WIS.FACILITY.FACILITY_S)
and (FINDER_WIS.PRODUCTION_HDR.START_TIME between FINDER_WIS.REPORTING_GROUP_DETAIL.start_time and nvl(FINDER_WIS.REPORTING_GROUP_DETAIL.end_time,'01-JAN-2010')
or FINDER_WIS.PRODUCTION_HDR.end_TIME between FINDER_WIS.REPORTING_GROUP_DETAIL.start_time and nvl(FINDER_WIS.REPORTING_GROUP_DETAIL.end_time,'01-JAN-2010'))
AND (FINDER_WIS.REPORTING_GROUP.REPORTING_GROUP_S = FINDER_WIS.REPORTING_GROUP_DETAIL.REPORTING_GROUP_S)
AND (FINDER_WIS.FACILITY.FACILITY_S = FINDER_WIS.REPORTING_GROUP_DETAIL.FACILITY_S)))T
WHERE (FINDER_WIS.PRODUCTION_HDR.ACTIVITY_TYPE = 'ALLOCATED'
AND FINDER_WIS.PRODUCTION_HDR.TIME_PERIOD_TYPE = 'MONTH'
AND FINDER_WIS.PRODUCTION_HDR.STATE_TYPE = 'STANDARD'
AND FINDER_WIS.PRODUCTION_HDR.EXISTENCE_TYPE = 'ACTUAL'
AND FINDER_WIS.PRODUCTION_DATA.MATERIAL_TYPE='OIL'
and FINDER_WIS.REPORTING_GROUP.REPORTING_GROUP_TYPE = 'ASSET_TEAM'
and finder_wis.production_HDR.SOURCE = 'NEWWIS'
AND FINDER_WIS.PRODUCTION_HDR.START_TIME BETWEEN :startdate_var AND :enddate_var)
AND ((FINDER_WIS.FACILITY.FACILITY_S = T.FACILITY_S)
AND (FINDER_WIS.PRODUCTION_DATA.PRODUCTION_HDR_S=FINDER_WIS.PRODUCTION_HDR.PRODUCTION_HDR_S)
AND FINDER_WIS.FACILITY_FIELD_X.UWI = FINDER_WIS.FACILITY.UWI
AND FINDER_WIS.FACILITY_FIELD_X.FIELD_CODE NOT IN ('MW','BRM','PLG','SIP')
and (FINDER_WIS.PRODUCTION_HDR.FACILITY_S = FINDER_WIS.FACILITY.FACILITY_S)
and (FINDER_WIS.PRODUCTION_HDR.START_TIME between FINDER_WIS.REPORTING_GROUP_DETAIL.start_time and nvl(FINDER_WIS.REPORTING_GROUP_DETAIL.end_time,'01-JAN-2010')
or FINDER_WIS.PRODUCTION_HDR.end_TIME between FINDER_WIS.REPORTING_GROUP_DETAIL.start_time and nvl(FINDER_WIS.REPORTING_GROUP_DETAIL.end_time,'01-JAN-2010'))
AND (FINDER_WIS.REPORTING_GROUP.REPORTING_GROUP_S = FINDER_WIS.REPORTING_GROUP_DETAIL.REPORTING_GROUP_S)
AND (FINDER_WIS.FACILITY.FACILITY_S = FINDER_WIS.REPORTING_GROUP_DETAIL.FACILITY_S))
GROUP BY FINDER_WIS.PRODUCTION_HDR.END_TIME
the results look like this but this is without the values for the 'SIP' field:
PROD_DATE DISTRICT TOTAL_OIL BOPD
31/10/2007 SCD 168009 5420
30/11/2007 SCD 167339 5578
31/12/2007 SCD 170277 5493
31/01/2008 SCD 173677 5602
29/02/2008 SCD 168498 5810
31/03/2008 SCD 172689 5571
30/04/2008 SCD 168180 5606
31/05/2008 SCD 165448 5337
30/06/2008 SCD 164631 5488
31/07/2008 SCD 170073 5486
31/08/2008 SCD 166520 5372
30/09/2008 SCD 160321 5344
When I try to add the decode or case statement, I get ORA-00979; not a Group By expression as the error.
Can anyone assist me with this please?
Thanks in advanceHi and welcome to the forum.
Simply put the field names you use in your DECODE also in your GROUP BY and it should work:
simple example:
MHO%xe> with t as (
2 select 1 col1, 1 col2 from dual union all
3 select 1 col1, 1 col2 from dual union all
4 select 2 col1, 2 col2 from dual union all
5 select 3 col1, 3 col2 from dual union all
6 select 4 col1, 4 col2 from dual
7 )
8 select col1
9 , decode(col1, 1, col2*100, col2)
10 , sum(col2)
11 from t
12 group by col1 -->> NO col2 here...
13 order by col1;
, decode(col1, 1, col2*100, col2)
FOUT in regel 9:
.ORA-00979: not a GROUP BY expression
Verstreken: 00:00:05.78
MHO%xe> with t as (
2 select 1 col1, 1 col2 from dual union all
3 select 1 col1, 1 col2 from dual union all
4 select 2 col1, 2 col2 from dual union all
5 select 3 col1, 3 col2 from dual union all
6 select 4 col1, 4 col2 from dual
7 )
8 select col1
9 , decode(col1, 1, col2*100, col2)
10 , sum(col2)
11 from t
12 group by col1, col2
13 order by col1;
COL1 DECODE(COL1,1,COL2*100,COL2) SUM(COL2)
1 100 2
2 2 2
3 3 3
4 4 4 -
How to use REGEXP for case statement
Hello everyone, I'm very new here and am struggling with a using REGEXP in a case statement, OK I am using the REGEXP to find all strings that match a specific format for a particular brand of product, for example serial numbers, and I need to be able to say something like [case when(xx.brandid) = '123' then if xx.serialnumber REGEXP_LIKE(xx.serialnumber,'[A-za-z][A-za-z][A-za-z]\d{5,}[A-za-z]$') then 'TRUE' else 'FALSE' end "TRUE/FALSE".]
Help would be greatly appreciated with this as I feel like I'm going backwards trying to figure this out
Thanks in advance for any assistance.Like this?
case
when xx.brandid = '123' and
regexp_like(xx.serialnumber,'[A-za-z][A-za-z][A-za-z]\d{5,}[A-za-z]$') then
'TRUE'
else
'FALSE'
end -
present case statement:
Thank
CASE WHEN MEPE.CSPI_ID IN ('MD03','MD04','MD05','MD06','MD07','MD08','MD09','MD10') THEN MEPE.CSPI_ID
ELSE 'N/A'
END AS LIS_CD,
Intending case statement.How di I go about it?
Please update the LIS copays to the following amounts:
MD07 & MD10 = $2.65 & $6.60
MD09 = $1.20 & $3.60
MD03, MD04, MD05, MD06, & MD08 are the same for 2015.better explanation of what I want.
INSERT INTO frdmrpt.PremeraHMOPlusConfirmedEnrollment
(BATCH_ID,
MEMBER_NAME,
MEMBER_ADDRESS,
CITY_STATE_ZIP,
LETTER_DT,
EFFECTIVE_DT,
SALUTATION,
PLAN_SELECTED,
MEMBER_ID,
PRODUCT_ID,
MEME_CK,
GRGR_ID,
DOCUMENT_ID,
PREMIUM_AMT,
COPAY_AMT,
PBP,
DEDUCTIBLE,
LIS_CD,----INTERESTED COLUMN
LEP_AMT,
UNCOV_MONTHS,
COMB_PREM,
GROUP_NAME,
CLASS_PLAN_ID,
LETTER_CD,
RUN_DT)
SELECT DISTINCT
@p_iBATCH_ID,
RTRIM(MEMB.MEME_FIRST_NAME) + ' ' + RTRIM(MEMB.MEME_MID_INIT) + ' ' + RTRIM(MEMB.MEME_LAST_NAME) AS MEMBER_NAME,
RTRIM(SBAD.SBAD_ADDR1) + ' ' + RTRIM(SBAD.SBAD_ADDR2) + ' ' + RTRIM(SBAD.SBAD_ADDR3) AS MEMBER_ADDRESS,
CITY_STATE_ZIP = RTRIM(SBAD.SBAD_CITY) + ', ' + RTRIM(SBAD.SBAD_STATE) + ' ' +
RTRIM(CASE WHEN LEN(RTRIM(SBAD.SBAD_ZIP)) = 5 THEN SBAD.SBAD_ZIP
ELSE SUBSTRING(SBAD.SBAD_ZIP,1,5) + '-' + SUBSTRING(SBAD.SBAD_ZIP,6,4)
END),
MEEL.MEEL_INSQ_DT AS LETTER_DT,
MEEL.MEEL_EFF_DT AS EFFECTIVE_DT,
CASE MEMB.MEME_SEX
WHEN 'M' THEN 'MR.'
WHEN 'F' THEN 'MS.'
ELSE ''
END as SALUTATION,
'Premera Blue Cross Medicare Advantage Plus (HMO)' AS PLAN_SELECTED,
RTRIM(SBSB.SBSB_ID) + '-' + RIGHT('0' + RTRIM(CONVERT(CHAR(2),MEMB.MEME_SFX)), 2) AS MEMBER_ID,
MEPE.PDPD_ID AS PRODUCT_ID,
MEEL.MEME_CK,
GRGR.GRGR_ID,
'Premera Blue Cross Medicare Advantage Plus (HMO)' AS DOCUMENT_ID,
SUM(PDRA_PREM_1) AS PREMIUM_AMT,
MCSA.CoPay AS LIS_CO_PAY,
MEMD.MEMD_MCTR_PBP AS PBP,
MCSA.DEDUCTIBLE AS LIS_DEDCTBL,
CASE WHEN MEPE.CSPI_ID IN ('MD03','MD04','MD05','MD06','MD07','MD08','MD09','MD10') THEN MEPE.CSPI_ID
ELSE 'N/A'
END AS LIS_CD, ---INTERESTED CASE STAEMENT TO B MODIFIED WITH d below REQUIREMENT
--(MD07 & MD10 = $2.65 & $6.60 (Are they respect values i.e.( MD07 =$2.65 & md10= $ 6.50)
--MD09 = $1.20 & $3.60----has the 2 values
-- MD03, MD04, MD05, MD06, & MD08 are the same for 2015.)
As you can see is a n insert into statement.
Assistance needed ASAP. Thanks. -
Help With A Case Statement With Multiple Variables
I apologize if this is the incorrect Forum for this type of question, but it was the closest one that I could find. I'm pretty new with SQL and am stuck on this issue. I have roughly 26 dates that I need to compare to one another. Each date is tied to a step code. I also have a Stop value that is tied directly to the "max date" of the step codes. So, I need to compare 30 dates against one another to 1st - ID the max date; 2nd - ID if the Stop value is correct; 3rd - if the stop value is incorrect, identify what the correct value would be.
At first, this seemed like it wouldn't be that hard. I wrote a query that found the max date for each step code. Then I realized that multiple step codes could have the same date. So, I tried using this case statement, but I did not get the expected results. Is there a more efficient way of getting what I need? This code seems like it's not necessary and probably the source of my issue.
CASE
WHEN FS25.ACTUAL_COMPLETION_DATE > FS.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS1.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS2.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS3.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS4.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS5.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS6.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS7.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS8.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS9.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS10.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS11.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS12.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS13.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS14.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS15.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS16.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS17.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS18.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS19.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS20.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS21.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS22.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS23.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS24.ACTUAL_COMPLETION_DATE AND L.FORECLOSURE_STOP_CODE <= '8' THEN '9'
ELSE 'UH OH'
END AS "CHANGE FC STOP TO"
Any assistance is appreciated!I think Igor pointed out a working solution before.
Applying it at your examples (you missed the operator after STOP_CODE, I assume it =):
CASE
WHEN FS25 = GREATEST(FS25, FS24, FS23) AND STOP_CODE = '9' THEN '9'
ELSE 'UH OH'
END AS 'CHANGE STOP CODE TO'
{code}
Be careful at the second example. You are checking:
{code:sql}
FS25 > FS24 OR FS25 IS NOT NULL AND FS24 IS NULL AND FS25 > FS23
OR
FS25 IS NOT NULL AND FS23 IS NULL AND STOP_CODE = '9'
{code}
Remember that AND has higher priority among operators than OR so if FS25 is greater than FS24 and FS23 the condition will be true even if STOP_CODE is not equal 9.
Regards.
Al -
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,
JYou 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 :)
AlmirHi 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 -
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?
Thxziekc 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 -
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.
DevFixed 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.
Maybe you are looking for
-
HT5622 How do I have two apple ID's or two itune accounts on one computer?
My wife and I want to keep our iTunes separate so we have our own pics and music on our own phones. How do we sync them without combining them?
-
How do I get a virtual lab to work on this computer. The technician said the CD is not compatible. Maybe corrupt. He seemed he did not know what he was talking about. I asked him was it any software I could downloa
-
My iPod touch 4g is having a huge lag.
Well, I had an iPod touch 4g before, but then it cracked and I got a new one. The new one is a 4th generation,16gb. I have this huge lag that won't go away. Sometimes it does go away. How I make it go away is let it charge for at least an hour, and t
-
I downloaded the latest iTunes and now it doesn't recognize my iPhone
I downloaded the latest iTunes update and now it doesn't recognize my iPhone
-
Downloading HD movie Extremely Slow or many times make my computer froze
hi i am so afraid to buy HD movie from itunes store. i have very fast internet mac pro great computer. i even have speedit video accelerator . every time i purchase HD movie, my computer froze 95% and sometimes takes 24 hours to download.. it happen