Case stament use in where clause grive error
i want to use this conditions but gives error. Please give me the solution of it
WHERE
CASE
WHEN CANCELLED_DATE IS NULL THEN
TO_DATE(INV_GL_DATE,'DD-MON-YY') <= TO_DATE (:P_DATE1,'DD-MON-YY')
ELSE
TO_DATE(INV_GL_DATE,'DD-MON-YY') >= TO_DATE(:P_DATE1,'DD-MON-YY')
END) --- <= TO_DATE(:P_DATE1,'DD-MON-YY')
Hi,
Remember that a CASE expression always evaluates to a single value in one of the SQL datatypes, such as VARCHAR2, NUMBER or DATE. (There is no Boolean datatype in SQL.)
You can't say
WHERE CASE ... END;for the same reason that you can't say
WHERE 'Hello, world!';orWHERE 100;or
WHERE SYSDATE;CASE expressions are really valuable because they allow you do perform IF-THEN-ELSE logic in places where you normally can't, like the SELECT clause.
The WHERE clause is a place where you can perform IF-THEN-ELSE logic anyway, so there's rarely a need for a CASE expression in a WHERE clause. It's just as efficient and just as clear (if not more so) to put all your conditions directly in the WHERE clause, like Max demonstrated.
Edited by: Frank Kulash on Feb 26, 2010 10:13 AM
user11995078 wrote:
Thanks Dear But how can we use with case and decodeWhy do you want to?
CASE does not help in this problem, any more than regular expressions or CONNECT BY or MODEL help. They are all great tools for particular jobs, but not for this job. Asking "How can I use CASE to do this?" Is like asking "How can I use a hammer to tighten a bolt?" Perhaps there is a way, but it's likely to be contrived and ridiculous.
Here's the least ridiculous way I can think of:
WHERE ( TO_DATE (INV_GL_DATE, 'DD-MON-YY')
- TO_DATE (:P_DATE1, 'DD-MON-YY')
) * CASE
WHEN cancelled_date IS NULL THEN 1
` ELSE -1
END <= 0
Similar Messages
-
Query Tuning - using CASE statement in the WHERE clause
Hi All,
My query has been modified to use a CASE statement in the WHERE clause to consider data from certain columns based on a parameter value. This modified query is doing a full table scan and running endlessly. Please suggest what may be done to improve its performance:
Query:
SELECT LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))) AS summary_date,
os.acctnum,
os.avieworigin_refid,
COUNT(1) cnt_articleview,
SUM(NVL(autocompletedterm,0)) cnt_autocompletedterm
FROM TABLE1 os
WHERE os.acctnum IS NOT NULL
AND os.avieworigin_refid IS NOT NULL
AND os.requestdatetime IS NOT NULL
AND UPPER(os.success_ind) = 'S'
AND CASE WHEN
Param_ValueToCheck = 'FULL' AND get_date_timestamp(os.requestdatetime) BETWEEN
TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
THEN 1
WHEN
Param_ValueToCheck = 'INCR' AND os.entry_createddate BETWEEN
TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
THEN 1
END = 1
AND CASE WHEN
Param_ValueToCheck = 'FULL' AND os.entry_CreatedDate BETWEEN
TO_DATE('01-APR-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
TO_DATE('07-JUN-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
THEN 1
WHEN
Param_ValueToCheck = 'INCR' THEN 1
END = 1
GROUP BY LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))), os.acctnum,os.avieworigin_refid;Table Description:
(Number of rows : approx > 600,000,000)
Name Null Type
ARTICLEID NOT NULL NUMBER(20)
USERKEY NUMBER(10)
AVIEWORIGIN_REFID VARCHAR2(10)
SUCCESS_IND VARCHAR2(2)
ENTRY_CREATEDDATE DATE
CREATED_BY VARCHAR2(10)
FILENUMBER NUMBER(10)
LINENUMBER NUMBER(10)
ACCTNUM VARCHAR2(10)
AUTOCOMPLETEDTERM NUMBER(2)
REQUESTDATETIME VARCHAR2(19)Explain Plan
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
Plan hash value: 2224314832
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 590 | 33040 | 2501K (1)| 08:20:15 | | |
| 1 | HASH GROUP BY | | 590 | 33040 | 2501K (1)| 08:20:15 | | |
| 2 | PARTITION RANGE ALL| | 590 | 33040 | 2501K (1)| 08:20:15 | 1 |1048575|
|* 3 | TABLE ACCESS FULL | TABLE1 | 590 | 33040 | 2501K (1)| 08:20:15 | 1 |1048575|
PLAN_TABLE_OUTPUT
Predicate Information (identified by operation id):
3 - filter(UPPER("OS"."SUCCESS_IND")='S' AND CASE WHEN ('FULL'='FULL' AND
"OS"."ENTRY_CREATEDDATE">=TO_DATE(' 2011-04-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
"OS"."ENTRY_CREATEDDATE"<=TO_DATE(' 2011-06-07 00:00:00', 'syyyy-mm-dd hh24:mi:ss')) THEN 1 WHEN
'FULL'='INCR' THEN 1 END =1 AND "OS"."REQUESTDATETIME" IS NOT NULL AND CASE WHEN ('FULL'='FULL'
AND "ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")>=TO_DATE(' 2011-05-01 00:00:00', 'syyyy-mm-dd
hh24:mi:ss') AND "ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")<=TO_DATE(' 2011-05-31 00:00:00',
'syyyy-mm-dd hh24:mi:ss')) THEN 1 WHEN ('FULL'='INCR' AND "OS"."ENTRY_CREATEDDATE">=TO_DATE('
2011-05-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "OS"."ENTRY_CREATEDDATE"<=TO_DATE('
PLAN_TABLE_OUTPUT
2011-05-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss')) THEN 1 END =1 AND "OS"."ACCTNUM" IS NOT NULL AND
"OS"."AVIEWORIGIN_REFID" IS NOT NULL)Edited by: Chaitanya on Jun 9, 2011 2:44 AM
Edited by: Chaitanya on Jun 9, 2011 2:47 AMHi Dom,
Modified Query:
SELECT LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))) AS summary_date,
os.acctnum,
os.avieworigin_refid,
COUNT(1) cnt_articleview,
SUM(NVL(autocompletedterm,0)) cnt_autocompletedterm
FROM TABLE1 os
WHERE os.acctnum IS NOT NULL
AND os.avieworigin_refid IS NOT NULL
AND os.requestdatetime IS NOT NULL
AND UPPER(os.success_ind) = 'S'
AND (('FULL' = 'FULL'
AND (get_date_timestamp(os.requestdatetime) BETWEEN TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND os.entry_CreatedDate BETWEEN TO_DATE('01-APR-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND TO_DATE('07-JUN-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
OR ('FULL' = 'INCR'
AND os.entry_createddate BETWEEN TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') ))
GROUP BY LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))), os.acctnum,os.avieworigin_refid;Execute Plan:
PLAN_TABLE_OUTPUT
Plan hash value: 3615447714
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 25125 | 1374K| | 407K (1)| 01:21:36 | | |
| 1 | HASH GROUP BY | | 25125 | 1374K| 3768K| 407K (1)| 01:21:36 | | |
| 2 | PARTITION RANGE ITERATOR| | 25125 | 1374K| | 407K (1)| 01:21:32 | 29 | 31 |
|* 3 | TABLE ACCESS FULL | TABLE1 | 25125 | 1374K| | 407K (1)| 01:21:32 | 29 | 31 |
PLAN_TABLE_OUTPUT
Predicate Information (identified by operation id):
3 - filter("OS"."ENTRY_CREATEDDATE">=TO_DATE(' 2011-04-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
UPPER("OS"."SUCCESS_IND")='S' AND "OS"."REQUESTDATETIME" IS NOT NULL AND
"ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")>=TO_DATE(' 2011-05-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
"ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")<=TO_DATE(' 2011-05-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
"OS"."ACCTNUM" IS NOT NULL AND "OS"."AVIEWORIGIN_REFID" IS NOT NULL AND "OS"."ENTRY_CREATEDDATE"<=TO_DATE('
2011-06-07 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))Edited by: Chaitanya on Jun 9, 2011 4:51 AM -
Query Tuning - using CASE statement in the WHERE clause - Duplicate Post
Duplicate Post by mistake.
Please check
Query Tuning - using CASE statement in the WHERE clause
Edited by: Chaitanya on Jun 9, 2011 2:45 AM
Edited by: Chaitanya on Jun 9, 2011 2:46 AMDuplicate Post by mistake.
Please check
Query Tuning - using CASE statement in the WHERE clause
Edited by: Chaitanya on Jun 9, 2011 2:45 AM
Edited by: Chaitanya on Jun 9, 2011 2:46 AM -
Using hierarchy attribute in where clause! ERROR
Hi,
I'm currently working on a report where I'm using a where clause in my MDX querry on the "ID" hierarchy, but also would like to show the ID in the report (table/matrix) in the ON COLUMNS part.
As I read it is not possible to use a slicer in an axis hierarchy, so I used a workaround by putting the lower granularity (attribute) in the where clause. This workaround works in my MDX query BUT does not work when I convert the querry for
my reporting! I get the same error again
ON COLUMNS, NON EMPTY
Filter(
NonEmpty(
[Wager].[Wager number].[Wager
number],
[Measures].[Payment amount]
* [Date].[Date].[Date]
[Measures].[Payment amount] > Cdbl(@PAR_Payment_Amount)
* [Point of sale].[POS default connection].[POS default connection]
WHERE
[Point of sale].[Regional office].&[22]
* {[Date].[Calendar - Bonus Program].[Invoice week].&[2014]&[06],[Date].[Calendar - Bonus Program].[Invoice week].&[2014]&[07]}
* {[Point of sale].&[10005397],[Point of sale].&[10005397]}
--> In my report MDQ query designer
WHERE
{StrToMember(@PAR_Regional_Office)}
* StrToSet(@PAR_Year_Week)
* {StrToSet(@PAR_ClientID)}
Any suggestions?Still looking for help if possible:)
-
How do you use 3 Where Clauses in a query
Hi, i am trying to figure out how to use 3 Where Clauses in a Query where 2 of the Where Clauses uses a Sub query.
Display the OrderID of all orders that where placed after all orders placed by “Bottom-Dollar Markets”.
Order the result by OrderID in ascending order.
First WHERE clause checks for OrderDate and uses a sub query with ALL keyword.
Second WHERE clause use equals and sub query.
Third WHERE clause uses equal and company name.
This is what i have so far but i am pretty confused on how to do this.
My Code for NorthWind:
Select OrderID
From Orders o
Where o.OrderID IN (Select OrderDate From Orders Where Orders.OrderID > ALL
(Select CompanyName From Customers Where CompanyName = 'Bottom-Dollar Markets'));
The book shows how to use the ALL Keyword but not in a Sub query with Multiple Where Clauses.
Select VenderName, InvoiceNumber, InvoiceTotal
FROM Invoices JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
WHERE InvoiceTotal > ALL (Select InvoiceTotal From Invoices Where VendorID = 34)
ORDER BY VendorName;>Where Orders.OrderDate
> ALL (Select
CompanyName
The comparison operator (>) requires compatible data types.
DATETIME is not compatible with VARCHAR string for comparison.
Here is your homework:
SELECT orderid
FROM orders o
WHERE o.orderdate > ALL (SELECT orderdate
FROM orders
WHERE shipvia = (SELECT Max(shipvia)
FROM orders o
INNER JOIN customers c
ON c.customerid =
o.customerid
WHERE
c.companyname = 'Bottom-Dollar Markets'));
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
Kalman Toth Database & OLAP Architect
SQL Server 2014 Database Design
New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014 -
What happens u0093Updateu0094 command is used without where clause ?
Hi
What happens Update command is used without where clause ?
thank youHi subash,
chk this help
UPDATE dbtab SET f1 ... fn. or
UPDATE (dbtabname) SET f1 ... fn.
Extras:
1. ... WHERE condition
2. ... CLIENT SPECIFIED
3. ... CONNECTION con
Effect
Updates values in a database table. <b>If there is no WHERE clause, all lines (in the current client) are updated.</b> If a WHERE condition is specified, only thoserecords which satisfy the WHERE condition are updated. -
Trying to use a where clause from a table
hi,
I have a report and I need to use a where clause that is archived in a table, it is a different one depending on the row (colour=blue or number = 88..) I have tryied with ref cursor, dinamis sql, functions, packages and could not get it, any suggestion??
thanksYou may use Lexical Rerefences: "Lexical references are placeholders for text that you embed in a SELECT statement. You can use lexical references to replace the clauses appearing after SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, CONNECT BY, and START WITH....."
-
REP 300 error when using dynamic where clause
Hi,
I am developing a report and I need a dynamic where clause.The code I have written in the where clause is as below
function where_clauseFormula return VARCHAR2 is
begin
if :P_PO_REQD = 'No' then
return ('and pha.segment1 is null');
elsif :P_PO_REQD = 'Yes' then
return('and pha.segment1 between NVL(:p_po_num_from,1) and NVL(:p_po_num_to,99999999999999999)');
else return NULL;
end if;
end;
Now if i directly use either of the conditions in my report query it works fine.But if the parameter is No then the where clause does not work.I have also tried different statements in the if clause of the 'No' part and they work.For example the following code works when :P_PO_REQD is 'No' :
function where_clauseFormula return VARCHAR2 is
begin
if :P_PO_REQD = 'No' then
return ('and pha.segment1 between NVL(:p_po_num_from,1) and NVL(:p_po_num_to,99999999999999999)');
elsif :P_PO_REQD = 'Yes' then
return('and pha.segment1 between NVL(:p_po_num_from,1) and NVL(:p_po_num_to,99999999999999999)');
else return NULL;
end if;
end;
I don't know why this is happening.I really need a solution or a workaround urgently
Thanks
MukundYou may use Lexical Reference for the dynamic where. In After Parameter Form Trigger, code....
if :P_PO_REQD = 'No' then
:P_where := :P_where || 'and pha.segment1 is null';
elsif :P_PO_REQD = 'Yes' then
:P_where := :P_where || 'and pha.segment1 between NVL(:p_po_num_from,1) and NVL(:p_po_num_to,99999999999999999)';
end if;
.......... -
Using a WHERE clause in APEX Tree
Hi All -
I have an hierarchical SQL query that I'm displaying as an APEX tree.
My sample app is here:
https://apex.oracle.com/pls/apex/f?p=32581:29
login: guest
pw: app_1000
workspace: leppard
I am trying to add a WHERE clause so that only nodes with the lowest level children are displayed, i.e. something like 'WHERE connect_by_isleaf = 0 OR level = 5'
The tree query with the where clause works fine in the SQL commands window, but when I add the WHERE clause to my apex tree the page no longer displays anything. Is this an issue with APEX or is there some other way to filter my results?
Thanks in advance for any suggestions,
johnConnect by occurs first, the where clause then is applied to those results, effectively cutting away in the hierarchical structure. Since apex has to build a hierarchical structure from the query it relies on the level pseudocolumn, which is butchered by applying the where clause. It's a miracle you aren't even receiving errors because I'd almost expect an incorrect json array to have been built. With no top level to start from and only level 5 or leaf nodes there is no structure to present: both apex can not put out a correct representation and not jstree either. In your query you will see your "root nodes" but it is not representable.
I'm not sure why you want to present this in a tree? Both connect_by_is_leaf and level = 5 will simply give you a list of nodes without any hierarchical structure.
The better thing to do is to use a subquery to first limit your dataset and then use that to base the tree query on, that way you don't break any of those vital columns.
Eg if you only want leaf nodes and their immediate parent you could go for something like this (quick mockup on some testdata):
with dataset as (
select node_id, parent_id
from treedata
where connect_by_isleaf = 0
connect by prior node_id = parent_id
start with parent_id = 0
dataset2 as (
select node_id, parent_id
from dataset
union all
select node_id, null parent_id
from treedata
where node_id in (select parent_id from dataset)
select level, node_id
from dataset2
connect by prior node_id = parent_id
start with parent_id is null -
We have tables that are interval range partitioned on a DATE column, with a partition for each day - all very standard and straight out of Oracle doc.
A 3rd party application queries the tables to find number of rows based on date range that is on the column used for the partition key.
This application uses date range specified relative to current date - i.e. for last two days would be "..startdate > SYSDATE -2 " - but partition pruning does not take place and the explain plan shows that every partition is included.
By presenting the query using the date in a variable partition pruning does table place, and query obviously performs much better.
DB is 11.2.0.3 on RHEL6, and default parameters set - i.e. nothing changed that would influence optimizer behavior to something unusual.
I can't work out why this would be so. It very easy to reproduce with simple test case below.
I'd be very interested to hear any thoughts on why it is this way and whether anything can be done to permit the partition pruning to work with a query including SYSDATE as it would be difficult to get the application code changed.
Furthermore to make a case to change the code I would need an explanation of why querying using SYSDATE is not good practice, and I don't know of any such information.
1) Create simple partitioned table
CREATETABLE part_test
(id NUMBER NOT NULL,
starttime DATE NOT NULL,
CONSTRAINT pk_part_test PRIMARY KEY (id))
PARTITION BY RANGE (starttime) INTERVAL (NUMTODSINTERVAL(1,'day')) (PARTITION p0 VALUES LESS THAN (TO_DATE('01-01-2013','DD-MM-YYYY')));
2) Populate table 1million rows spread between 10 partitions
BEGIN
FOR i IN 1..1000000
LOOP
INSERT INTO part_test (id, starttime) VALUES (i, SYSDATE - DBMS_RANDOM.value(low => 1, high => 10));
END LOOP;
END;
EXEC dbms_stats.gather_table_stats('SUPER_CONF','PART_TEST');
3) Query the Table for data from last 2 days using SYSDATE in clause
EXPLAIN PLAN FOR
SELECT count(*)
FROM part_test
WHERE starttime >= SYSDATE - 2;
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 1 | 8 | 7895 (1)| 00:00:01 | | |
| 1 | SORT AGGREGATE | | 1 | 8 | | | | |
| 2 | PARTITION RANGE ITERATOR| | 111K| 867K| 7895 (1)| 00:00:01 | KEY |1048575|
|* 3 | TABLE ACCESS FULL | PART_TEST | 111K| 867K| 7895 (1)| 00:00:01 | KEY |1048575|
Predicate Information (identified by operation id):
3 - filter("STARTTIME">=SYSDATE@!-2)
4) Now do the same query but with SYSDATE - 2 presented as a literal value.
This query returns the same answer but very different cost.
EXPLAIN PLAN FOR
SELECT count(*)
FROM part_test
WHERE starttime >= (to_date('23122013:0950','DDMMYYYY:HH24MI'))-2;
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 1 | 8 | 131 (0)| 00:00:01 | | |
| 1 | SORT AGGREGATE | | 1 | 8 | | | | |
| 2 | PARTITION RANGE ITERATOR| | 111K| 867K| 131 (0)| 00:00:01 | 356 |1048575|
|* 3 | TABLE ACCESS FULL | PART_TEST | 111K| 867K| 131 (0)| 00:00:01 | 356 |1048575|
Predicate Information (identified by operation id):
3 - filter("STARTTIME">=TO_DATE(' 2013-12-21 09:50:00', 'syyyy-mm-dd hh24:mi:ss'))
thanks in anticipation
JimAs Jonathan has already pointed out there are situations where the CBO knows that partition pruning will occur but is unable to identify those partitions at parse time. The CBO will then use a dynamic pruning which means determine the partitions to eliminate dynamically at run time. This is why you see the KEY information instead of a known partition number. This is to occur mainly when you compare a function to your partition key i.e. where partition_key = function. And SYSDATE is a function. For the other bizarre PSTOP number (1048575) see this blog
http://hourim.wordpress.com/2013/11/08/interval-partitioning-and-pstop-in-execution-plan/
Best regards
Mohamed Houri -
Tree issue using a where clause
Tree created
select "ID" id,
"PARENT_ID" pid,
"TITLE" name,
"LINK" link,
null a1,
null a2
from apexim.TREE_NAV
I am attempting to alter this tree this way
select "ID" id,
"PARENT_ID" pid,
"TITLE" name,
"LINK" link,
null a1,
null a2
from apexim.TREE_NAV
WHERE INSTR(PAGE_ID_1,:app_page_id) > 0
This returns rows in PL/SQL however does not return tree values to the page.
this throw the error
Warning: Tree root ID "1" not found.
When I change the where clause to:
where 1=1
I get all the enitire tree as i would expect.
We are creating a report delevery application that will allow people to see specific leaves passed on authority level.
The Tree_nav table ha the url for the page to display and has the page_id_1 for the portal that launches specific content page.
So ultimatly I will include the following into the where clause
(and authority_level <= :p100_auth_level)
I have tried several things and am at wits end.rbackmann wrote:
And the page_id_1 is a field in the table (SORRY EXAMPLE BAD NAMING CONVENTIONS) that IS VARCHAR
and houses a comma seperated list of page numbers ie 28,29, 30Okay, I was wrong about the field name :(
Carefully examine the query, expecially the INSTR line. Get the values in both the column and the bind variable and make sure they are what you think they are.
The example on the other post might have failed because you were equality comparing the column value to '28' when it might have been a csv with commas unless you changed it (something worth trying).
On 4th look I may (or may not) have something. PAGE_ID_1 is a VARCHAR2, while the 28 you compared it to was a number. Its a long shot, but maybe SQL is trying to convert the csv field to a number, failing to raise an error message where you can find it, and failing for that reason. Try using '28' on the equality test.
Edited by: riedelme on May 8, 2009 6:03 AM -
Case Statement in a Where clause help
Oracle Database 11g Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
"CORE 11.2.0.1.0 Production"
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
Hello,
I have an APEX application that I need to build a SQL statement for a LOV (List of Values). I have a hidden filed that contains the customer type which can be an 'R' or 'B'. The query needs to be able to display two different result sets based on the customer type of 'R' or 'B'.
If the customer type is 'R' then:
SELECT drg_descr d, drg_code r
FROM distance_ranges
WHERE drg_min_miles IN (0,5)
ORDER BY drg_min_milesIf the customer type is 'B' then:
SELECT drg_descr d, drg_code r
FROM distance_ranges
WHERE drg_min_miles IN (0,5,10,15,20)
ORDER BY drg_min_milesCan someone help me with what I think needs to be a case statement?
Thanks,
JoeHi,
You can try CASE statement with WHERE clause
SELECT drg_descr d, drg_code r
FROM distance_ranges
WHERE (CASE param_cust_type
WHEN(param_cust_type='R') THEN (drg_min_miles IN (0, 5)
WHEN (param_cust_type='B') THEN (drg_min_miles IN (0,5,10,15,20)
END;
Please try and let me know if anything wrong.
Anyone from the forum comment my code if there is any wrong.
Thanks!
Naresh -
hi,
can we use output of a function in where clause directly ?
i.e
select node(condition1) clause from dual;
clause
occupation='SALARY'
then i want to use this output i.e clause string directly in select stmt.
select * from abc_table where clause
more strictly
select * from abc_table where (select node(condition1) clause from dual)
which is interpreted as
select * from abc_table where occupation='SALARY'
Is there any way to use functions in clause area ???
I tried CURSOR but not workd for this
Thanks in advance,
RupI cant really understand your problem
--fn1 is a function
sql>
select fn1
from dual;
FN1
CLERK
sql>
select * from emp
where job = fn1;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10
Message was edited by:
jeneesh -
Define a Standalone Download Integrator with an Additional Where clause - SQL error
Hi,
I am trying to create a standalone integrator with an Additional Where clause. Followed the steps mentioned in the metalink note 360105.1. When I run the integrator, I get the following error message.
Please resolve the following error to continue.
SQL error: ORA-00936: missing expression occurred
processing stored SQL for Content 20007:GENERAL_441_CNT.
When I look at the log file, here is what I can see:
10/22/14 10:50 AM AJPRequestHandler-HTTPThreadGroup-15 TRACE BneHrSQLControl - SQL:SELECT FROM PER_PEOPLE_V where last_name=$PARAM$.last_name
10/22/14 10:50 AM AJPRequestHandler-HTTPThreadGroup-15 TRACE validateSQLStatement value of sqlStatement is: SELECT FROM PER_PEOPLE_V where last_name=:RSQLP1
10/22/14 10:50 AM AJPRequestHandler-HTTPThreadGroup-15 DETAIL BneHrSQLControl.validateSQLStatement() paramName: last_name
10/22/14 10:50 AM AJPRequestHandler-HTTPThreadGroup-15 DETAIL BneHrSQLControl.validateSQLStatement() error in SQL: java.sql.SQLSyntaxErrorException: ORA-00936: missing expression
So, clearly, the SQL statement that is getting built internally is wrong. Ideally, I would expect the statement to be something like this:
SELECT * FROM PER_PEOPLE_V where last_name=$PARAM$.last_name.
RDBMS : 11.2.0.3.0
Oracle Applications : 12.1.3
Any pointers towards the resolution of the issue would be of great help.
Thanks in Advance!
Lalitha.This is a duplicate post of your other question: create trigger automatic
DO NOT start duplicate threads for the same question.
Locking this thread -
Can pipelined functions' return values be used in WHERE clause?
If I have function MY_FUNC that returns a REFCURSOR with columns COL1, COL2, COL3
can I use the values returned in the output cursor in my WHERE clause as well as in the SELECT clause?
e.g.
SELECT COL1, COL2, COL3
FROM TABLE(MY_FUNC(param1, param2))
WHERE COL1 = 24 AND COL2=25
Would that be proper SQL?Hi,
SQL> Create OR Replace Package Pkg_Test_ Is
2
3 Type my_typ Is Table Of Number;
4
5 Function fnc_test Return my_typ Pipelined;
6
7 End;
8 /
Package created
SQL> Create OR Replace Package Body Pkg_Test_ Is
2
3 Function fnc_test Return my_typ
4 Pipelined Is
5 va_typ my_typ := my_typ();
6 Begin
7 For i IN 1 .. 10 Loop
8 va_typ.Extend;
9 va_typ(va_typ.Count) := i;
10 Pipe Row(va_typ(va_typ.Count));
11 End Loop;
12 Return;
13 End;
14
15 End;
16 /
Package body created
SQL> SELECT *
2 FROM Table(PKG_TEST_.FNC_TEST)
3 WHERE COLUMN_VALUE > 5
4 /
COLUMN_VALUE
6
7
8
9
10Regards,
Christian Balz
Maybe you are looking for
-
BEx query based on virtual cube donu00B4t display a valid List of Value (LOV)
Hello I have a problem with an invalid LOV. The scenario is the following; There´s a BEx query based on a virtual cube. The query has an exit variable on caracteristic that is based on 0CALMONTH. At Universe Designer I simply create a connection, a u
-
Photosmart 8700 not printing Black & colours are washed out
Have an A3 Photosmart 8700 printer which has for no reason started to print in muted / knocked back colour and a solid black is now printing out as a washed out grey. I've cleaned the printheads at all levels, tried a new cartridge, all of the normal
-
IPhoto 8 Photos Corrupted or Disappeared
Using iPhoto 7.0.2 (341) I've now had two separate iPhoto libraries where photos were either corrupted or lost. In the first instance I noticed a problem trying to do a Retrospect Back-up and the procedure simply hung the program. Then when I tried t
-
Hello, I am getting ready to purchase a new iMac and a new Apple laptop (I am still unsure which model). Because of this, I know that I will be needing to migrate everything that I have from my current 15 inch Macbook Pro, which is operating Tiger 10
-
When exiting firefox the GUI shuts down but firefox.exe is sill running
Firefox starts and runs fine, but when time to exit, the GUI shuts down while firefox.exe is still running using 50% of possessor resources. I have to go into Windows Task Manager under processes and end program from there. Other wise firefox.exe wil