Decode or case in where
i have a problem in decode clause
i have 2 conditions in my code
--condition 1
-- this condition in where clause
where gl.attribute1 is null
and gl.attribute2 is null
-- condition 2
--another condition in where clause is
where gl.attribute1 is not null
and gl.attribute2 is not nulli have a parameter is :p_exception = yes then condition 1 runs otherwise condition 2 runs
please help
Checkout:
SQL> SELECT * FROM t;
A B
1
5
4 5
SQL> variable p_exception varchar2(3);
SQL> exec :p_exception :='Yes';
PL/SQL procedure successfully completed.
SQL> SELECT * FROM t WHERE (:p_exception = 'Yes' AND a IS NULL AND b IS NULL) OR (:p_exception='No' AND a IS NOT NULL AND b IS NOT NULL);
A B
SQL> exec :p_exception :='No';
PL/SQL procedure successfully completed.
SQL> ed
Wrote file afiedt.buf
1* SELECT * FROM t WHERE (:p_exception = 'Yes' AND a IS NULL AND b IS NULL) OR (:p_exception='No' AND a IS NOT NULL AND b IS NOT NULL)
SQL>
SQL> /
A B
4 5
Similar Messages
-
Decode/Case in Where clause
Hello,
We are experiencing an issue with a Select statement that uses Decode in the Where clause. Specifically, it seems to be ignoring a nested Decode and just returning the default value. We have another nested decode that works fine, though.
A member of our team mentioned that he believed there was an issue with using Decode and Case statements inside of a where clause within HTML DB...Is this correct? If it is, is there a workaround? Since the nested Decode works elsewhere in this statement, that doesn't seem right.
My select statement looks like:
SELECT
SUBSTR(OBOB.OBOB_CNAME, 0, 30) d,
OBOB.OBOB_UID r
FROM
ISR_OBOB_OBJECT OBOB,
ISR_OBAF_OBJECT_AFFILIATION OBAF,
ISR.ISR_OBSD_SDR OBSD
WHERE
OBOB.OBOB_UID = OBSD.OBOB_UID AND
OBOB.OBOB_UID = OBAF.OBOB_B_UID AND
/*If Personal radio button is selected, displays all SDRs associated with user.
If All is selected, displays all SDRs associated with IS group selected from drop down list
or every SDR.
OBAF.OBOB_A_UID = DECODE(:P1_DISPLAY_ALL,
'Personal', :F101_APP_USER_UID,
/*This is the decode statement that it seems to ignore. If ALL is selected, the query should see if a group has
has been selected from a drop down list that appears when the ALL button is chosen. If a group is selected
(the item isn't 0), only SDRs for that group should be shown. Otherwise, all SDRs for every group should display.
'ALL', DECODE(:P1_SEARCH_IS_SUPPORT_GROUP, 0, OBAF.OBOB_A_UID, :P1_SEARCH_IS_SUPPORT_GROUP),
OBAF.OBOB_A_UID) AND
/*If Personal radio button is selected, display SDRs where user is the Primary assignee.
OBAF.OBAT_UID = DECODE(:P1_DISPLAY_ALL, 'Personal', (select OBAT_UID from ISR_OBAT_OBJ_AFFIL_TYPE WHERE OBAT_APP_REF = 'SDR_PRIMARY'), OBAF.OBAT_UID) AND
/*If a SDR Status (open, completed, on hold, not started...) is selected, only display the SDRs with that status.
OBSD.KTTR_STATUS_UID = DECODE(:P1_ISR_STATUS_UID, 0, OBSD.KTTR_STATUS_UID, :P1_ISR_STATUS_UID) AND
/*If SDR_History textbox is Null, or Open, Not Started, or On Hold SDR status has been selected, then all SDRs with
a create date between today and 99999 months ago will display. Otherwise, only SDRs with a create date between
today and however many months are in the textbox will display (i.e. Completed SDRs created in the past 6 months.)
MONTHS_BETWEEN(sysdate, OBSD.OBSD_CREATE_DATE) <= DECODE(:P1_SDR_HISTORY, NULL, 99999, DECODE(:P1_ISR_STATUS_UID,
(select KTTR_UID from ISR_KTTR_TRANSLATION where KTTR_APP_REF = 'SD_STAT_OPEN'), 99999,
(select KTTR_UID from ISR_KTTR_TRANSLATION where KTTR_APP_REF = 'SD_STAT_NOT_STA'), 99999,
(select KTTR_UID from ISR_KTTR_TRANSLATION where KTTR_APP_REF = 'SD_STAT_HOLD'), 99999,
:P1_SDR_HISTORY))
/*Alphabetical order
Order by
dWe originally wrote this as a PL/SQL statement that returned a query string since most of the where clause is dependent on items the user may or may not select, but we have moved the query into a multiselect list, which only seems to allow SQL Queries.
Any help or advice would be appreciated.
Thanks,
ScottScott: Did you try running that SQL statement in SQL Workshop in Apex? You can run it as it is, it will popup a window asking you to enter values for the bind variables.
JAC73: I don't think an IN clause doesn't work that way, you need a actual SQL sub-query, not an expression from a DECODE/CASE statement. Search this site for str2tbl and see Tom's excellent discussion at
http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:110612348061 -
Hi
I need small clarification about difference between decode and case
Thanks who visit my threadAnd for those people who can't be ar$ed to follow links...
Decode allows for conditional output where the conditions are straightforward matches between values.
Case allows for regular conditions to be used such as greater than, less than etc.
Some of the things done by case can be achieved through decode as well but in a more convoluted manner than simply using case.
;) -
I have emp_allocation table. It has data as below
EMPID YEAR MONTH
X 2006 JAN
X 2006 MAR
Y 2006 JAN
Y 2006 FEB
Y 2006 MAR
I want SQL Query(Without Decode or Case) which will give output as below
EMPID YEAR JAN FEB MAR APR JUN JUL
X 2006 Y N Y
Y 2006 Y Y YWhy you'd want to do it this way I do not know, but if you insist...
SQL> ed
Wrote file afiedt.buf
1 WITH t AS (select 'X' AS EMPID, 2006 AS YEAR, 'JAN' AS MONTH FROM DUAL UNION ALL
2 select 'X', 2006, 'MAR' FROM DUAL UNION ALL
3 select 'Y', 2006, 'JAN' FROM DUAL UNION ALL
4 select 'Y', 2006, 'FEB' FROM DUAL UNION ALL
5 select 'Y', 2006, 'MAR' FROM DUAL)
6 -- END OF TEST DATA
7 SELECT EMPID, YEAR, NVL(MAX(JAN),'N') AS JAN, NVL(MAX(FEB),'N') AS FEB, NVL(MAX(MAR),'N') AS MAR, NVL(MAX(APR),'N') AS APR
8 FROM
9 (
10 SELECT EMPID, YEAR, 'Y' AS JAN, NULL AS FEB, NULL AS MAR, NULL AS APR FROM t WHERE MONTH = 'JAN' UNION ALL
11 SELECT EMPID, YEAR, NULL AS JAN, 'Y' AS FEB, NULL AS MAR, NULL AS APR FROM t WHERE MONTH = 'FEB' UNION ALL
12 SELECT EMPID, YEAR, NULL AS JAN, NULL AS FEB, 'Y' AS MAR, NULL AS APR FROM t WHERE MONTH = 'MAR' UNION ALL
13 SELECT EMPID, YEAR, NULL AS JAN, NULL AS FEB, NULL AS MAR, 'Y' AS APR FROM t WHERE MONTH = 'APR'
14 )
15 GROUP BY EMPID, YEAR
16* ORDER BY 1,2
SQL> /
E YEAR J F M A
X 2006 Y N Y N
Y 2006 Y Y Y N
SQL> -
What is better and fast to use between decode and case
Hi friends,
i wanted to know what is better to use decode or case in sql,which gives faster result.
thks
sonal....Here's a very simple timing comparison. This table (actually it's a partition) has a little over 1 million rows.
As you can see, the timing difference is trivial. (I ran an earlier query to fetch the blocks from disk).
SQL> select sum(decode(balloon_flag
2 ,'Y',1
3 ,0
4 )
5 ) bal_count
6 from prod.loan_master
7 where report_date = to_date('31-DEC-2005');
BAL_COUNT
9036
Elapsed: 00:00:07.65
SQL> select sum(case balloon_flag
2 when 'Y' then 1
3 else 0
4 end
5 ) bal_count
6 from prod.loan_master
7 where report_date = to_date('31-DEC-2005');
BAL_COUNT
9036
Elapsed: 00:00:07.68
SQL> select sum(case
2 when balloon_flag = 'Y' then 1
3 else 0
4 end
5 ) bal_count
6 from prod.loan_master
7 where report_date = to_date('31-DEC-2005');
BAL_COUNT
9036
Elapsed: 00:00:07.46 -
How does decode and case works?
Hi,
I want to know how decode and case works? How they process the arguements. Please explain me with some examples.
Thanks,
Sarav
Edited by: 943941 on Jul 3, 2012 1:42 AMwelcome
check this link
https://forums.oracle.com/forums/ann.jspa?annID=1535
you will find everything you need
DECODE compares expr to each search value one by one. If expr is equal to a search, then Oracle Database returns the corresponding result. If no match is found, then Oracle returns default. If default is omitted, then Oracle returns null.
This example decodes the value warehouse_id. If warehouse_id is 1, then the function returns 'Southlake'; if warehouse_id is 2, then it returns 'San Francisco'; and so forth. If warehouse_id is not 1, 2, 3, or 4, then the function returns 'Non domestic'.
SELECT product_id,
DECODE (warehouse_id, 1, 'Southlake',
2, 'San Francisco',
3, 'New Jersey',
4, 'Seattle',
'Non domestic')
"Location of inventory" FROM inventories
WHERE product_id < 1775;Edited by: user 007 on Jul 3, 2012 2:40 PM -
Hello to everyone
I have a select like as
select
where (KPMERCE IS NULL AND KSMERCE IS NULL)
UNION
where (KPMERCE IS NOT NULL AND KSMERCE IS NULL AND K_PMERCE = KPMERCE)
UNION
where (KSMERCE IS NOT NULL AND K_PMERCE = KPMERCE AND K_SMERCE = KSMERCE)is possibile create a CASE in where clause, something as, So, i can write a single query,
CASE (in where-clause)
WHEN (KPMERCE IS NULL AND KSMERCE IS NULL)
WHEN (KPMERCE IS NOT NULL AND KSMERCE IS NULL AND K_PMERCE = KPMERCE)
WHEN (KPMERCE IS NOT NULL AND KSMERCE IS NOT NULL AND K_PMERCE = KPMERCE AND K_SMERCE = KSMERCE) so
IF CONDITION IS SATISFYED RETURNs ROWS OTHERWISE NO ROWS
Thanks in advanceIf your select lists are equal, then you can do it like this:
where ( (KPMERCE IS NULL AND KSMERCE IS NULL)
or (KPMERCE IS NOT NULL AND KSMERCE IS NULL AND K_PMERCE = KPMERCE)
or (KSMERCE IS NOT NULL AND K_PMERCE = KPMERCE AND K_SMERCE = KSMERCE)
)Regards,
Rob. -
I want to create an array that goes into a case structure where each element in the array is an individual case and gets done in order. Any ideas, I've been playing with the idea but have had no luck, is this even possible?
Hi,
Please check it out the attached Vi.. Is this you need?
Sasi.
Certified LabVIEW Associate Developer
If you can DREAM it, You can DO it - Walt Disney
Attachments:
Event.vi 11 KB -
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 -
Which is the best decode or case
Hi,
When you check performance wise which is the best one decode or case?
Thanks,> You mean CPU processor speed or oracle buffer(SGA).
Neither. CPU architecture. RISC vs CISC vs ..
On a PA-RISC1 CPU a DECODE is just a tad faster than a CASE. On an AMD64 CPU, the reverse is true.
> When I increase memory, The case and decode performance will increase?
No. A CASE and a DECODE does not need memory to work faster. It is a set of machine code instructions that needs to compare values to determine a result. It depends on just how fast the CPU can execute this set of machine code instructions.
A faster CPU will make a very significant difference. An AMD64 Opteron CPU is a couple of times faster than a PA-RISC1 CPU.
I had this exact same conversation back in 2006 on this forum - and posted [url
http://forums.oracle.com/forums/thread.jspa?messageID=1346165�]this benchmark to show that the decision of using CASE or DECODE is not a decision that should be based on raw performance. -
Hi,
What is the difference between decode and case?
What are the cases in which we can use Decode and case
Thanxyou can not put Search CASE statements by using DECODE
Eg:
SELECT AVG(CASE WHEN e.salary > 2000 THEN e.salary ELSE 2000 END) "Average Salary" FROM employees e;Can't we?
select avg(decode(sign(e.salary - 2000), 1, e.salary, 2000)) "Average Salary" from employees e; -
Hi all,
select
id
,one
,two
From
(select id
, one
, two
, Max(decode(TYPE, "ER", 3*4, 4*7)
from
(select id
, sum(one) as one
, sum(two) as two
from t1
group by id)
group by id, one, two
) temp inner join t2....
The above query is just a sample structure to tell the problem that i am facing. It works fine, but i need to convert it to ANSI Standard.
Hence I need to convert the DECODE to CASE statement. but if i convert to CASE, i need to use any of the GROUP BY FUNCTIONS, but if i use the MAX of any group by functions, the result would be wrong...how to overcome it
ThanksHi,
Yes I did a silly mistake, the code I gave was for only the decode but when I converted it to case statement, there was a silly mistake, which i corrected.
Now i corrected it to
Select
COL1
, Col2
, MAX(CASE WHEN Upper(TYPE) = 'YR' THEN Round(3.43*3, 2) END) AS CALC1
, Max(CASE WHEN Upper(TYPE) = 'ZP' THEN Round(3.12*12, 2) END) AS CALC2
From
Group by....
but now when i add a sum of the calc1 & calc2, I am not getting any results the column is blank. Is there anything that i missed
Select
COL1
, Col2
, MAX(CASE WHEN Upper(TYPE) = 'YR' THEN Round(3.43*3, 2) END) AS CALC1
, Max(CASE WHEN Upper(TYPE) = 'ZP' THEN Round(3.12*12, 2) END) AS CALC2
, (MAX(CASE WHEN Upper(TYPE) = 'YR' THEN Round(3.43*3, 2) END
+ Max(CASE WHEN Upper(TYPE) = 'ZP' THEN Round(3.12*12, 2) END)) as SUM3
From
Group by....
Thanks -
I'm a beginner with Oracle SQL and I have a select statement with the following code:
max(decode(EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD, '1000', EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE, '2000', EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE, '4000', EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE))
I've been asked to create several more columns in a similar manner. My concern is that since this relies on 'decode' which is less efficient than 'case', that adding more columns using this approach will bog down an already not efficient query. Bottom line my problem is that I don't really understand this 'decode' since all the explanations of 'decode' I've found stop at four parameters.
Could someone please show me how the expression above looks in If-then-else terms as well as comment on how to convert this 'decode' to 'case'? Thanks in advance!1) The if-then-else stmt
-- Longest way
if EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '1000' then
return value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE;
elsif EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '2000' then
return value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE;
elsif EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '4000' then
return value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE;
else return null;
end if;
-- shorter way
if EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '1000' or
EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '2000' or
EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '4000' then
return value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE;
else return null;
end if;
-- shortest if-then-else code
if EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD in ('1000', '2000', '4000') then
return value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE;
else return null;
end if;
2) The MAX() function
The MAX function will return the greatest value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE in step 1 above.
2) Using CASE
select
max(case when (EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD in('1000','2000','4000'))
then EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE end)
into ...your_item_or_local_variable
from EDW.V_RECRUIT_TEST_RESULT_HIST;
Try the code above, good luck
v/r
Vien Tran -
I wanna get a UML Case Tools, where can i find one for free or opensource?
Hi, Merry X.Mas!!!
I wanna get a UML Case Tools, where can i find one for free or opensource?
Merry X.Mas!!!Hi,
There is one more tool by name Enterprise Architect which supports UML2.0. A 30days eval copy is available. You can try hands on it. -
IPad Air. The case. Where is the origin of that leather ?
iPad Air. The new case.
Where does the leather comes from ?
What is the origin ?
Which animal ?Cowhide, I would imagine. Were it anything else (exotic) it would surely be advertised/marketed as such.
Maybe you are looking for
-
In the year view, why is iCal coloring the boxes?
In the year view, why is iCal coloring the boxes and what does each color mean and how do we change it? It makes no sense or has any rhymn or reason according to what is in my calendar.
-
Send to One note or print to one note not working...
print to onenote or send to one note is no longer working.... i have unistalled and re installed microsoft.... any one know how to resolve this issue? thanks :)
-
Problem on downloading a zip file
I am having a problem on downloading a zip file(file size is about 2Mb). Sometimes I can get a complete file and can open the file, sometimes the zip file is chunked(the file size is smaller than the right one), so I can't open it. The file content i
-
ORACLE error from auxiliary database: ORA-01152: file 2 was not restored fr
Hello, i'm trying to clone a database. It is in archive log mode. I took a full back using below command run { backup tag 'DAILY_WHOLE' format 'FULL_backup_%s_%p_%c.bkp' database plus archivelog tag 'DAILY_WHOLE' delete
-
[SOLVED] Ruby 1.9.2
I can't get the new Ruby to work. It's ruby 1.9.2 from Extra. Here is what I get when I run gem: [root@xhkr ruby]# gem /usr/lib/ruby/site_ruby/1.9.1/rubygems/source_index.rb:68:in `installed_spec_directories': undefined method `path' for Gem:Module (