How to use CASE or DECODE in this?
Hi , I have a query like this
AND ...smthng
AND --smtg
AND age BETWEEN ....For the last AND clause for age column, I have the values passed from IN param of this SP in form of strings
'0-30', '31-60', '61-90', and '91+'. The IN param is of course VARCHAR2.
What I want to do in this AND clause
e.g. for age range '0-30', the AND clause should be
AND age BETWEEN ( if this range is '0-30' then ) 0 AND 30 ( if range is '31-60') then BETWEEN 31 AND 60.. and so on...
However this BETWEEN will work till 61-90 range but for 91+ , there is no upper bound so it should be
AND age>91
Now , i am not sure how to achieve this both BETWEEN and '>' clauses in the same AND statement.
DECODE will not work and am not sure how to use the CASE in this situation as even that can not solve this issue.
Dont want to make the last AND clause dynamic
Please suggest me how to do this.
Thanks,
Aashish
Edited by: Aashish S. on Oct 21, 2011 6:01 PM
A third alternative would be to choose a suitably large value as a high end for the 91+ group and use something like:
age between to_number(CASE WHEN instr(:param, '+') = 0
THEN substr(:param, 1, instr(:param, '-') -1)
ELSE substr(:param, 1, instr(:param, '+') -1) end) and
to_number(CASE WHEN instr(:param, '+') = 0
THEN substr(:param, instr(:param, '-') +1)
ELSE '9999' END)John
Similar Messages
-
How to use case and decode to extract the data
Hello PL/SQL Gurus,
I am using Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production version
I have a table in following format -
drop table TT2;
create table TT2(College, Class,gender,status,fees) as select
'IITB','MBA','M','P',255600 from dual union all select
'IITK','MTech','M','P',300000 from dual union all select
'IITD','MBA','F','P',450000 from dual union all select
'IITKH','MBA','F','P',350000 from dual union all select
'IITC','MTech','F','P',420000 from dual union all select
'IITB','MTech','M','P',185000 from dual union all select
'IITC','MTech','M','P',235000 from dual union all select
'IITD','MBA','F','F',175000 from dual union all select
'IITM','MBA','M','F',257000 from dual union all select
'IITKH','MTech','F','P',335000 from dual union all select
'IITD','MBA','F','P',540335 from dual union all select
'IITC','MBA','F','F',125089 from dual union all select
'IITD','MTech','M','P',290756 from dual union all select
'IITM','MBA','M','P',200000 from dual union all select
'IITKH','MBA','F','F',534990 from dual union all select
'IITD','MBA','F','P',221000 from dual ;some of the extraction conditions are as following -
CASE CONDITION
College in 'IITB' and status='P'- 'WestRegion Passed'
College in 'IITC' and status='P'- 'SouthRegion Passed'
College in 'IITD' and 'IITK' and status='P' and Gender='F' - 'NothRegion Female Passed'
College not in 'IITK' and status='F' - 'Ex Kanpur Failed'
Expected output -
Region Statnding Fees
WestRegion Passed 440460
SouthRegion Passed 655000
NothRegion Female Passed 1386335
Ex Kanpur Failed 1092079SQL Used
I am using the following query which only make sure of case but this is not how i want the output , if i try to use the case within decode then how to work on this -
SELECT (CASE WHEN College in ('IITB') and status='P' then sum(fees) else 0 end) WP,
(case when College in ('IITC') and status='P' then sum(fees) else 0 end) SP,
(case when College in ('IITD','IITK') and gender='F' and status='P' then sum(fees) else 0 end) NFP,
(case when College in ('IITK') and status='F' then sum(fees) else 0 end) ExKF
FROM
TT2
GROUP BY College, Class,gender,statususer555994 wrote:
Thank you so much jeneesh i am really thankful to you ...vov.
one more query in case if any of the selection don't have the output data , then values will be displayed like -One way..
with t as
--"Add all your descriptions
(select 'WestRegion Passed' region_standing from dual union all
select 'SouthRegion Passed' region_standing from dual union all
select 'NothRegion Female Passed' region_standing from dual union all
select 'Ex Kanpur Failed' region_standing from dual)
select region_standing,sum(fees) fees
from (
(SELECT CASE WHEN College in ('IITB') and status='P'
then 'WestRegion Passed'
when College in ('IITC') and status='P'
then 'SouthRegion Passed'
when College in ('IITD','IITK') and gender='F' and status='P'
then 'NothRegion Female Passed'
when College in ('IITK') and status='F'
then 'Ex Kanpur Failed'
else 'Others' end region_standing,
sum(fees) fees
FROM TT2
GROUP BY CASE WHEN College in ('IITB') and status='P'
then 'WestRegion Passed'
when College in ('IITC') and status='P'
then 'SouthRegion Passed'
when College in ('IITD','IITK') and gender='F' and status='P'
then 'NothRegion Female Passed'
when College in ('IITK') and status='F'
then 'Ex Kanpur Failed'
else 'Others' end
union all
select region_standing,0
from t
group by region_standing;
REGION_STANDING FEES
Others 2567835
NothRegion Female Passed 1211335
WestRegion Passed 440600
Ex Kanpur Failed 0
SouthRegion Passed 655000
{code}
Edited by: jeneesh on Nov 5, 2012 5:07 PM -
How to use case function in where clause
Hi,
Suppose a table DEMO has columns
DEMO TABLE
user_id
user_name
location
In this table i have 15 users. but out of 15 users i want to use only 5 users for passing as user_name.
then how to achieve the result
1. when i pass the particular 5 user_name in where clause then i should get all the user_name and for other 10 users it will show only the passing user_name.
how to use case functionDo you mean this ?
SQL> var name varchar2(10)
SQL> exec :name := 'ALLEN'
PL/SQL procedure successfully completed.
SQL> select ename from emp where case when :name in ('SMITH','ALLEN') then ename
2 else :name end = ename;
ENAME
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
14 rows selected.
SQL> exec :name := 'SMITH'
PL/SQL procedure successfully completed.
SQL> select ename from emp where case when :name in ('SMITH','ALLEN') then ename
2 else :name end = ename;
ENAME
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
14 rows selected.
SQL> exec :name := 'BLAKE'
PL/SQL procedure successfully completed.
SQL> select ename from emp where case when :name in ('SMITH','ALLEN') then ename
2 else :name end = ename;
ENAME
BLAKERgds. -
How to use case when function to calculate time ?
Dear All,
May i know how to use case when function to calculate the time ?
for the example , if the First_EP_scan_time is 12.30, then must minus 30 min.
CASE WHEN FIRSTSCAN.EP_SHIFT <> 'R1' AND FIRSTSCAN.EP_SHIFT <> 'R2'
THEN ROUND(CAST((DATEDIFF(MINUTE,CAST(STUFF(STUFF((CASE WHEN SHIFTCAL.EP_SHIFT = 'N1'
THEN CONVERT(VARCHAR(8),DATEADD(DAY,+1,LEFT(FIRSTSCAN.EP_SCAN_DATE ,8)),112) + ' ' + REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+0,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','')
ELSE LEFT(FIRSTSCAN.EP_SCAN_DATE ,8) + ' ' + REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+0,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','') END),12,0,':'),15,0,':') AS DATETIME),CAST(STUFF(STUFF(LASTSCAN.EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME)) / 60.0 - 0.25) AS FLOAT),2)
ELSE ROUND(CAST((DATEDIFF(MINUTE,CAST(STUFF(STUFF(FIRSTSCAN.EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME),CAST(STUFF(STUFF(LASTSCAN.EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME)) / 60.0) AS FLOAT),2) END AS OTWORK_HOURDo not use computations in a declarative language. This is SQL and not COBOL.
Use a table of time slots set to one more decimal second of precision than your data. You can now use temporal math to add it to a DATE to TIME(1) get a full DATETIME2(0). Here is the basic skeleton.
CREATE TABLE Timeslots
(slot_start_time TIME(1) NOT NULL PRIMARY KEY,
slot_end_time TIME(1) NOT NULL,
CHECK (start_time < end_time));
INSERT INTO Timeslots --15 min intervals
VALUES ('00:00:00.0', '00:14:59.9'),
('00:15:00.0', '00:29:59.9'),
('00:30:00.0', '00:44:59.9'),
('00:45:00.0', '01:00:59.9'),
('23:45:00.0', '23:59:59.9');
Here is the basic query for rounding down to a time slot.
SELECT CAST (@in_timestamp AS DATE), T.start_time
FROM Timeslots AS T
WHERE CAST (@in_timestamp AS TIME)
BETWEEN T.slot_start_time
AND T.slot_end_time;
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
How to use XVID MEPG4 DECODER in labview
May i ask how to use Xvid MEPG4 Decoder to compress the AVI file. For this decoder have been installed in my computer but the VI(IMAQ AVI2 Get Codec Names)can not list this code name. So may i ask how can i use this decoder in the labview. For this decoder is relatively has good quality and also small size. Or what kind of other solution can be provieded for the relative small size of AVI file.
Thanks
DanielI believe this was answered already in a different thread: http://forums.ni.com/t5/LabVIEW/LabView-to-grab-and-save-video-to-mpeg/m-p/643864
www.movimed.com - Custom Imaging Solutions -
How to use CASE statement in WDA
Hi All,
Can any one Please expain me ' How to use CASE statement in Web dynpro ABAP? '
Please give me an example also.
Thanks in Advance !Hi,
The usage of case statement in webdynpro is same as used in general ABAP.
Data: l_id type string.
l_id = wdevent->get_string( 'ID' ).
case l_id.
when 'BTN1'.
when 'BTN2'.
when 'OTHERS'.
endcase.
Regards,
Radhika. -
How to use CASE stmt in Where clause
Hi,
How to use CASE stmt in WHERE clause?. I need the code. Please send me as early as possible..........Hi,
1004977 wrote:
Hi,
How to use CASE stmt in WHERE clause?. There's no difference between how a CASE expression is used in a WHERE clause, and how it is used in any other clause. CASE ... END always returns a single scalar value, and it can be used almost anywere a single scalar expression (such as a column, a literal, a single-row function, a + operation, ...) can be used.
CASE expressions aren't needed in WHERE clauses very much. The reason CASE expressions are so useful is that they allow you to do IF-THEN-ELSE logic anywhere in SQL. The WHERE clause already allows you to do IF-THEN-ELSE logic, usually in a more convenient way.
I need the code.So do the peple who want t help you. Post a query where you'd like to use a CASE expression in the WHERE clause. Post CREATE TABLE and INSERT statements for any tables used unless they are commonly available, such as scott.emp). Also, post the results you want from that sample data, and explain how you get those resuts from that data.
See the forum FAQ {message:id=9360002}
Please send me as early as possible..........As mentioned bfore, that's rude. It's also self-defeating. Nobody will refuse to help you because you don't appear pushy enough, but some people will refuse to help you if you appear too pushy. -
How to use "url.openStream()" . What this function does?
how to use "url.openStream()" . What this function does?
Edited by: sahil1287 on Apr 16, 2009 10:02 PMhttp://java.sun.com/javase/6/docs/api/java/net/URL.html#openStream()
http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html -
How to use case when in Select qry?
Hi Friends,
I want to use Case when in Select qry, my situation is like this
SELECT bmatnr blgort bj_3asiz bmat_kdauf b~mat_kdpos
SUM( ( case when bshkzg = 'S' then bmenge else 0 END ) -
( case when bshkzg = 'H' then bmenge else 0 END ) ) AS qty
INTO corresponding fields of table it_projsal
FROM mseg AS b
INNER JOIN mkpf AS a ON bmblnr = amblnr
AND bmandt = amandt
WHERE abudat < '20061201' AND blgort IN ('1050')
and b~mandt = '350'
GROUP BY bmatnr bj_3asiz bmat_kdauf bmat_kdpos b~lgor
If we give like this it gives an error.
Please help me, how to use or handle in select qry itself.
Regards
Shankarthis is not a way to select data from the DB tables.
first get all the data from the DB tables then u have to do SUM Ups .
Regards
prabhu -
How to use, Case function and Filter in Column Formula?
Hello All,
I am using case function and also would like to filter value to populate.
Below is showing error :
case
when '@{Time}' = 'Year' then "Time"."Fiscal Year"
when '@{Time}' = 'Quarter' then "Time"."Fiscal Quarter"
when '@{Time}' = 'Month' then FILTER ("Time"."Fiscal Period" USING "Time"."Fiscal Period" NOT LIKE 'A%')
else ifnull('@{Time}','Selection Failed') end
Thanks, AKwhen '@{Time}' = 'Month' then FILTER ("Time"."Fiscal Period" USING "Time"."Fiscal Period" NOT LIKE 'A%')I dont think Filter this works here or any other data types except number.
Try to use option Column's->Filter->Advanced->Convert this filter to SQL
If helps mark -
I want to count several categories(say a b c d) under same count and some catogories under another count ..there I need to use count with decode pls let me know the syntacs to write my query..this is ergent
regards
BuddhikeSomething like this ?
select count(decode(your_column,'a',1)) as "a",
count(decode(your_column,'b',1)) as "b",
count(decode(your_column,'c',1)) as "c",
count(decode(your_column,'d',1)) as "d"
from your_table;But maybe you can give us more details about what do you expected ?
Nicolas. -
How to use case to replace the & in select statement
Oracle version : 11.1.0.6.0
RHEL
Hi,
I want to get a list of all expired and locked users but want to replace the "&" with and here is my code but it isn't working.
Please some guidance, thanks.
SQL> select username,
2 case (account_status like 'EXP%' then 'EXPIRED AND LOCKED'
3 ELS 'no locked users'
4 end) account_status
5 from dba_users
6 where account_status like 'EXPI%';
case (account_status like 'EXP%' then 'EXPIRED AND LOCKED'
ERROR at line 2:
ORA-00907: missing right parenthesis798188 wrote:
thanks for the comments I corrected the ELSE and 'EPX%' but still same error :
EOH> select username,
2 case (account_status like 'EXP%' then 'EXPIRED AND LOCKED'
3 ELSE 'no locked users'
4 end) account_status
5 from dba_users
6 where account_status like 'EXP%';
case (account_status like 'EXP%' then 'EXPIRED AND LOCKED'
ERROR at line 2:
ORA-00907: missing right parenthesisFirst of all, you do not need a CASE statement.
Did you see the where clause of your query??
6 where account_status like 'EXP%';do you realise that your query will always return those records for which value of account_status column starts with 'EXP'?
Why use CASE when it could be simply written like this :
SELECT username,
'EXPIRED AND LOCKED'
FROM dba_users
WHERE account_status LIKE 'EXP%' -
How to use case when statements in ODI
I need to put conditional logic before DVM look up in ODI. In the expression editor I put the following statement:-
CASE WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='RCS' THEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME=EBIZ_CELL.CELL_DATA
WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='FDC' THEN CONCAT(POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME,POC_JOURNAL_TEMP_SOURCE_TBL.BOOK_CODE)=EBIZ_CELL.CELL_DATA
END
It did not work,
Under Operators ->All Executions, found the error:-
905 : 42000 : java.sql.SQLException: ORA-00905: missing keyword
The description in Session Task contained:-
select
C1_JOURNAL_TEMPL
from APPS.POC_JOURNAL_TEMP_SOURCE_TBL POC_JOURNAL_TEMP_SOURCE_TBL, APPS.C$_0POC_JOURNAL_TEMP_TARGET_TB
where
(1=1)
And (CASE
WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='RCS'
THEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME=C2_CELL_DATA
WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='FDC'
THEN CONCAT(POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME,POC_JOURNAL_TEMP_SOURCE_TBL.BOOK_CODE)=C2_CELL_DATA
END)
Checked the above code in PL/SQL Developer but it gave errors.
In PL/SQL developer tried to check a simple query using case-when but even that is giving errors in the case-when portion.
The query is as follows:-
select phase_code, accounting_period, sum(eff_cc) as BD_Eff_QTD
from prj_detail
where
case POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME
when 'EXT'
then
1
when 'RAC'
then
2
when 'XXX'
then
3
else
end
I would like to know what is wrong with the above code.
Please let me know what is the correct way of using case-when in PL/SQL as well as in ODI.Your ODI case statement and PL/SQL Case statement both looks confusing.
You are writing case statement under where clause which is not a good practise. If you want to implement logic like-
select a,b,c from <table>
where
when cond^n^ 1 then do 1
when cond^n^ 2 then do 2
then better you seperate your query for each filter and do a union, in other words-
select a,b,c from <table>
where cond^n^ 1
union
select a,b,c from <table>
where cond^n^ 2
If you are writing case staement to retrieve a value/column (EBIZ_CELL.CELL_DATA) then no need to include it under filter.
ODI case for column EBIZ_CELL.CELL_DATA will be:
CASE
WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='RCS'
THEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME
WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='FDC'
THEN CONCAT(POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME,POC_JOURNAL_TEMP_SOURCE_TBL.BOOK_CODE)
END
Pl/SQL query-
select phase_code, accounting_period,
case POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME
when 'EXT'then 1
when 'RAC' then 2
when 'XXX' then 3
else 'default value' --- (if no else needed then can also remove else part)
end,
sum(eff_cc) as BD_Eff_QTD
from prj_detail
Suggested as per what is understood, hope it helps.
Edited by: 939451 on Jul 5, 2012 12:47 AM
Edited by: 939451 on Jul 5, 2012 12:48 AM -
How to use logic:interate tag in this case? thanks a lot.
a javabean:
a{
int a;
ArrayList bList (to store some strings);
request.setAttribute("a",a);
then how can I use <logic:iterate> tag to loop to use <bean:write> tag to write out the strings contained by bList?use getparams and get that attribute(arraylist)
define a bean in jsp with property as this attribute for which u should have a getter method in context
the following is to iterate through arralist of arraylist
<logic:iterate id="accList" name="<%=subAppContextName%>" ="accountsHoldedList">
<tr>
<logic:iterate id="accList2" name="accList">
<td> <bean:write name="accList2"/> </td>
</logic:iterate>
<td>
<jfp:link styleClass="appNavNext" warn="false" bundle="<%=bundleName%>" key="GiveNotice" paramId="selectedAccount" paramName="accList2" href="javascript:submitMyForm();"></jfp:link>
</td>
</tr>
</logic:iterate>
accountsHoldedList is the one which is set in Context and i am iterating to display it.
bye -
How to use case for this query.
I have this table name Employee.
columns..
EmpId,
EmpType,
Joining date,
Resigning date
Now in EmpType either it can be 'P' OR 'C'
so i want to check if the EmpTYpe is 'P' then joiningdate should not be null
and if the EmpType is 'C' then Resigning date should not be null.
Then only want those records from employee.
Please help.978172 wrote:
I have this table name Employee.
columns..
EmpId,
EmpType,
Joining date,
Resigning date
Now in EmpType either it can be 'P' OR 'C'
so i want to check if the EmpTYpe is 'P' then joiningdate should not be null
and if the EmpType is 'C' then Resigning date should not be null.
Then only want those records from employee.
Please help.
-- EmpId, EmpType, Joining date, Resigning date
select * from Employee
where (EmpType = 'P' and JoiningDate IS NULL) OR (EmpType = 'C' and ResigningDate IS NULL);
Maybe you are looking for
-
Hi i'm in a bad place right now,my car watch and iphone 5s have just been stolen,my phone is reading 'offline'on find my phone, i was wondering if there was any way to locate the phone while offline?,any help would be very much appreciated.thanks in
-
HT4059 How do I get my page on iBook to take the full page
My book in iBook has a few sentences on the top and bottom of the page that is cut off
-
Problems with Transfering Custom Generic List using WebServices
Friends: I need your help. I have a class "ListaModelos" public class ListaModelos<T> extends ArrayList<T>{ private List<T> eliminados; public ListaModelos() super(); eliminados = new ArrayList<T>(); @Override public b
-
Hi, I'm running Arch on an Asus UX32LN and I've noticed that since my installation of Arch, my battery time has been reduced by 2-3 hours compared to when I was running Windows 8.1. What's the cause of this? Can I diagnose it somehow? I've lowered my
-
Query to see Material movements in MM (MSEG) + storage type from WM (LTAP)
Hallo, I tried transaction MCLH, but the transaction gives to informations. I think that the infosystem is not completely builded up. So I have do create a new Z-Report to get the an query so see Material movements in MM (MSEG) with the storage type