Where Clause condition - using CASES in which clause
I generate data based on certain criteria of dates as below:
I need to create a report based on the CLOSE_DATE COLUMN,
If SYSDATE < 15 of the month can generate report on CLOSE_DATE between the 1st to the 15th of the current month
If SYSDATE > 15th of the month and then generate report on CLOSE_DATE between the 16th of this month and the last day of the next month.
I tried, but could not get to the query
SELECT scheme_code, MODEL_NAME, close_date
Plans OF
WHERE)
BOX WHEN to_number (to_char(close_date,'DD')) > 15
THEN close_date BETWEEN trunc (trunc (SYSDATE, 'MM') + 15) AND last_day (add_months(SYSDATE,1))
WHEN to_number (to_char(close_date,'DD')) < 15
THEN close_date BETWEEN trunc(SYSDATE,'MM') AND trunc (trunc(SYSDATE,'MM') + 15)
(END);
where nearby date between the two cases when to_number (to_char (close_date, 'DD') > 15 then... else... end and case... when... then... else... end would be the right structure)
in other words, you can use cases coming with each of these two values for him between the comparison. Do not use case to inject arbitrary clauses
Tags: Database
Similar Questions
-
Case where the Condition when Clause Condition use
Hello
How can I use case in the where condition and in when I need to use a condition, be clearly
I need to get the following
When I run the present, I'm missing keyword error.and (case WHEN :PROMT = 'Closed' then status_date >= trunc(sysdate)) end)
How can I solve this?I think you can do it just like this:
... and ( :PROMPT != 'Closed' or status_date >= trunc(sysdate) ) ...
-
Using a CASE statement in 'IN' where clause clause
Hello
I have a form that users see after they connect with a few items and they type or select values based on the element type. All reports in the application use these values of PAGE 1 in where clause to filter the lines.
One of these question is "Quarter". I have a group of radio buttons on the PAGE 1 on the P1_QTR point.
I need to provide users a way to take the last 4, last 3, last 2 or current quarter and I need to dynamically build a clause WHERE the interactive report based on the selection in the form.
I am trying to use the CASE as shown below and get the syntax error.
AND QTR to (when BOX: P1_QTR = 'CURRENT' then ("T4"))
When: P1_QTR = "LAST" then ("Q3")
When: P1_QTR = 'LAST TWO' then ('Q3, "Q4")
When: P1_QTR = 'LAST THREE' then ("Q2", "Q3,' T4 ')
another ("T1", "T2", "Q3,' T4 ')
END)
How can I achieve this?
Following a simple logic, try
and QTR to
(
Select 'T4' double where: P1_QTR <> 'LAST '.
Union of all the
Select 'Q3' to double where: P1_QTR <> "in PROCESS".
Union of all the
Select "Q2" double where: P1_QTR not in ('CURRENT', 'LAST', "LAST TWO")
Union of all the
Select "Q1" double where: P1_QTR not in ('CURRENT', 'LAST', 'LAST TWO', 'THREE LATEST')
)
-
Version of DB: database Oracle 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production
I have a table my_table as below:
create table my_table
(
my_code varchar2 (6).
my_id varchar2 (24).
forced pk_1 primary key (my_code, my_id)
);
Primary_key here's a composite key that contains columns 1 (my_code) and 2 (my_id).
Is there that a difference in the way below queries is executed in terms of performance (use of indexing in the extraction).(a) select * from my_table where my_code = '123' and my_id = "456";
(b) select * from my_table where my_id = '456' and my_code = '123';
The understanding I have the order of the column in the where clause should be identical to the sequence in
What primary key draws for indexing to be used by oracle in oracle other DML queries ignores indexing
However when I used explain plain both show the same query cost with single scan with index rowid.
so I don't know if I'm wrong in the concept that I have. Kindly help.Thanks in advance,
GerardYour question is answered in the Performance Tuning Guide
14.1.4 choosing composite indexes
A composite index contains several key columns. Composite indexes can provide additional benefits compared to the index to single column:
- Improved selectivity
Sometimes the two or more columns or expressions, each with a low selectivity can be combined to form a composite with a high selectivity.
- Reduced IO
If all columns selected by a query are a composite index, then Oracle may return these values in the index without access to the table.
A SQL statement can use a path on a composite index if the statement contains constructions that use a main part of the index.
Note:
This is no longer the case with the skip index scans. See "Index Skip Scans".
A main part of an index is a set of one or more columns that have been specified first and consecutively in the list of columns in the
CREATE
INDEX
statement that created the index. - Improved selectivity
-
No output for report of XML editor using CASE/DECODE in a Where Clause
Hello
I have a requirement of the company to modify an existing report that has two input parameters,
-> p_statcode (closed status) which may have values "Y" or "n".
-> p_overdue (flag late), which can have values of "Y" or "n".
The flag late is a column evaluated with O/N values and it is evaluated as follows,
The requirement of user now is that they must be a third option for setting called p_overdue ALL,ONTF_MOD_VAL(NVL ( (TRUNC (SYSDATE) - (TO_DATE (oe_order_lines.attribute18, 'DD-MON-RRRR') + TO_NUMBER (fnd_lookup_values.meaning))), 0 )) overdue_flag
which by the way the output should include records with
p_statcode is p_statcode ELSE is N AND Y OR p_overdue p_overdue is N
In other words records with raising the Y and N for flag late must be returned regardless of the value given to the closed state.
Original where clause contained in the data definition file is as follows,
My modified code is the following,WHERE Closed_Status = nvl(:p_statcode,Closed_Status) AND overdue_flag = nvl(:p_overdue,overdue_flag)
ORWHERE Closed_Status = NVL (:p_statcode, Closed_Status) AND overdue_flag = (CASE WHEN :p_overdue = 'Y' THEN 'Y' WHEN :p_overdue = 'N' THEN 'N' ELSE overdue_flag END)
Both approaches have the same problem.WHERE Closed_Status = NVL (:p_statcode, Closed_Status) AND overdue_flag = DECODE (:p_overdue, 'Y', 'Y', 'N', 'N',overdue_flag)
The output is in EXCEL format. The modified query works very well for p_overdue as Y or N but when p_overdue is passed as ALL he returned a blank EXCEL sheet with just exit report column headings.
Any help regarding, why is this the case? What's not in my approach?
Kind regards
Vishalunclear on p_overdue = ALL
P_overdue = ALL on what necessary values?try sth like
WHERE Closed_Status = NVL (:p_statcode, Closed_Status) AND ( overdue_flag = DECODE (:p_overdue, 'Y', 'Y', 'N', 'N',overdue_flag) and :p_overdue != 'ALL' or :p_overdue = 'ALL' and (overdue_flag = 'Y' or overdue_flag = 'N') )
for overdue_flag, which has more then 'Y' values of "n".
If overdue_flag that in ('Y', ' don't) thenWHERE Closed_Status = NVL (:p_statcode, Closed_Status) AND ( overdue_flag = DECODE (:p_overdue, 'Y', 'Y', 'N', 'N',overdue_flag) and :p_overdue != 'ALL' or :p_overdue = 'ALL' )
-
using case when statement or decode unfavorable in where clause
Hi the gems...
I have a problem in the following query...
I try to use when case statement in where clause of a select query.
Select cr.customer_name. ' - ' || CR.customer_number as cust_name,
CR. Salary salary
of customer_details cr
where (case when ' > ' = ' > ' then ' cr.salary > 5000')
When ' > ' = ' < ' then ' cr.salary < 5000'
When ' > ' = '=' and then 'cr.salary = 5000'
Another null
(end);
the expression in the when clause of the statement of the case, when comes from HQ and according to the choice I have to make the where clause.
That is why, for the execution of the query, I put ' > ' in this place.
If the original query will look like this (for reference):
Select cr.customer_name. ' - ' || CR.customer_number as cust_name,
CR. Salary salary
of customer_details cr
where (case when variable = ' > ' then ' cr.salary > 5000')
When the variable = ' < ' then ' cr.salary < 5000'
When the variable = '=' and then 'cr.salary = 5000'
Another null
(end);
so, in a real case, if the user selects ' > ' then the filter will be ' where cr.salary > 5000.
If the user selects ' < ' then the filter will be ' where cr.salary < 5000.
If the user selects '=', then the filter will be 'where cr.salary = 5000 '.
but I get the error "ORA 00920:invalid relational operator.
Help, please... Thanks in advance...Hello
select cr.customer_name || ' - ' ||cr.customer_number as cust_name, cr.salary as salary from customer_details cr where ( v_variable = 'bigger' and cr.salary > 5000 ) or ( v_variable = 'less' and cr.salary < 5000 ) or ( v_variable = 'eq' and cr.salary = 5000 )
Published by: user6806750 on 22.12.2011 14:56
For some reason that I can't write in sql "<', '="">", "=".',>
-
Tuning - using the CASE statement in the WHERE clause of the query
Hi all
My request has been changed to use a CASE statement in the WHERE clause to examine the data to some columns based on a parameter value. This modified request is made a full table and constantly running scan. Please suggest what can be done to improve its performance:
Query:
Description of the table: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;
(Number of lines: approx. > amount 600 000 000)
Explain PlanName 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)
Published by: Chaitanya on June 9, 2011 02:44SQL> 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)
Published by: Chaitanya on June 9, 2011 02:47When it is executed individually, MERGE LOGIC 1 and LOGIC of MERGE 2 take about ten for a daterange of 30 days data.
BUT FULL is to get the scores of APR - JUN, INCR becomes partitions for MAY so not both 30 days.
Are compare us like with like? -
Cannot run a «BUSINESS...» WHEN ' with a date in a WHERE CLAUSE condition
Hello
I need to check for a condition and date according to the date, he would check a set of host names or the other game... so I thought I could write something like this in the WHERE clause:
AND HOST_NAME IN (
CASE
WHEN TO_DATE (: StopDate, 'MON-DD-YYYY') > = TO_DATE ('22 - DEC - 2015 "," DD-MON-YYYY ') THEN
('SERVERNAMEA', 'SERVERNAMEB', 'SERVERNAMEC') ON THE OTHER
("SERVERNAME1", "NAMESERVER2", "SERVERNAME3")
END
)
I get the following:
ORA-00907: lack of right parenthesis
00907 00000 - "lack the right parenthesis.
But there is no missing parentheses!
If I take the case... when... end, and run the host_name in ('SERVERNAMEA', 'SERVERNAMEB', 'SERVERNAMEC') (separately, they run fine)...
I also tried:
AND
( CASE
WHEN TO_DATE (: StopDate, 'MON-DD-YYYY') > = TO_DATE ('22 - DEC - 2015 "," DD-MON-YYYY ') THEN
HOST_NAME IN ('SERVERNAMEA', 'SERVERNAMEB', 'SERVERNAMEC') ELSE
HOST_NAME IN ('SERVERNAME1","NAMESERVER2","SERVERNAME3")
END
)
the error I get is:
ORA-00905: lack of keyword
00905 00000 - 'lack the key word'
What keyword miss me?
Post edited by: c75d2e42-06a0-4eb1-a576-5652edcbfbe8
Hello
c75d2e42-06a0-4eb1-A576-5652edcbfbe8 wrote:
It was a typo when transferred to the Oracle forum... the version of Oracle is: PL/SQL version 10.2.0.3.0 (10 G)
I also used IF the statement and that has not worked (in error "missing parenthesis") which I'm sure is a matter of "syntax"... I did not know that the CASE statement returns the value single only, so it's probably the problem. Is it possible to re - write this?
...
IF only works in PL/SQL. It does not in SQL, even if that SQL is embedded in PL/SQL.
There are many ways to re - write the condition. And here's one:
AND ((host_name IN ('SERVERNAMEA', 'SERVERNAMEB', 'SERVERNAMEC')
AND TO_DATE (: StopDate, "MON-DD-YYYY") > = TO_DATE ('22-DEC-2015', "MON-DD-YYYY")
)
OR (host_name IN ('servername1","Nameserver2","SERVERNAME3")
AND TO_DATE (: StopDate, "MON-DD-YYYY")< to_date="" ('dec-22-2015', =""> >
)
)
If: StopDate can be NULL, it must be a bit more complicated.
You can also use nested, such as CASE expressions
AND THE CASE
WHEN TO_DATE (: StopDate, "MON-DD-YYYY") > = TO_DATE ('22-DEC-2015', "MON-DD-YYYY")
WHILE CASE
WHEN host_name IN ('SERVERNAMEA', 'SERVERNAMEB', 'SERVERNAMEC')
AND THEN 'OK '.
END
OTHER CASES
WHEN host_name IN ('servername1","Nameserver2","SERVERNAME3")
AND THEN 'OK '.
END
END = 'OK '.
I wonder if this in your application is the best way, however. If SERVERNAMEA was put into service on December 22, maybe that that fact should be recorded in a table. If you have a table with 1 row per server, you can consider adding 2 columns DATE to show when the server was first and last used. Then, rather than use a WHERE clause to test servers appropriate, you could do an inner join.
-
May I ask where clause to satisfy two conditions at the same time (and)
Hello
I wrote a few joins and usually in the final stage where condition with certain conditions be satisfied in where clause. In all conditions where I only want one and condition fulfilled only if both are collected.
If we look at the this funciton is I should meet (get_order_type (ogr.order_number) <>'R')) AND wh1.third_party_warehouse! = 'Y') but I'm not get rid off both that if order_type is 'R' they are removed and if tooling_group is yes then ther are removed the result. I'm trying with both below pointed out together how can check these conditions? I can do in where clause?
((sg.tooling_stock_group <> 'Y'))
and
(get_order_type (ogr.order_number) <>'R'))
Of order_goods_received ogr
LEFT JOIN part_master pm
On ogr.part_number = pm.part_number
-stock_groups sg left join
SG stock_groups left join
on pm.stock_group = sg.stock_group
and ((sg.tooling_stock_group! = 'Y'))
and (get_order_type (ogr.order_number)! = 'R'))
-Pm.stock_group = sg.stock_group
LEFT JOIN batch_record_1 br1
-A FEW MORE CODE HERE-
WHERE ((ogr.delivery_date > = trunc (date_départ)))
AND
((ogr.delivery_date < = trunc (end_date)))
AND
OGR.goods_received_number <>'1' AND
get_order_type (OGR.order_number) NOT IN ('L') AND
OGR.returned_to_supplier_indicator not in('S','L') and
-sg.tooling_stock_group <>'Y '.
-((sg.tooling_stock_group <> 'Y') and (get_order_type (ogr.order_number) not in ('R')))
-(get_order_type (OGR.order_number) <>'R' AND sg.tooling_stock_group <>'Y')
((sg.tooling_stock_group <> 'Y'))
and
(get_order_type (ogr.order_number) <>'R'))
AND wh1.third_party_warehouse! = « Y »
UNION
(
Thank you.
Whenever you have a problem with such logic, you should try and draw a logic state diagram:
In your case, you are looking to two variables, with two possible States (equal to a value or is not it)
Then, take this sense of the sg.tooling_stock_group T and O as being the get_order_type (ogr.order_number) and create a state diagram logical based on your description of what you want. We will use a value of "Y" to indicate we want to record and a value of "N" to indicate the folder should not be returned.
Start with an empty logical diagram...
T = 'Y' T! = « Y »
+---------+---------+
| | |
| | |
| | | O = 'R '.
| | |
| | |
+---------+---------+
| | |
| | |
| | | O! = « R »
| | |
| | |
+---------+---------+First of all, you said: "If there is the result returned by the defined user function is"R"and the table that has the tooling_stock_group y then it should avoid this record."
T = 'Y' T! = « Y »
+---------+---------+
| | |
| | |
| N | | O = 'R '.
| | |
| | |
+---------+---------+
| | |
| | |
| | | O! = « R »
| | |
| | |
+---------+---------+Then you said: "If order_type does not return" R' but the tooling_stock_group is 'Y', then I should not avoid recording ".
T = 'Y' T! = « Y »
+---------+---------+
| | |
| | |
| N | | O = 'R '.
| | |
| | |
+---------+---------+
| | |
| | |
| Y | | O! = « R »
| | |
| | |
+---------+---------+Now, you have not given any logic for the rest of the diagram, so I made the assumption that you do not want to avoid these records...
T = 'Y' T! = « Y »
+---------+---------+
| | |
| | |
| N | Y | O = 'R '.
| | |
| | |
+---------+---------+
| | |
| | |
| Y | Y | O! = « R »
| | |
| | |
+---------+---------+Now there are different ways to determine the location of the clause of this diagram.
We could say that we want the records for all of the T column! = "Y" or all the records for all of the O line! " = R...where (sg.tooling_stock_group! = 'Y' or get_order_type (ogr.order_number)! = 'R')
We say we want to EXCLUDE (using DO NOT) all records where T = "Y" AND O = "O" "
If not (sg.tooling_stock_group = 'Y' and (ogr.order_number) get_order_type = 'R')
Those are the two most logical ways to do.
(In my previous answer, that we can see in this diagram, there is no need of the "or get_order_type (ogr.order_number).) (= 'R' "part, which is superfluous, but would not break it)Now let's look at your logic to see what it looks like to in the diagram...
where ((sg.tooling_stock_group <> «Y»))
and (get_order_type (ogr.order_number) <> 'R')
)In a logic diagram that looks to...
T = 'Y' T! = « Y »
+---------+---------+
| | |
| | |
| N | N | O = 'R '.
| | |
| | |
+---------+---------+
| | |
| | |
| N | Y | O! = « R »
| | |
| | |
+---------+---------+As you can see, your logic was only targeting one of the 4 possible results for select records and excluding 3.
-
DBMS_XMLGEN. GETXML confusing WHERE the condition of clause?
I can't quite wrap my head around that, but I found the following query
Select * from user_views
where dbms_xmlgen.getxml ("select text from user_views where view_name =" "|") view_name | ') like '% view_table_name % '.
I am totally confuse everything, that's what makes the WHERE condition in the query?
When I run the game "highlight text" it returns nothing
If anyone knows why the WHERE clause is necessary?
Thank yourodneyc8063 wrote:
I don't understand why there are extra, extra quotes | and the "solomon_is_cool".Additional citations have nothing to do with dbms_xmlgen. In string literals Oracle must be in quotes by acronym. So what do you do if literal contains a quotation, for example O'Reily? The rule is, apostrophe inside the quoted string must be preceded by a second single quote. Thus, to produce the O string literal 'Reily, one must use 'O' Reily'. Now, back to dbms_xmlgen.get_xml. She waits for the SQL statement as a parameter. What SQL statement we want to produce? It would be:
Select the text from user_views where view_name = "some-view-name."
Right? Then, how the chain of the SQL statement above should look like? One way is to double single quotes:
"Select the text from user_views where view_name =" some-view-name "'.
But some-name-of-view is stored in the column of our query view_name. We have therefore concatenate the piecies which are:
(1) text select lieral user_views where view_name = '
(2) column view_name
(3) literal 'If we end up having:
"Select the text from user_views where view_name ="' | view_name | ''''
Now, you have replaced with solomon_is_cool view_name. This will not work because there is no solomon_is_cool column is user_views. What you probably wanted was:
"Select the text from user_views where view_name ="solomon_is_cool"'.
Then:
dbms_xmlgen. GetXml ("select text from user_views where view_name ="solomon_is_cool"'") would look like to display user named solomon_is_cool text is tiny, which again could be a problem since the names, except between quotes, are stored in the data dictionary for uppercase. In any case, the counting of single quotes in string literals can be confusing. In the new versions of oracle introduced then Q-literals,
dbms_xmlgen. GetXml ("select text from user_views where view_name =" "|") view_name | '''')
could be replaced by more readable:
dbms_xmlgen. GetXml (Q'[select text from user_views where view_name = ']' | view_name |) Q'[']')
For example:
SQL> select Q'[select text from user_views where view_name = ']' || view_name || Q'[']' from user_views where rownum < 3; Q'[SELECTTEXTFROMUSER_VIEWSWHEREVIEW_NAME=']'||VIEW_NAME||Q'[']' ------------------------------------------------------------------------------ select text from user_views where view_name = 'EMP_VW' select text from user_views where view_name = 'REMOTE_DDL_VIEW_TEST' SQL>
SY.
-
Case statement in a WHERE clause
Gurus,
Im trying to avoid Union of several select statements using a CASE inside a WHERE clause. When the parameter is defined as "What's new" it is advisable to use a code and for "Update Items" another condition.
Having a problem with the following... I know that BOX in WHEREs instructions are allowed only can not make it work for this one.
Points for correct and helpful answers!and case when 'New Items' = 'xxx' --p_item_status then msi.creation_date = msi.last_update_date when 'Updated Items' = 'bbb' --p_item_status then msi.creation_date != msi.last_update_date else 1=1 end;
Published by: sreese on March 27, 2012 17:51Hello
The great thing about CASE expressions, is that they allow you to use a login IF-THEN-ELSE in the clause SELECT, the ORDER BY clause or anywhere else. The WHERE clause has its own way to IF-THEN-ELSE logic, so even if you can use a CASE expression in a clause WHERRE, it usually does not help anything. You can do somehting like this:
WHERE ( :p_item_status = 'New Items' AND msi.creation_date = msi.last_update_date ) OR ( :p_item_status = 'Updated Items' AND msi.creation_date != msi.last_update_date ) OR ( :p_item_status NOT IN ( 'New Items' , 'Updated Items' ) )
This guess: p_item_status is not NULL.
If: p_item_status can be NULL, and you want to include all the lines where it is, and then change the last condition toOR ( NVL (:p_item_status, 'OK') NOT IN ( 'New Items' , 'Updated Items' ) )
I hope that answers your question.
If not, post a small example of data (CREATE TABLE and INSERT, only relevant columns instructions), some values of the parameter and the expected results of these data for each value of the parameter.
Explain, using specific examples, how you get these results from these data.
Always tell what version of Oracle you are using. -
How to use the instruction box in where clause
Hi all
I need to use cases where clause guide me please how I can do it.
AND WHEN BOX: KAPCO = "KAPCO" THEN cla. CLASS_NAME LIKE '% KAPCO % '.
WHEN: KAPCO = "NON_KAPCO" THEN SUPERIOR (cla. CLASS_NAME) NOT LIKE '% KAPCO % '.
WHEN: KAPCO = 'ALL' GOLD: KAPCO IS NULL SO SUPERIOR (cla. CLASS_NAME) LIKE '% '.
END
Concerning
Published by: user10941925 on March 4, 2012 22:31Hello
CASE expressions return a single value, for example a VARCHAR2. They do refer to code fragments.
You usually don't need (or want) an expression BOX in a WHERE clause, or any other condition. I think that you were trying to do the equivalent of:
AND ( (:KAPCO = 'KAPCO' AND cla.CLASS_NAME LIKE '%KAPCO%') -- Don't you want UPPER on this line? OR (:KAPCO = 'NON_KAPCO' AND UPPER(cla.CLASS_NAME) NOT LIKE '%KAPCO%') OR (NVL (:KAPCO, 'ALL') = 'ALL' AND cla.CLASS_NAME IS NOT NULL) )
If you would care to post some sample data (CREATE TABLE and INSERT statements) and the results desired from this data, I was able to test this.
-
Query on calculated field where deletes conditions of the clause
Hi gurus,
Under a new requirement, we have created a new field calculated in Contact before JC, which removes the condition from which to extract the clause and made full table.
Requirement:
We have a field in the user interface, that has data like "J1234" or "K44335". Customer won't see the first character of the alphabet of these data.
Solution, we have developed:
(1) create a new calculated field 'ABC person UId Calc' as follows.
Name: ABC person UId Calc
Calculated: true
Computed value: right ([nobody UId], Len([Person UId]) - 1).
(2) exposed this field in the user interface.
Question:
When the user queries in the applet for this field, the generated SQL code has no condition in a Where Clause to the query on PERSON_UID.
SQL has earlier been something like...
(T25. EMP_FLG = 'N') AND (T25. PERSON_UID LIKE: 3)
But after you make this change, the current SQL is trimmed of the just
(T25. EMP_FLG = 'N')
This means that this isn't question with PERSON_UID, resulting in an impact huge performance.
If someone could suggest on this issue, to the difficulty.
ConcerningOh sorry, I guess I missed something.
I guess the simplest solution is to store the values of redundantly (A, B the value of the value and the value of A + B) and stop using a calculated field.
You could script something in the PreQuery event, but I think it's an ugly hack.
-
A CASE statement with additional where clause
Hi all
I need assistance in which this obligation clause
Table:-balance_table
Columns:-balance_type, balance_amount, balance_month, budget_name
balance_type column had given 'Real' OR 'Budget '.
Now I am trying to extract data such as: if: entered_month is Mar-2009 then before and including Mar-2009 will show real balance sheet Date and after Mar-2009 will show budget data.
It works fine, I need to add a condition more restrict the budget name, there are several budget_name in the table for balance_type = 'Budget '.
SELECT SUM (balance_amount), balance_type, balance_month
OF balance_table
WHERE balance_type =
(CASE
WHEN balance_month < =: entered_month
THEN 'real '.
ANOTHER 'Budget '.
END
)
AND budget_name = 'BUDGET1.
Balance_type GROUP, balance_month
Stated above is erroneous because ' AND budget_name = ' BUDGET1 "clause restricts the set of data."
Please help in contrcuting where clause 1) to sort the data according to Budget/actual and 2) for specific budgets, so budget_type = Budget
Thank you
BobinThat should do it.
SELECT SUM (balance_amount), balance_type, balance_month FROM balance_table WHERE (balance_type = 'Actual' and balance_month <= :entered_month ) or (balance_type = 'Budget' and balance_month > :entered_month and budget_name = :entered_budget_name ) GROUP BY balance_type, balance_month ORDER BY 2
See you soon
Sarma. -
Using the index function in where clause of Exchange.
Hello friends,
I need your help with a problem.
I have a query that uses two table Say T1 and T2, where C1 is common column with which both are joined.
C1 is the primary key in T1, but no index available in Q2 for the C1. T1C2 is the column that we want to select.
(Note that table may be a Master table)
Now let's see the query:
Select T1C2
From T1, T2
where T2. C1 = T1. C1
Here where the clause may have other conditions and From clause can have other tables as needed.
I want to know that if I have change the query as continuation of leave my query to use the index available of T1. C1.
Select T1C2
from T1, T2
where T1. C1 = T2.C1
Then, the query uses the index available of T1. and I get better performance. Even a small improvement of performance help me much because this type of query is used in a loop where clause (so it will be run several times).
Please advise on this...
Kind regards
Lifexisxnotxsoxbeautiful...Hello
18:43:17 rel15_real_p>create table t1(c1 number primary key, c2 number); Table created. 18:43:26 rel15_real_p>create table t2(c1 number, c2 number); 18:45:08 rel15_real_p> 18:45:09 rel15_real_p>begin 18:45:09 2 for i in 1..100 18:45:09 3 loop 18:45:09 4 insert into t1(c1,c2) values (i,i+100); 18:45:09 5 end loop; 18:45:09 6 commit; 18:45:09 7 end; 18:45:09 8 / PL/SQL procedure successfully completed. 18:45:09 rel15_real_p> 18:45:09 rel15_real_p> 18:45:09 rel15_real_p>begin 18:45:09 2 for i in 1..100 18:45:09 3 loop 18:45:09 4 insert into t2(c1,c2) values (i,i+200); 18:45:09 5 end loop; 18:45:09 6 commit; 18:45:09 7 end; 18:45:09 8 / 18:45:23 rel15_real_p>select count(*) from t1; COUNT(*) ---------- 100 18:45:30 rel15_real_p>select count(*) from t2; COUNT(*) ---------- 100 18:45:49 rel15_real_p>select index_name,index_type from user_indexes where table _name='T1'; INDEX_NAME INDEX_TYPE ------------------------------ --------------------------- SYS_C0013059 NORMAL 18:48:21 rel15_real_p>set autotrace on 18:52:25 rel15_real_p>Select T1.C2 18:52:29 2 From T1, T2 18:52:29 3 where T2.C1 = T1.C1 18:52:29 4 / C2 ---------- 101 102 103 104 105 ..... ...... C2 ---------- 200 100 rows selected. Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=7 Card=100 Bytes= 900) 1 0 HASH JOIN (Cost=7 Card=100 Bytes=3900) 2 1 TABLE ACCESS (FULL) OF 'T1' (TABLE) (Cost=3 Card=100 By es=2600) 3 1 TABLE ACCESS (FULL) OF 'T2' (TABLE) (Cost=3 Card=100 By es=1300) Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 21 consistent gets 0 physical reads 0 redo size 1393 bytes sent via SQL*Net to client 562 bytes received via SQL*Net from client 8 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 100 rows processed 18:52:31 rel15_real_p>analyze table t1 compute statistics; Table analyzed. 18:55:35 rel15_real_p>analyze table t2 compute statistics; 18:55:38 rel15_real_p>set autotrace on 18:55:42 rel15_real_p>Select T1.C2 18:55:43 2 From T1, T2 18:55:45 3 where T2.C1 = T1.C1 18:55:46 4 / C2 ---------- 101 102 103 104 105 ..... ...... C2 ---------- 200 100 rows selected. Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=6 Card=100 Bytes=7 00) 1 0 MERGE JOIN (Cost=6 Card=100 Bytes=700) 2 1 TABLE ACCESS (BY INDEX ROWID) OF 'T1' (TABLE) (Cost=2 Ca rd=100 Bytes=500) 3 2 INDEX (FULL SCAN) OF 'SYS_C0013059' (INDEX (UNIQUE)) ( Cost=1 Card=100) 4 1 SORT (JOIN) (Cost=4 Card=100 Bytes=200) 5 4 TABLE ACCESS (FULL) OF 'T2' (TABLE) (Cost=3 Card=100 B ytes=200) Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 23 consistent gets 0 physical reads 0 redo size 1393 bytes sent via SQL*Net to client 562 bytes received via SQL*Net from client 8 SQL*Net roundtrips to/from client 1 sorts (memory) 0 sorts (disk) 100 rows processed 18:56:56 rel15_real_p>Select T1.C2 18:56:56 2 From T1, T2 18:56:56 3 where T1.C1 = T2.C1 18:56:58 4 / C2 ---------- 101 102 103 104 105 ..... ...... C2 ---------- 200 100 rows selected. Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=6 Card=100 Bytes=7 00) 1 0 MERGE JOIN (Cost=6 Card=100 Bytes=700) 2 1 TABLE ACCESS (BY INDEX ROWID) OF 'T1' (TABLE) (Cost=2 Ca rd=100 Bytes=500) 3 2 INDEX (FULL SCAN) OF 'SYS_C0013059' (INDEX (UNIQUE)) ( Cost=1 Card=100) 4 1 SORT (JOIN) (Cost=4 Card=100 Bytes=200) 5 4 TABLE ACCESS (FULL) OF 'T2' (TABLE) (Cost=3 Card=100 B ytes=200) Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 23 consistent gets 0 physical reads 0 redo size 1393 bytes sent via SQL*Net to client 562 bytes received via SQL*Net from client 8 SQL*Net roundtrips to/from client 1 sorts (memory) 0 sorts (disk) 100 rows processed
-Pavan Kumar N
Maybe you are looking for
-
A copy of no-upgrade to iPhoto
I recently got an old mac running OS 10.7.5, (2006). iPhoto has been deleted and I would like to get a copy no-upgrade to iPhoto to install. Any suggestion would be appreciated. Thanks for the help in advance!
-
HP EliteBook 745 G3 HPE: win 10 Crash with Bluedump
Hello I use HP EliteBook 745 G3 HPE, I enable virtualization in the BIOS and enable the hyper-v & hyper-v services in OS. After reboot start giving bluedump with the hundler error. After System Restore I able to connect to the operating system. can h
-
iMessage receives no text document
Hi, it has been fo a week iMessage opens the document text (such as .pages or .docx). I don't know why, but if someone send me an iMessage document does not download the file. I see something like this when I try to open the document. From my iPhone,
-
Equium A60-191 - need a new recovery disk
Hello. Anyone know where I can get a cd recovery Toshiba Equium A60-191-the one I was badly scratched and I need a new - can I download one on line? Any help would be greatly appreciated. Thank youDee :-)
-
I hear the sound to the hybrid Aver pcmcia tuner
I hear the sound to aver hybrid pcmcia tuner. On another model, the noise is absent Post edited by: alexey_k