To use Boolean function in DECODE or CASE statement
Hi all
I have a scenario where i need to use a boolean function inside DECODE statement. When i tried this way iam getting "ORA-06553: PLS-382: expression is of wrong type".
I doubt whether i can use boolean function inside DECODE or not?
My query will be like this:
select decode(my_fuction( ),'TRUE',1,'FALSE',0) from dual;
Any help is highly appreciated.
Thanks
Sriram
Overloaded functions must differ by more than their
return type . At the time that the overloaded
function is called, the compiler doesn't know what type
of data that function will return. The compiler cannot,
therefore, determine which version of the function to
use if all the parameters are the same.
Similar Messages
-
Give me detail functions use boolean function, use detail
give me detail functions use boolean function, use detail func in calculated key figure
Hi,
Adding to that some more few links which helps you are
Defining Formulas:[http://help.sap.com/saphelp_nw70/helpdata/EN/71/0aec3b6e011341e10000000a114084/content.htm]
Percentage Functions:[http://help.sap.com/saphelp_nw70/helpdata/EN/e2/16f13a2f160f28e10000000a114084/content.htm]
Data Functions:[http://help.sap.com/saphelp_nw70/helpdata/EN/e2/16f13a2f160f28e10000000a114084/content.htm]
Boolean Operators:[http://help.sap.com/saphelp_nw70/helpdata/EN/23/17f13a2f160f28e10000000a114084/content.htm]
Technicle Notes About Formula Operators:[http://help.sap.com/saphelp_nw70/helpdata/EN/37/b4393ccab19b57e10000000a114084/content.htm]
Defining Restricted Key Figures :[http://help.sap.com/saphelp_nw70/helpdata/EN/61/d0b143aa26b849b4e79a859ea1d7d1/content.htm]
Defining Calculated Key Figures :[http://help.sap.com/saphelp_nw70/helpdata/EN/13/e072abaddb574284d22361f0b824bf/content.htm]
[http://help.sap.com/saphelp_nw70/helpdata/EN/d3/8057f830a911d4b2be0050dadfb23f/content.htm]
[http://help.sap.com/saphelp_nw70/helpdata/EN/d7/c70540ba5ee569e10000000a155106/content.htm]
Regards,
Rajkandula -
MDX -Children count function Not working in Case statement
Hi,
I am trying to create set when you slice with the Hierarchy member is leaf level , I want a output only that Leaf level .
and When I slice with the parent level , it has to give all the members below that parent level.
But the problem here is when I select child member or leaf member , The first condition in the Case is not working
WITH SET
TESTSET AS
CASE
WHEN
[Dimension].[Hierarchy].currentmember.children.count<0
THEN
[Dimension].[Hierarchy].currentmember
ELSE
DESCENDANTS([Dimension].[Hierarchy].Currentmember,,AFTER)
END
SELECT
WBSSET ON 1,
{} on 0
FROM
(SELECT {[Dimension].[Hierarchy].&[10]} ON COLUMNS FROM [CubeName])
Thanks,
SantoshHi Santosh,
I don't think Children count function not working in case statement, I have tested it on my local environment, here s the sample query for you reference.
with member
testset as
case
when
[Geography].[Geography].currentmember.children.count<10
then "X"
else "OK"
end
select testset on 0,
{[Geography].[Geography].[Country].members} on 1
from
[Adventure Works]
In your scenario, the issue might be caused by the query isself, you can try to use IsLeaf Funcion to achieve your requirement. Please refer to the links below.
http://msdn.microsoft.com/en-us/library/ms144932.aspx
http://www.databasejournal.com/features/mssql/article.php/3633696/MDX-Operators-The-IsLeaf-Operator--Conditional-Logic-within--Calculations.htm
http://www.mdxpert.com/Functions/MDXFunction.aspx?f=22
Regards,
Charlie Liao
TechNet Community Support -
Using a scalar user defined function in a select case statement
I have a simple select statement below and I want to have a case statement for some conditions based on the result of the udf returned value. I then want the returned value from that specific case statement to be returned where i have indicated 'DISPLAY
ZIPCODE' below.
SELECT
CASE
WHEN (SELECT Top 1 ZipCode FROM ufn_GetAddressByBusinessEntityIDandAddressTypeID(table1.BusinessEntityID,712) IS NOT NULL THEN 'DISPLAY ZIPCODE'
WHEN (SELECT Top 1 ZipCode FROM ufn_GetAddressByBusinessEntityIDandAddressTypeID(table1.BusinessEntityID,714) as r) IS NOT NULL THEN 'DISPLAY ZIPCODE'
ELSE NULL
END as Zipcode,
FROM
table1SELECT COALESCE(ufn_GetAddressByBusinessEntityIDandAddressTypeID(table1.BusinessEntityID,712),ufn_GetAddressByBusinessEntityIDandAddressTypeID(table1.BusinessEntityID,712)) AS Zipcode
FROM table1
Nope. This is two function calls. coalesce(a, b, c, ...) is just syntatic sugar for
CASE WHEN a IS NOT NULL THEN a
WHEN b IS NOT NULL THEN b
WHEN c IS NOT NULL THEN c
END
But if you use isnull it's a different matter. (But isnull() permits two arguments.)
Erland Sommarskog, SQL Server MVP, [email protected] -
Can you use boolean function in where clause
Hi,
I have a boolean function. Is it possible to use it in where clause of query.
Eg;
is_prime(13) returns boolean
select 1 from dual where is_prime(13)What about something like this
Create or replace function boolret(id number) return boolean as
begin
If id <10 then
return true;
elsif id>10 and id<100 then
return false;
else
return null;
end if;
end;
1 declare
2 id_cat boolean;
3 begin
4 id_cat:= boolret(8);
5 dbms_output.put_line(id_cat);
6* end;
SQL> /
dbms_output.put_line(id_cat);
ERROR at line 5:
ORA-06550: line 5, column 2:
PLS-00306: wrong number or types of arguments in call to 'PUT_LINE'
ORA-06550: line 5, column 2:
PL/SQL: Statement ignored
It seems PL/Sql doesn't have any datatype like Boolean,But it handles Boolean like datatype.
Regards
Raj deep.A -
Decode and case statement in the update..
Its is more to it, but I want to check with you guys, the experts on this, this look busy to me, it should be a more simplify way of doing it, do you think will work
The government decide to change the ethnic codes, and we have to come with certain rules to in our report, anyway, do you think this will work? again It is more to it I declare my variables, this is just one part of the precedure.
BEGIN
UTL_FILE.fclose_all;
v_file_handle := UTL_FILE.fopen (v_out_path, v_out_file, 'a');
UTL_FILE.put_line (v_file_handle,
CHR (10) || TO_CHAR (SYSDATE, 'DD-MON-YYYY HH:MI:SS')
UTL_FILE.put_line (v_file_handle, 'Entering spbpers_update');
SELECT upd_spbpers_upd_cur IS
spriden_pidm,
szscapp_birth_state,
szscapp_birth_city,
DECODE(szscapp_hisp_or_latino_ind,Y,'2',N,'1'),
DECODE(szscapp_hisp_or_latino_options,XCM,'2',CUB,'2',MEX,'2',PRI,'2',XSM,'2',ESP,'2',XOH,'2'),
DECODE(szscapp_amer_indn_alaska_opt,XAN,'1','1',XCW,'1',XCH,'1',XCK,'1',XNV,'1',XSX,'1'),
DECODE(szscapp_amer_indn_alaska_other,XON,'1') (,IND,'1',JPN,'1',KOR,'1',PAK,'1',PHL,'1',VNM,'1',XEA,'1',XIS,'1',XSA,'1'),
DECODE(szscapp_asian_options,IND,'1',JPN,'1',KOR,'1',PAK,'1',PHL,'1',VNM,'1',XEA,'1',XIS,'1',XSA,'1'), ,
DECODE(szscapp_other_east_asia,(IND,'1',JPN,'1',KOR,'1',PAK,'1',PHL,'1',VNM,'1',XEA,'1',XIS,'1',XSA,'1'),
DECODE(szscapp_other_indian_subcont,XIS,'1'),
DECODE(szscapp_other_southeast_asia,XSA,'1'),
DECODE(szscapp_blk_or_afr_amer_opt,XAA,'1',XAF,'1',XCB,'1',XOA,'1'),
DECODE(szscapp_blk_or_afr_amer_other,XOA,'1'),
DECODE(szscapp_natve_hawaian_options,GUM,'1',XHI,'1',ASM,'1',XOP,'1'),
DECODE(szscapp_hawaiian_other,XOP,'1'),
DECODE(szscapp_white_options,XEU,'1',XME,'1',XOW,'1'),
DECODE(szscapp_white_other(XOW,'1')
FROM
saturn_midd.szscapp
WHERE
spriden_id = szscapp_id
AND spriden_ntyp_code = 'CAPL'
IF upd_spbpers_upd_cur%ISOPEN
THEN
CLOSE upd_spbpers_upd_cur;
END IF;
OPEN upd_spbpers_upd_cur;
LOOP
FETCH upd_spbpers_upd_cur
INTO v_pidm,v_birth_state,v_birth_city,v_latino_ind,v_latino_options,
v_indn_alaska_opt,v_indn_alaska_other,v_asian_options,
v_other_east_asia,v_other_indian_subcont,v_other_southeast_asia,
v_blk_or_afr_amer_opt,v_blk_or_afr_amer_other,v_natve_hawaian_options,
v_hawaiian_other,v_white_options,v_white_other;
EXIT WHEN upd_spbpers_upd_cur%NOTFOUND;
IF upd_spbpers_upd_cur%FOUND
UPDATE saturn.spbpers
set SPBPERS_ETHN_CODE = CASE
WHEN v_latino_ind IS NOT NULL THEN (spbpers_ethn_code = v_latino_ind,spbpers_activity_date = sysdate)
WHEN v_latino_options IS NOT NULL THEN (spbpers_ethn_code = v_latino_options,spbpers_activity_date = sysdate)
WHEN v_indn_alaska_opt IS NOT NULL THEN (spbpers_ethn_code = v_indn_alaska_opt,spbpers_activity_date = sysdate)
WHEN v_indn_alaska_other IS NOT NULL THEN (spbpers_ethn_code = v_indn_alaska_other,spbpers_activity_date = sysdate)
WHEN v_asian_options IS NOT NULL THEN (spbpers_ethn_code = v_asian_options,spbpers_activity_date = sysdate)
WHEN v_other_east_asia IS NOT NULL THEN (spbpers_ethn_code = v_other_east_asia,spbpers_activity_date = sysdate)
WHEN v_other_indian_subcont IS NOT NULL THEN (spbpers_ethn_code = v_other_indian_subcont,spbpers_activity_date = sysdate)
WHEN v_other_southeast_asia IS NOT NULL THEN (spbpers_ethn_code = v_other_southeast_asia,spbpers_activity_date = sysdate)
WHEN v_blk_or_afr_amer_opt IS NOT NULL THEN (spbpers_ethn_code = v_blk_or_afr_amer_opt,spbpers_activity_date = sysdate)
WHEN v_blk_or_afr_amer_other IS NOT NULL THEN (spbpers_ethn_code = v_blk_or_afr_amer_other,spbpers_activity_date = sysdate)
WHEN v_natve_hawaian_options IS NOT NULL THEN (spbpers_ethn_code = v_natve_hawaian_options,spbpers_activity_date = sysdate)
WHEN v_hawaiian_other IS NOT NULL THEN (spbpers_ethn_code = v_hawaiian_other,spbpers_activity_date = sysdate)
WHEN v_white_options IS NOT NULL THEN (spbpers_ethn_code = v_white_options,spbpers_activity_date = sysdate)
WHEN v_white_other IS NOT NULL THEN (spbpers_ethn_code = v_white_other,spbpers_activity_date = sysdate)
WHEN v_birth_state IS NOT NULL THEN (spbpers_stat_code_birth = v_birth_state,spbpers_activity_date = sysdate)
WHEN v_birth_city IS NOT NULL THEN (spbpers_city_birth = v_birth_city,spbpers_activity_date = sysdate)
WHERE spbpers_pidm = v_pidm;
END
END IF;
END LOOP;Did the procedure compile ?
Doesn't look like a right Decode syntax.
DECODE (col1,'VAL1','This','VAL2','That','ElseThis')
means
--Psuedocode
IF col1 = 'VAL1' THEN 'This'
IF col1 = 'VAL2' THEN 'That'
ELSE 'ElseThis'You can use CASE statement Instead of DECODE
CASE
when szscapp_amer_indn_alaska_other
in ('XON','IND','JPN','KOR','PAK' ..... ) THEN '1'
when szscapp_hisp_or_latino_options
in ('XCM','CUB','MEX','PRI','XSM','ESP','XOH' ...) THEN '2'
END SS -
Using a function return in an "IN" statement
All,
I need to filter the records in a table based on the return value of a function. The function determines a person's group and then executes 2 separate select statements depending on the group.
CREATE OR REPLACE PACKAGE pkg_rolebased
AS
CURSOR all_dmn_cur
IS
SELECT dmn_id
FROM tomwojeck.pa_domain ;
CURSOR child_dmn_cur (role_id_in IN varchar)
IS
SELECT distinct r.dmn_id
FROM tomwojeck.pa_domain_restriction_domain r
WHERE r.dmn_restriction_id IN (
SELECT DISTINCT a.dmn_restriction_id
FROM tomwojeck.pa_role_wf_entity_fct_access a
WHERE a.role_id = role_id_in
AND a.workflow_entity_fct_id= 'View Student.Student.View'
AND a.dmn_restriction_id IS NOT NULL);
FUNCTION rolelookup (stud_email IN varchar)
RETURN roletable;
END;CREATE OR REPLACE PACKAGE BODY pkg_rolebased
IS
FUNCTION rolelookup (stud_email IN varchar)
RETURN roletable
IS
v_roleid varchar2(200);
v_returnval varchar2(200);
v_data roletable := roletable ();
dmn_rec all_dmn_cur%ROWTYPE;
BEGIN
-- Find out the role of the person
SELECT r.role_id
INTO v_roleid
FROM tomwojeck.pa_user_prfl p, tomwojeck.pa_user_prfl_role r
WHERE p.user_name = r.user_name
AND UPPER (p.email_addr) = UPPER (stud_email);
IF UPPER (v_roleid) = 'ALL'
THEN
OPEN all_dmn_cur;
LOOP
FETCH all_dmn_cur
INTO dmn_rec;
v_data.EXTEND;
v_data (v_data.COUNT) := dmn_rec.dmn_id;
EXIT WHEN all_dmn_cur%NOTFOUND;
END LOOP;
CLOSE all_dmn_cur;
ELSE
OPEN child_dmn_cur (v_roleid);
LOOP
FETCH child_dmn_cur
INTO dmn_rec;
v_data.EXTEND;
v_data (v_data.COUNT) := dmn_rec.dmn_id;
EXIT WHEN child_dmn_cur%NOTFOUND;
END LOOP;
CLOSE child_dmn_cur;
END IF;
RETURN v_data;
END rolelookup;
END;
The select statement to filter on this mess is:
select
lname,
fname,
dmn_id,
stud_id
from tomwojeck.pa_student p
where p.DMN_ID in (select * from table(cast(tomwojeck.pkg_rolebased.rolelookup('[email protected]') as tomwojeck.RoleTable)))
The problemm is this performs horribly! If I enter an email address that causes the first cursor to be used, it runs well, but if I use an email address that causes the second cursor to run, it takes forever.
Is there a better way to do this?
Thanks,
Tom Wojeck<<The version of Oracle is 9i.>>
Could you specify the exact version? For example, 9.2.0.3.0.
<<I'm not sure of the query optimizer version. How do I find that?>>
SQL> show parameter optimizer_mode
NAME TYPE
VALUE
optimizer_mode string
CHOOSE<<The interestin thing is that the function itself runs well; it's when I try to use the function as an "IN" clause that the performance degrades. It almost seems as though the database is retrieving each row from the student table, and then running the function to see if there are any matches. >>
Your query has to be tuned. The execution plan as well as the information on the indexes will certainly help. -
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 -
What will be good for proformance wise:
Decode or Case in a sql statement.?????See the following link for Tom Kyte's opinion (point #4 in his first answer):
http://asktom.oracle.com/pls/ask/f?p=4950:8:16717708356827415201::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:1243867216406 -
Using Presentation variables..along with case statements..
Hi All.
I have a issue using presentation variable along with CASE statements. My approach is
1) I have a dashboard prompt, which is being set as Presentation variable.
Based on the value selected in prompt, for ex the values of prompt can be 'ABC' and 'DEF'.
I have a calculated column, the calculation goes this way...
The forumal is
CASE WHEN @{Presentation Variable Name} = 'ABC' THEN xxxxxxxxxx ELSE IF @{Presentation Variable Name} = 'DEF' END. It gives error of "no table being referenced"..
Is this is the right approach??
Can i get the values of variable in a column formula, so that a column can have values selected in prompt?
Can anybody pls help me here..
Thanks in advance...Hi
Thanks for the quick response..
I agree to ur point..
But the requirement is
Based on the value of the prompt I need to switch the calculation in one of the formula area of one column..
If Prompt value is ABC then one kind of calculation in Fx and If the prompt value is DEF then one kind of calculation in the same Fx..
How can I acheive this?
Thanks in advance.. -
Using function on decode or case in query
Hi experts
I am using oracle 11G database,I have to check length of name column value from employee table and if length(name) > 39 then value should be substr(name,0,39)
else value should be name only.
i tried below code
select CASE when length(name) > 39,substr(name,0,39)
else name
END
from employee but its not working ..can I do this using decode too ? ,,which one would be better or this is not a right way ?
ThanksHi,
siebelD wrote:
Hi experts
I am using oracle 11G database,I have to check length of name column value from employee table and if length(name) > 39 then value should be substr(name,0,39)
else value should be name only.
i tried below code
select CASE when length(name) > 39,substr(name,0,39)
else name
END
from employee but its not working ..Review the syntax of the CASE expression in the SQL Language manual. A CASE expression always has at least one THEN clause. Commas are not part of the CASE expression syntax.
can I do this using decode too ? Sure, anything you can do with CASE you can also do with DECODE.
,,which one would be betterThis is one of the many situations where CASE is much shorter, clearer and more efficient than DECODE.
or this is not a right way ?How about
SELECT SUBSTR (name, 1, 39) AS employee_name
FROM employee;?
If name is 39 characters (or less), then <tt> SUBSTR (name, 1, 39) </tt> will return it, unchanged.
Edited by: Frank Kulash on Mar 3, 2013 2:07 PM -
How can I use oracle function to decode the encode value
Hi everybody,
If the data is encode value how can I decode this valueDBMS_OBFUSCATION_TOOLKIT
DBMS_OBFUSCATION_TOOLKIT allows an application to encrypt data using either the Data Encryption Standard (DES) or the Triple DES algorithms.
The Data Encryption Standard (DES), also known as the Data Encryption Algorithm (DEA) by the American National Standards Institute (ANSI) and DEA-1 by the International Standards Organization (ISO), has been a worldwide encryption standard for over 20 years. The banking industry has also adopted DES-based standards for transactions between private financial institutions, and between financial institutions and private individuals. DES will eventually be replaced by a new Advanced Encryption Standard (AES).
DES is a symmetric key cipher; that is, the same key is used to encrypt data as well as decrypt data. DES encrypts data in 64-bit blocks using a 56-bit key. The DES algorithm ignores 8 bits of the 64-bit key that is supplied; however, developers must supply a 64-bit key to the algorithm.
Triple DES (3DES) is a far stronger cipher than DES; the resulting ciphertext (encrypted data) is much harder to break using an exhaustive search: 2**112 or 2**168 attempts instead of 2**56 attempts. Triple DES is also not as vulnerable to certain types of cryptanalysis as is DES. DES procedures are as follows:
DESEncrypt Procedure
DESDecrypt Procedure
Oracle installs this package in the SYS schema. You can then grant package access to existing users and roles as needed. The package also grants access to the PUBLIC role so no explicit grant needs to be done.
This chapter discusses the following topics:
Overview of Key Management
Summary of DBMS_OBFUSCATION Subprograms
Overview of Key Management
Key management, including both generation and secure storage of cryptographic keys, is one of the most important aspects of encryption. If keys are poorly chosen or stored improperly, then it is far easier for a malefactor to break the encryption. Rather than using an exhaustive key search attack (that is, cycling through all the possible keys in hopes of finding the correct decryption key), cryptanalysts typically seek weaknesses in the choice of keys, or the way in which keys are stored.
Key generation is an important aspect of encryption. Typically, keys are generated automatically through a random-number generator. Provided that the random number generation is cryptographically secure, this can be an acceptable form of key generation. However, if random numbers are not cryptographically secure, but have elements of predictability, the security of the encryption may be easily compromised.
The DBMS_OBFUSCATION_TOOLKIT package does not generate encryption keys nor does it maintain them. Care must be taken by the application developer to ensure the secure generation and storage of encryption keys used with this package. Furthermore, the encryption and decryption done by the DBMS_OBFUSCATION_TOOLKIT takes place on the server, not the client. If the key is passed over the connection between the client and the server, the connection must be protected using Oracle Advanced Security; otherwise the key is vulnerable to capture over the wire.
Key storage is one of the most important, yet difficult aspects of encryption and one of the hardest to manage properly. To recover data encrypted with a symmetric key, the key must be accessible to the application or user seeking to decrypt data. The key needs to be easy enough to retrieve that users can access encrypted data when they need to without significant performance degradation. The key also needs to be secure enough that it is not easily recoverable by an unauthorized user trying to access encrypted data he is not supposed to see.
The three options available to a developer are:
Store the key in the database
Store the key in the operating system
Have the user manage the key
Storing the Key in the Database
Storing the keys in the database cannot always provide bullet-proof security if you are trying to protect data against the DBA accessing encrypted data (since an all-privileged DBA can access tables containing encryption keys), but it can provide security against the casual snooper, or against someone compromising the database files on the operating system. Furthermore, the security you can obtain by storing keys in the database does not have to be bullet-proof in order to be extremely useful.
For example, suppose you want to encrypt an employee's social security number, one of the columns in table EMP. You could encrypt each employee's SSN using a key which is stored in a separate column in EMP. However, anyone with SELECT access on the EMP table could retrieve the encryption key and decrypt the matching social security number. Alternatively, you could store the encryption keys in another table, and use a package to retrieve the correct key for the encrypted data item, based on a primary key-foreign key relationship between the tables.
A developer could envelope both the DBMS_OBFUSCATION_TOOLKIT package and the procedure to retrieve the encryption keys supplied to the package. Furthermore, the encryption key itself could be transformed in some way (for example, XORed with the foreign key to the EMP table) so that the key itself is not stored in easily recoverable form.
Oracle recommends using the wrap utility of PL/SQL to obfuscate the code within a PL/SQL package itself that does the encryption. That prevents people from breaking the encryption by looking at the PL/SQL code that handles keys, calls encrypting routines, and so on. In other words, use the wrap utility to obfuscate the PL/SQL packages themselves. This scheme is secure enough to prevent users with SELECT access to EMP from reading unencrypted sensitive data, and a DBA from easily retrieving encryption keys and using them to decrypt data in the EMP table. It can be made more secure by changing encryption keys regularly, or having a better key storage algorithm (so the keys themselves are encrypted, for example).
Storing the Key in the Operating System
Storing keys in the operating system (that is, in a flat file) is another option. With Oracle8i you can make callouts from PL/SQL, which you could use to retrieve encryption keys. If you store keys in the O/S and make callouts to retrieve the keys, the security of your encrypted data is only as secure as the protection of the key file on the O/S. Of course, a user retrieving keys from the operating system would have to be able to either access the Oracle database files (to decrypt encrypted data), or be able to gain access to the table in which the encrypted data is stored as a legitimate user.
User-Supplied Keys
If you ask a user to supply the key, it is crucial that you use network encryption, such as that provided by Oracle Advanced Security, so the key is not passed from client to server in the clear. The user must remember the key, or your data is nonrecoverable.
http://download-west.oracle.com/docs/cd/B10501_01/appdev.920/a96612/d_obtool.htm#ARPLS028
Joel P�rez -
Using Decode Or Case Statement / Any Method
Dear All,
In my table Fields are
EmpId
TranYear
TranMonth
ApprovedBy
ApprovedYear
ApprovedMonth
My Input parameters are
1. TranYear
2. TranMonth
3. Tag1
4. Tag2
My requirement is
Tag1 Input are 0,1,2
Tag2 Inputs are 0,1,2
If Tag1 is 0 then checking TranYear/TranMonth And
Tag2 Is 0 Then ApprovedBy Is Not Null only
Tag2 Is 1 Then ApprovedBy Is Null only
Tag2 Is 2 Then both(Approved/unApproved)
If Tag1 is 1 then checking ApprovedYear/ApprovedMonth And Tag2 Passing 0 Default
If Tag1 is 2 then checking TranYear/TranMonth Or ApprovedYear/ApprovedMonth And
Tag2 Is 0 Then ApprovedBy Is Not Null only
Tag2 Is 1 Then ApprovedBy Is Null only
Tag2 Is 2 Then both(Approved/unApproved)
Based upon the input details select detailsdont quite understand your table structure requirement but it sounds like you want a conditional join.
looks like you are always joining on the tran year tran month but according to your 2 flags decides the rest of the join
I started something here where I am joining on the tran year / tran month then setting an indicator based on your flags
then just select the rows where this indicator is true.
anyway you can modify it to your needs
WITH t AS (SELECT 1 EmpId,
EXTRACT (YEAR FROM TO_DATE ('2010', 'YYYY')) TranYear,
EXTRACT (MONTH FROM TO_DATE ('Oct', 'Mon')) TranMon,
10 approved_by,
EXTRACT (YEAR FROM TO_DATE ('2010', 'YYYY')) ApprovedYear,
EXTRACT (MONTH FROM TO_DATE ('Nov', 'Mon')) ApprovedMon
FROM DUAL
UNION ALL
SELECT 2,
EXTRACT (YEAR FROM TO_DATE ('2009', 'YYYY')),
EXTRACT (MONTH FROM TO_DATE ('Jul', 'Mon')),
11,
EXTRACT (YEAR FROM TO_DATE ('2009', 'YYYY')),
EXTRACT (MONTH FROM TO_DATE ('Dec', 'Mon'))
FROM DUAL),
input AS (SELECT EXTRACT (YEAR FROM TO_DATE ('2010', 'YYYY')) TranYear,
EXTRACT (MONTH FROM TO_DATE ('Oct', 'Mon')) TranMon,
0 tag1,
0 tag2
FROM DUAL)
SELECT *
FROM (SELECT t.*,
tag1,
tag2,
CASE tag1 || '.' || tag2
WHEN '0.0'
THEN
CASE WHEN Approved_By IS NOT NULL THEN 1 ELSE 0 END -- if they are both 0 join on Tran Year, Tran Month and Approved by not null
WHEN '0.1'
THEN
CASE WHEN Approved_By IS NULL THEN 1 ELSE 0 END -- if they are 0,1 then Tran Year, Tran Month and Approved by is null
WHEN '0.2'
THEN
0 -- CASE --Then both(Approved/unApproved) i Don't know what this means
/* WHEN '1.0' then whatever
when '1.1' then whatever
when '1.2 then whatever
ELSE
0
END
ind
FROM t, input
WHERE t.TranYear = input.TranYear AND t.TranMon = input.TranMon)
WHERE ind = 1 -
Help with decode or case statement
I have the following insert statement
insert into t_outcome (
TRANSACTION_ID,
charge_DATE,
Charge_TIME,
STATUS_CODE)
values(cur_tem.transaction_id,
cur_tem.charge_date,
cur_tem.charge_time,
cur_tem.STATUS_code)
I require to incorporate the following rules into the population of charge_TIME
if cur_tem.STATUS_code is 7 and cur_temp.charge_time is not null then populate charge_TIME with cur_temp.charge_time
else if cur_tem.STATUS_code is 7 and cur_temp.charge_time is null then populate charge_TIME with -9
else if cur_tem.STATUS_code is not 7 then populate charge_TIME with -7
How do I add these rules into the insert statement ?
Thanks
Brendondecode(cur_tem.STATUS_code, 7, nvl(cur_tem.charge_time, -9), -7)
I think that will work.
Lee
Message was edited by:
Lee Forkenbrock -
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
Maybe you are looking for
-
MY LETTER TO CREATIVE...NO RESPONSE FROM THEM!!!
From: [email protected] To: [email protected]; [email protected]; [email protected] Subject: CONSUMERS HAVE NO FAITH IN CREATIVE Date: Fri, 6 Jun 2008 00:49:40 -0400 Dear Creative, In the years that have pas
-
Error while opening an excel file via labview
i don't know why i get this message,then the excel won't open at all
-
Personalize Option Problem In Windows 8.1
Hi. I'm using genuine windows 8.1 pro. I downloaded windows 8.1 pro from my dream spark account. But I'm missing some features in my windows 8.1 pro. Personalize Option is still like windows 8 personalize option. I can't change the background of metr
-
the latest i tunes wont detect my iphone 5 or i pad??
-
Convert "Grayscale I16" to "RGB U64"
Hello, I need to convert an image from Grayscale I16 to RGB U64, but it doesn't work (I have joined a zip file). I think there is a bug in the NI Vision, or it's not implemented because I have no problem to do it from Grayscale U8 to RBG32. Can you