Problem using Decode/Case
I have one table which stores candidates' response. The structure is like this
(Seatno, Questionnumber, Answer).
And I have another table Master as (Questionnumber, Answer)
Now I want to calculate marks by comparing Candidate's response with Master.
I tried to use decode and case. But was not successful.
My query with decode was ...
select c1.candidatesrno, sum(decode(c1.answer,(c1.answer=m1.answer),1,0))
from candidate c1,master q1
where c1.question=m1.question
group by c1.candidatesrno
And query with CASE was...
select c1.candidatesrno,
case when (c1.answer=m1.answer) then 1 else 0
end resultset
from candidate c1,master m1
where c1.questionnumber=m1.questionnumber
group by c1.candidatesrno
Can anybody help ?
I want to compare candidate's response with master
table.
Now for each question of Candidate, if its answer
matches with answer in Master table I have to give 1
marks...if it does not match I have to give 0
marks...like this there are two-three conditions...
Something like this ?
test@ORA10G>
test@ORA10G> with master as (
2 select 'Q1' as question_num, 'A1' as answer_num from dual union all
3 select 'Q2', 'A2' from dual union all
4 select 'Q3', 'A3' from dual),
5 candidate_response as (
6 select 'S1' as seat_num, 'Q1' as question_num, 'A1' as answer_num from dual union all
7 select 'S1', 'Q2', 'A2' from dual union all
8 select 'S1', 'Q3', 'A3' from dual union all
9 select 'S2', 'Q1', 'A5' from dual union all
10 select 'S2', 'Q2', 'A6' from dual union all
11 select 'S2', 'Q3', 'A3' from dual)
12 --
13 select
14 cr.seat_num,
15 cr.question_num,
16 cr.answer_num,
17 case when m.question_num is null and m.answer_num is null then '0 point'
18 else '1 point'
19 end as points
20 from candidate_response cr, master m
21 where cr.question_num = m.question_num(+)
22 and cr.answer_num = m.answer_num(+)
23 order by cr.seat_num, cr.question_num, cr.answer_num;
SE QU AN POINTS
S1 Q1 A1 1 point
S1 Q2 A2 1 point
S1 Q3 A3 1 point
S2 Q1 A5 0 point
S2 Q2 A6 0 point
S2 Q3 A3 1 point
6 rows selected.
test@ORA10G>
test@ORA10G> with master as (
2 select 'Q1' as question_num, 'A1' as answer_num from dual union all
3 select 'Q2', 'A2' from dual union all
4 select 'Q3', 'A3' from dual),
5 candidate_response as (
6 select 'S1' as seat_num, 'Q1' as question_num, 'A1' as answer_num from dual union all
7 select 'S1', 'Q2', 'A2' from dual union all
8 select 'S1', 'Q3', 'A3' from dual union all
9 select 'S2', 'Q1', 'A5' from dual union all
10 select 'S2', 'Q2', 'A6' from dual union all
11 select 'S2', 'Q3', 'A3' from dual)
12 --
13 select cr.seat_num, count(*) as score
14 from candidate_response cr, master m
15 where cr.question_num = m.question_num
16 and cr.answer_num = m.answer_num
17 group by cr.seat_num
18 order by cr.seat_num;
SE SCORE
S1 3
S2 1
test@ORA10G>
test@ORA10G>pratz
Similar Messages
-
Problem using DECODE() function with a Query of Queries
I
posted
on my blog about an issue I was having trying to use the PL/SQL
DECODE() function with a Coldfusion Query of Queries. This function
works fine when you query a database for information. However, when
you query another query, it seems that CF doesn't recognize it. I
got errors stating that it found a left parenthesis where it
expected a FROM key word. Here is a simplified version of what I am
trying to do:
quote:
<!--- Simulated query; similar to what I was calling from
my database --->
<cfscript>
qOriginal = queryNew("Name,Email,CountryCode",
"VarChar,VarChar,VarChar");
newRow = queryAddRow(qOriginal, 5);
querySetCell(qOriginal, "Name", "Joe", 1);
querySetCell(qOriginal, "Email", "[email protected]", 1);
querySetCell(qOriginal, "CountryCode", "AMER", 1);
querySetCell(qOriginal, "Name", "Sally", 2);
querySetCell(qOriginal, "Email", "[email protected]", 2);
querySetCell(qOriginal, "CountryCode", "AMER", 2);
querySetCell(qOriginal, "Name", "Bob", 3);
querySetCell(qOriginal, "Email", "[email protected]", 3);
querySetCell(qOriginal, "CountryCode", "ASIA", 3);
querySetCell(qOriginal, "Name", "Mary", 4);
querySetCell(qOriginal, "Email", "[email protected]", 4);
querySetCell(qOriginal, "CountryCode", "EURO", 4);
querySetCell(qOriginal, "Name", "John", 5);
querySetCell(qOriginal, "Email", "[email protected]", 5);
querySetCell(qOriginal, "CountryCode", "EURO", 5);
</cfscript>
<cfquery name="qCountries" dbtype="query">
SELECT DISTINCT(CountryCode) AS CountryCode,
DECODE(states, "AMER", "North America & Canada",
"EURO", "Europe & Africa", "ASIA", "Japan &
Asia","") CountryName
FROM qOriginal
ORDER BY CountryCode
</cfquery>
<cfdump var="#qCountries#">
<!--- ========== END OF CODE ========== --->
So running this returned the following error:
Query Of Queries syntax error.
Encountered "(. Incorrect Select Statement, Expecting a
'FROM', but encountered '(' instead, A select statement should have
a 'FROM' construct.
Does anybody know why this doesn't work? Is it just not
supported? Please note that I have also tried to use the CASE()
function instead of DECODE() and that resulted in basically the
same error. For now I an looping over my distinct query with a
switch statement and manually loading a new query with the data how
I want it. But it would be a lot cleaner and less code to have the
DECODE() to work. Thx!DECODE() is an Oracle function, not generic SQL. Q-of-Q is a
very limited subset of SQL and lacks many functions and clauses
available in standard SQL, especially what you may be used to using
in your particular RDBMS.
See
Query
of Queries user guide
Phil -
I'm trying to decode the following, but I seem to be having a syntax issue:
decode
p.balls,p.strikes,
1,0 '1-0 Count',
2,0 '2-0 Count',
3,0 '3-0 Count',
0,1 '0-1 Count',
0,2 '0-2 Count',
1,1 '1-1 Count',
2,1 '2-1 Count',
3,1 '3-1 Count',
0,2 '0-2 Count',
1,2 '1-2 Count',
2,2 '2-2 Count',
) as Count,what I'm trying to have happen is anytime the balls column has a '1' and the strike column has a '0' to decode to '1-0 Count' etc...Decode compares the value of the first paramter with the values of the even parameters and returns the following odd parameters value when the first match is found. If the last parameter is an even numbered parameter it is taken as the default when no other matches are found.
So your code compares p.balls to p.strikes and if they match returns a 1 then it runs into a syntax error since you don't have any commas after the forth column.
If you want to perform a multicolumn compare in the manner you are showing you will need to use a CASE statement instead:
case when (p.balls, p.strikes) in ( (1, 0) ) then '1-0 Count'
when (p.balls, p.strikes) in ( (2, 0) ) then '2-0 Count'
when (p.balls, p.strikes) in ( (3, 0) ) then '3-0 Count'
when (p.balls, p.strikes) in ( (0, 1) ) then '0-1 Count'
when (p.balls, p.strikes) in ( (0, 2) ) then '0-2 Count'
end as "COUNT" -
Hi All,
The Transformation Guide of OWB mentions that DECODE, CASE can be used. But I am unable to find either of these functions in the Expression Builder. Can you please tell me wher should I search for these functions, or if you could tell me if there is any other way of implementing IF THEN ELSE logic.
-ArnabHello Arnab,
Just type your CASE ... END expression directly in the expr. builder window.
Always press the Validate button for syntax verification when you have changed your expression.
Regards, Hans Henrik -
What I'm trying to do is: For each employee list the value of all pending orders, the value of all completed orders and the value of all orders issued by the employee.
Using these tables:
Orders
Name
ORD_ID
SUPPLIER_ID
ISSUING_EMP_ID
ORDER_DATE
ORDER_STATUS
Sup_Pro
Name
S_ID
P_ID
CATALOG_NO
PRICE
Here is my query
select o.issuing_emp_id,
sum(decode(o.order_status, 'P', p.price, 0)) pending
sum(decode(o.order_status, 'C',p.price,0)) completed,
sum(decode(o.order_status, 'P + C', p.price,0)) all_orders
from
orders o,
sup_pro p
where o.supplier_id = p.s_id
group by o.issuing_emp_id;
The problem is with the line sum(decode(o.order_status, 'P + C', p.price,0)) all_orders. All it does is display 0, and doesn't actually add anything up. What am I doing wrong?
Edited by: user4555857 on 30-Nov-2009 13:59select o.issuing_emp_id,
sum(decode(o.order_status, 'P', p.price, 0)) pending
sum(decode(o.order_status, 'C',p.price,0)) completed,
sum(decode(o.order_status, 'P', p.price,'C',p.price,0)) all_orders
from
orders o,
sup_pro p
where o.supplier_id = p.s_id
group by o.issuing_emp_id;max -
Query to find out sum by using decode or case
Hi, I have data like below and required an output as given below, could you please help me to get the same by using decode / case .....Thanks for your help
INSNAME INSID STATUS
AAA 1000 Scheduled
AAA 1000 Processing
BBB 1001 Inspector
BBB 1001 Scheduled
BBB 1001 Processing
BBB 1001 Inspector
CCC 1002 Scheduled
CCC 1002 Processing
CCC 1002 Inspector
CCC 1002 Scheduled
CCC 1002 Processing
CCC 1002 Inspector
Required Output...
INSNAME INSID sum_of_scheduled sum_of_Processing sum_of_Inspector
AAA 1000 1 1 0
BBB 1001 1 1 2
CCC 1002 2 2 2And if you want it with CASE statement:
WITH test_table AS
SELECT 'AAA' insname, 1000 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'AAA' insname, 1000 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Inspector' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Inspector' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Inspector' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Inspector' status FROM DUAL
SELECT insname
,insid
,SUM(CASE WHEN status = 'Scheduled'
THEN 1
ELSE 0
END
) sum_of_scheduled
,SUM(CASE WHEN status = 'Processing'
THEN 1
ELSE 0
END
) sum_of_processing
,SUM(CASE WHEN status = 'Inspector'
THEN 1
ELSE 0
END
) sum_of_inspector
FROM test_table
GROUP BY insname
,insid;
Regards
Arun -
Hello All,
Can I use decode/case function within a decode function?
{code}
SELECT TO_CHAR (
EDMS_STRAGG_WC (
DISTINCT DECODE (
eqs.attrib_code,
'PRODUCT_AUTHORIZATION', 'Authorization: '
|| CASE eqs.qual_value
WHEN 'LIST'
THEN
(SELECT lookup_desc
FROM edmsadm.edms_lookup
WHERE lookup_type =
'PARTNER_AUTHORIZATION'
AND lookup_code =
eqls.list_value)
ELSE
(SELECT lookup_desc
FROM edmsadm.edms_lookup
WHERE lookup_type =
'PARTNER_AUTHORIZATION'
AND lookup_code =
eqs.qual_value)
END,
'PRODUCT_CERTIFICATION', 'Certification: '
|| CASE eqs.qual_value
WHEN 'LIST'
THEN
eqls.list_value
ELSE
eqs.qual_value
END,
'PRODUCT_SPECIALIZATION', 'Specialization: '
|| (SELECT lookup_desc
FROM edmsadm.edms_lookup
WHERE lookup_type =
'PARTNER_SPECIALIZATION'
AND lookup_code =
CASE eqs.qual_value
WHEN 'LIST'
THEN
eqls.list_value
ELSE
eqs.qual_value
END))))
program_value
FROM edms_qual_stg eqs, edms_qual_list_stg eqls
WHERE 1 = 1
AND eqs.qual_id = eqls.qual_id(+)
AND eqs.req_id = 192647
AND eqs.disc_line_id = 598631
AND eqs.attrib_code IN
('PRODUCT_CERTIFICATION',
'PRODUCT_AUTHORIZATION',
'PRODUCT_SPECIALIZATION');
{code}
Edms_qual_stg:
AND_OR_CONDITION
ATTRIBUTE_SOURCE
ATTRIB_CODE
CREATED_BY
CREATED_DT
DISC_LINE_ID
END_DT
EXCLUDE_INCLUDE
GROUP_AND_OR_CONDITION
GROUP_CODE
MAX_VALUE
MIN_VALUE
MODIFIED_BY
MODIFIED_DT
QUAL_APPL_PRECEDENCE
QUAL_ID
QUAL_OPERATOR
QUAL_TYPE
QUAL_VALUE
REQ_ID
START_DT
Edms_qual_list_stg:
ATTRIBUTE_SOURCE
ATTRIB_CODE
CREATED_BY
CREATED_DT
END_DT
INCLUDE_AFFILIATES
INCLUDE_EXCLUDE
LIST_VALUE
MODIFIED_BY
MODIFIED_DT
PRIMARY_PARTY
QUAL_ID
QUAL_LIST_ID
REFERENCE_ID
START_DT
Edms_lookup:
CREATED_BY
CREATED_DT
DISPLAY_SEQ
EDMS_LOOKUP_ID
END_DATE
LOOKUP_CODE
LOOKUP_DESC
LOOKUP_REFERENCE
LOOKUP_TYPE
MODIFIED_BY
MODIFIED_DT
START_DATE
SELECT eqs.qual_id, eqs.disc_line_id, eqs.req_id, eqs.attrib_code, eqs.qual_value, eqls.list_value
FROM edms_qual_stg eqs, edms_qual_list_stg eqls
WHERE 1 = 1
AND eqs.qual_id = eqls.qual_id(+)
AND disc_line_id = 598631
AND req_id = 192647
AND eqs.attrib_code IN
('PRODUCT_CERTIFICATION',
'PRODUCT_AUTHORIZATION',
'PRODUCT_SPECIALIZATION');
7509575 598631 192647 PRODUCT_CERTIFICATION LIST GOLD
7509575 598631 192647 PRODUCT_CERTIFICATION LIST SILVER
7509576 598631 192647 PRODUCT_AUTHORIZATION LIST ATP - JOULEX PROVIDER ESCO
7509576 598631 192647 PRODUCT_AUTHORIZATION LIST ATP - JOULEX PROVIDER IDENTIFY
7509577 598631 192647 PRODUCT_SPECIALIZATION LIST ADVANCED SECURITY
7509577 598631 192647 PRODUCT_SPECIALIZATION LIST EXPRESS FOUNDATION
Required Output:
Certification: GOLD, SILVER, Authorization: ATP - JOULEX PROVIDER ESCO, ATP - JOULEX PROVIDER IDENTIFY, SPECIALIZATION: ADVANCED SECURITY, EXPRESS FOUNDATION.
Thx
Shank.Hi,
Sure; a DECODE or CASE expression that returns a NUMBER can be used any place a NUMBER is expected, including within another DECODE or CASE expression. A DECODE or CASE expression that returns a DATE can be used any place a DATE is expected, including within another DECODE or CASE expression. A DECODE or CASE expression that returns a VARCHAR2 can be used almost any place a VARCHAR2 is expected. (There a couple of special situations where a string literal is absolutely required.)
There are not many situations where you really need to do that, though. It's usually simpler and more efficient to use a single CASE expression; nesting is rarely needed.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Simplify the problem as much as possible, so that it contains only enough to show the part you don't already know how to do.
If you really need a user-defined function to show the problem, then include a CREATE FUNCTION statement, and explain what the function does. Again, simplify: if the function isn't what you don't understand, post a problem that doesn't use the function.
Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
See the forum FAQ: https://forums.oracle.com/message/9362002 -
Using 'DECODE' instead of 'CASE' in select
Hi,
I am using CASE statements in my Select clause using 9i.
SELECT
CASE
WHEN S.cmpy_num = S.cpty_borg_num THEN
''OURS''
ELSE
''THEIRS''
END AS SDIOwner,
S.active_flag,
from SDI sI wud like to try this in 8I but 8i doesnt support CASE statements
in Select clauses.
It was recommended to use Decode in place of CASE in 8I
How can i write the decode statement for the above CASE statement in 9IGOT IT RUNNIG.THANKS A LOT JAMES...
I AM TESTING SYSTEMS ON 9I AND 8I... -
More effecient way of using Decode and CASE
Is it possible to use both case and decode together? I feel I have too many decode and I am not sure how to write this in a more efficient way.
I would really appreciate any help.
Thank you so much in advance!!!
SELECT Person_ID,Work_ID,
CASE WHEN NBR = 1 THEN
MIN(DECODE(NBR, 1, field1)) aug_one_field1;
MIN(DECODE(NBR, 1, field2)) aug_one_field2;
MIN(DECODE(NBR, 1, field3)) aug_one_field3;
MIN(DECODE(NBR, 1, field4)) aug_one_field4;
MIN(DECODE(NBR, 1, field5)) aug_one_field5;
MIN(DECODE(NBR, 1, field6)) aug_one_field6;
CASE WHEN NBR = 2 THEN
MIN(DECODE(NBR, 2, field1)) aug_two_field1;
MIN(DECODE(NBR, 2, field2)) aug_two_field2;
MIN(DECODE(NBR, 2, field3)) aug_two_field3;
MIN(DECODE(NBR, 2, field4)) aug_two_field4;
MIN(DECODE(NBR, 2, field5)) aug_two_field5;
MIN(DECODE(NBR, 2, field6)) aug_two_field6;
CASE WHEN NBR = 3 THEN
MIN(DECODE(NBR, 3, field1)) aug_three_field1;
MIN(DECODE(NBR, 3, field2)) aug_three_field2;
MIN(DECODE(NBR, 3, field3)) aug_three_field3;
MIN(DECODE(NBR, 3, field4)) aug_three_field4;
MIN(DECODE(NBR, 3, field5)) aug_three_field5;
MIN(DECODE(NBR, 3, field6)) aug_three_field6;
CASE WHEN NBR = 4 THEN
MIN(DECODE(NBR, 4, field1)) aug_four_field1;
MIN(DECODE(NBR, 4, field2)) aug_four_field2;
MIN(DECODE(NBR, 4, field3)) aug_four_field3;
MIN(DECODE(NBR, 4, field4)) aug_four_field4;
MIN(DECODE(NBR, 4, field5)) aug_four_field5;
MIN(DECODE(NBR, 4, field6)) aug_four_field6;
END
FROM (SELECT Person_ID,Work_ID, NBR
fiel1, field2, field3,field4,field5, field6,
FROM field_rep
GROUP BY Person_ID,Work_IDThanks alot John and Frank.
John to answer your question, I just felt the 24 or more decode will slow down the systems performance, hence I was trying to find a better way to do it.
Frank
This is a sample data but I want it pivoted hence the reason for using the decode. I have oracle 10g. These are sample data
Person_id work_id NBR field1 field2 field3 field4 field5 field6
1 ao334 1 1/2/2009 1/9/2010 block_A HH 55667 1
1 ao334 2 5/2/2011 9/9/2013 block_Z HL 11111 3
1 ao334 2 1/2/2009 1/9/2010 block_A HH 22222 1
1 ao334 4 1/2/2009 1/9/2010 block_A HH zzzzz 7
1 z5521 1 10/5/2006 12/31/2012 block_C SS 33322 1
1 z5521 2 1/2/2009 1/9/2010 block_C SS 21550 1
1 z5521 3 1/2/2009 1/9/2010 block_R SS 10000 1
1 z5521 4 1/2/2009 1/9/2010 block_D SS 99100 5
1 z5521 5 1/2/2009 1/9/2010 block_P SS 88860 1
1 z5521 6 1/2/2009 1/9/2010 block_G SS 99660 8
1 ob114 1 1/2/2009 1/9/2010 block_A HH 52333 1
1 ob114 2 1/2/2009 1/9/2010 block_A HH 88888 1Output will look like this
Person_id work_id aug_one_field1 aug_one_field2 aug_one_field3 aug_one_field4 aug_one_field5 aug_one_field6 aug_two_field1 aug_two_field2 aug_two_field3 aug_two_field4 aug_two_field5 aug_two_field6
1 ao334 1/2/2009 1/9/2010 block_A HH 55667 1 5/2/2011 9/9/2013 block_Z HL 11111 3 -
I have an iphone 4s - whenever I take a picture using the flash, all I get is a white "blob". Pictures without the flash are fine. I thought the flash bouncing off the case might be the problem, but when I used different cases I still had the same problem. Any suggestions?
I have an iphone 4s - whenever I take a picture using the flash, all I get is a white "blob". Pictures without the flash are fine. I thought the flash bouncing off the case might be the problem, but when I used different cases I still had the same problem. Any suggestions?
-
How to use index, when query has decode/case
Hi,
I have the following query
i have a index on party_id,party_type_Code in the zx_party_tax_profile table But this index is not used as Iam using a decode on the columns of the zx_party_tax_profile table,
Is there any way i can rewrite the query so that it uses index
sELECT /*+ INDEX(ZX_PARTY_TAX_PROFILE_U2) */ party_tax_profile_id FROM (SELECT
ThirdPartyTaxProfileEO.SUPPLIER_FLAG,
ThirdPartyTaxProfileEO.CUSTOMER_FLAG,
ThirdPartyTaxProfileEO.SITE_FLAG,
ThirdPartyTaxProfileEO.PARTY_TAX_PROFILE_ID,
ThirdPartyTaxProfileEO.PARTY_ID,
ThirdPartyTaxProfileEO.REP_REGISTRATION_NUMBER,
ThirdPartyTaxProfileEO.OBJECT_VERSION_NUMBER,
ThirdPartyTaxProfileEO.REGISTRATION_TYPE_CODE,
ThirdPartyTaxProfileEO.COUNTRY_CODE,
ThirdPartyTaxProfileEO.MERGED_TO_PTP_ID,
ThirdPartyTaxProfileEO.MERGED_STATUS_CODE,
ThirdPartyTaxProfileEO.PROGRAM_APP_NAME,
ThirdPartyTaxProfileEO.PROGRAM_NAME,
PartyPEO.PARTY_NAME,
PartyPEO.PARTY_ID AS PARTY_ID1,
PartyPEO.PARTY_NUMBER,
decode(ThirdPartyTaxProfileEO.CUSTOMER_FLAG,
'Y',decode(ThirdPartyTaxProfileEO.SUPPLIER_FLAG,
'Y', 'SC',
'C'),
decode(ThirdPartyTaxProfileEO.SUPPLIER_FLAG,
'Y', 'S',
NULL)
) AS PARTY_USAGE,
ThirdPartyTaxProfileEO.REP_REGISTRATION_NUMBER AS TAX_REG_NUMBER,
LkupPartyUsage.MEANING AS PARTY_USAGE_DESC,
PartyPEO.PARTY_NAME AS PARTY_FULL_NAME,
PartyPEO.ADDRESS1||','||
PartyPEO.ADDRESS2||','||
PartyPEO.ADDRESS3||','||
PartyPEO.CITY||','||
PartyPEO.postal_code||','||
PartyPEO.COUNTRY AS ADDRESS,
PartyPEO.COUNTRY AS COUNTRY_CODE_TCA,
TerritoryPEO.TERRITORY_SHORT_NAME AS COUNTRY_NAME,
PartyPEO.JGZZ_FISCAL_CODE AS TAX_PAYER_ID,
PartyPEO.DUNS_NUMBER_C AS DUNS_NUMBER,
PartyPEO.Party_Number as Party_Num_Calc,
null as REGISTRATION_TYPE_NAME,
null as ROUNDING_LEVEL_NAME,
null as ROUNDING_RULE_NAME,
null as COUNTRY_NAME_PTP,
'ZX_PARTY_TAX_PROFILE' as TAX_REPORTING_ENTITY_CODE
FROM ZX_PARTY_TAX_PROFILE ThirdPartyTaxProfileEO,
HZ_PARTIES PartyPEO,
FND_LOOKUP_VALUES_VL LkupPartyUsage,
FND_TERRITORIES_VL TerritoryPEO
WHERE ThirdPartyTaxProfileEO.PARTY_ID = PartyPEO.PARTY_ID AND
LkupPartyUsage.LOOKUP_CODE = decode(ThirdPartyTaxProfileEO.CUSTOMER_FLAG,
'Y',decode(ThirdPartyTaxProfileEO.SUPPLIER_FLAG,
'Y', 'SC',
'C'),
decode(ThirdPartyTaxProfileEO.SUPPLIER_FLAG,
'Y', 'S',
NULL)
) AND
PartyPEO.COUNTRY = TerritoryPEO.Territory_Code (+) AND
LkupPartyUsage.LOOKUP_TYPE = 'ZX_TP_PARTY_USAGE'
ORDER BY UPPER(PARTY_FULL_NAME)) QRSLT WHERE UPPER(PARTY_NAME) IS NOT
NULL
Any help will be appreciatedYou can rewrite your where clause to not use decode or case statements e.g. this:
AND LkupPartyUsage.LOOKUP_CODE = DECODE( ThirdPartyTaxProfileEO.CUSTOMER_FLAG
, 'Y', DECODE( ThirdPartyTaxProfileEO.SUPPLIER_FLAG, 'Y', 'SC', 'C' )
, DECODE( ThirdPartyTaxProfileEO.SUPPLIER_FLAG, 'Y', 'S', NULL ) )
)can be rewritten to this:
and (
( ThirdPartyTaxProfileEO.CUSTOMER_FLAG = 'Y' AND
((ThirdPartyTaxProfileEO.SUPPLIER_FLAG = 'Y' AND LkupPartyUsage.LOOKUP_CODE = 'SC') or
ThirdPartyTaxProfileEO.SUPPLIER_FLAG != 'Y' AND LkupPartyUsage.LOOKUP_CODE = 'C')) or
( ThirdPartyTaxProfileEO.CUSTOMER_FLAG != 'Y' AND
((ThirdPartyTaxProfileEO.SUPPLIER_FLAG = 'Y' AND LkupPartyUsage.LOOKUP_CODE = 'S') or
ThirdPartyTaxProfileEO.SUPPLIER_FLAG != 'Y' AND LkupPartyUsage.LOOKUP_CODE is null))
)It's not as sussinct, but it avoids the use of functions that could be preventing the optimiser from using an index. -
I'm trying to use a CASE statement in Ora 8.1.7 (with compatible
set to 8.1.0) and am finding that I cannot compile the
procedure.
I'm using the CASE statement in a call to a procedure...
insert_addr (p_id_number => v_id_number,
p_addr_type => v_addr_type,
p_addr_status => v_addr_status,
p_addr_pref_ind => v_pref_addr_ind,
p_date_modified =>
(CASE
WHEN cmf_rec.hadoc = cnv_constants.nov17_1958
THEN cmf_rec.doe
ELSE cmf_rec.hadoc
END)
I'm getting "PLS-00103 Encountered the symbol "CASE" when
expecting one of the following..." error msg when compiling.
What have I done wrong? Syntax error? Should I change the
compatible parameter?
Thanks.You can actually use the CASE statement in Pl/Sql, though not like in SqlPlus.
You have to use dynamic Sql.
Example
create or replace procedure test_case1 is
v_sql varchar2(4000);
begin
v_sql := 'update case_test set qual_rule = '||
'(case '||
' when dob < ''01-jan-1902'' then ''100+'' '||
' when dob between ''01-jan-1902'' and ''31-dec-1912'' then ''90-99'' '||
' when dob between ''01-jan-1912'' and ''31-jan-1932'' then ''70-89'' '||
' when dob between ''01-jan-1932'' and ''31-jan-1952'' then ''50-69'' '||
' when dob between ''01-jan-1952'' and ''31-jan-1972'' then ''30-49'' '||
' when dob between ''01-jan-1972'' and ''31-jan-1984'' then ''< 30'' '||
' else ''under age'' '||
' end ) ';
execute immediate v_sql;
commit;
end test_case1; -
Hi
I am trying to achieve the following in my report:
If an employee has a surname of . (dot) or a first name of . (dot), the report should not display a dot. An employee's name is made up of surname, first name and middle name which should all be concatenated together. To try to achieve this, I have the following statement in my report:
decode(e.Surname, '.', ( LTRIM(RTRIM((INITCAP(e.FIRST_NAME)))||' '||INITCAP(e.MIDDLE_NAME)) ) ,
e.FIRST_NAME, '.', ( LTRIM(RTRIM((INITCAP(e.Surname)))||' '||INITCAP(e.MIDDLE_NAME)) ) ,
( LTRIM(RTRIM((INITCAP(e.SURNAME )))||', '||INITCAP(e.FIRST_NAME)||' '||INITCAP(e.MIDDLE_NAME)) ) ) as emp_name
FROM Employee e
Problem: The above statement is only working for those employees with surname of . (dot). It's not working for first names of dot. How can I use the decode statement OR is there any other way of doing it without using the CASE statement?
It seems my decode statement doesn't work with 2 different fields (surname, firstname) being tested within one decode statement.Thanks.Thank you so much InoL . I have used the ltrim with Replace but now have a new problem.
Because I am joining Surname, First name and middle name together and put a comma after the Surname, the name now appears as follows:
, Maria Ane (if Surname is a dot)
Boiler, (if first name is a dot)
I would like to get rid of a comma and only have comma when surname or first name does not have a dot, i.e. for those people with full names e.g. Blake, Anna Marie.
InoL, how can I achieve this? Thanks. -
Can I use decode( decode...)...)
Hi,
Can I use decode function inside decode function.
It seem doesn't work. Please help
decode(lv.RANK||' / '||
decode('('||lv.ONE_YR_PCT||'%)', '(%)','N/A','('||lv.ONE_YR_PCT||'%)'),
'0 / (0%)','N/A', lv.RANK )
ThanksI'm not quite sure it's the result you intended but your nested decode statement does run ...
SQL> CREATE TABLE lv (rank number, one_yr_pct number)
2 /
Table created.
SQL> insert into lv values (1, 1)
2 /
1 row created.
SQL> insert into lv values (2,2)
2 /
1 row created.
SQL> insert into lv values (0,0)
2 /
1 row created.
SQL> select decode(lv.RANK||' / '||
2 decode('('||lv.ONE_YR_PCT||'%)', '(%)','N/A','('||lv.ONE_YR_PCT||'%)'),
3 '0 / (0%)','N/A', lv.RANK )
4 from lv
5 /
DECODE(LV.RANK||'/'||DECODE('('||LV.ONE_
1
2
N/A
SQL> If you're having problems maybe you should cut'n'paste the output from your SQL*Plus session so we can see what's happening.
Alternatively, as CD suggests, you may find it easier to express your logic using CASE.
Cheers, APC -
Problem with decode!!!
Hi guys,
I have a problem with "decode"!
How can I tell the decode expression
that if a rowset don't exists --> then doing something.
I've tried that with "NULL" but that don't works.
here my code:
I wanted that if tx.text doesn't exists then
it returns fs.anlageschwerpunkt_lang, and otherwise
it returns tx.Text.
select
decode(tx.Text,NULL,fs.anlageschwerpunkt_lang,tx.Text) Anlageschwerpunkt_1 from
POI.Fondsstamm fs,
Texte tx,
Texttypstamm txy
where
tx.match='amfcomm'
and tx.match=rtrim(fs.match)
and tx.TEXTTYPID=txy.TEXTTYPID
and txy.BEREICHSID=14
and txy.texttyp='Marketingstrategie'
I hope somebody of you can help me
thanxx
SchoeibI read Schoeibs question differently; no ROWS are being returned. In that case you cannot expect DECODE or any other function to do anything. Assuming that rows always exist in fs and txy and are only missing in tx, then you need an outer join. Note that the join of tx.match = rtrim(fs.match) has been altered to avoid outer joining to more than one table. As mentioned before, although the decode is OK, NVL could be used instead.
select
decode(tx.Text,NULL,fs.anlageschwerpunkt_lang,tx.Text) Anlageschwerpunkt_1 from
POI.Fondsstamm fs,
Texte tx,
Texttypstamm txy
where
tx.match(+)='amfcomm'
and rtrim(fs.match) = 'amfcomm'
and tx.TEXTTYPID(+)=txy.TEXTTYPID
and txy.BEREICHSID=14
and txy.texttyp='Marketingstrategie'
Maybe you are looking for
-
Open Documents with Office Web App - SharePoint 2013
We have configured Office Web App for our SharePoint 2013 Farm and in Site settings, Site Collection Features, have disabled "Open Documents in Client Applications by Default" . Out of 15 sites, all documents in 13 sites open in Office Web App. One
-
Mighty mouse scroll ball behavior
Less than a week ago, I bought a Wireless Mighty Mouse. I thought that it was having trouble scrolling right, so I took it back for a replacement. This one seems to have the same problem. The problem seems to be that the mouse has a hard time scrolli
-
Password Manager 4.10 forced to foregound
I use Windows 7 Pro and a ThinkPad X230 One of the products i install is Thinkvantage Password manager 4.10. When I start Lotus Notes as a user the welcome and password prompt will appear. Behind that screen a TV Password Manager screen is displayed
-
Hello, I am using an iSPC chart and feeding in my own control limits. I have multiple (up to three) phases of control limit data, each with varying center lines, UCL and LCL's. I have two problems. 1) The control limits seem to be plotted before thei
-
HT201412 my ipdad is restarting again and again
After updating to i-os 8, my I pad is stucking. It is hanging again n again. It is just showing apple symbol. Then i need to restart again and again. sometime it is restarting but sometime i need to try so many times to get it start again. it become