Case condition
Hi,
this is some diff question from same logic,
i wanted to use timestamp format in case condition
case when start
date > '2019-08-08 00:00:00' then TIMESTAMPDIFF(SQLTSI_MINUTE,"Esr"."Power Off",TIMESTAMP '2011-08-22 00:00:00' )
getting error message
Oracle Error code: 1843, message: ORA-01843: not a valid month at OCI call
Pleasehelp
thanks
I think the real issue here is that you are dealing with 2 different datatype in one column...
You have start_date which is date, but your timestampdiff fucntion will give you result in number. So the bottomline is that you can't have 2 different data type in one field..
I'd leave start_date column as it is and create another field called timediff and use timestampdiff expression in that column. So column timediff column will give you results in number for all of the start dates..
Then based on the result of timediff column, for example, if you can spot that for everything that the start date > 2019-08-08 00:00:00, the timediff value is > 300 (just an example). Then you can apply filter or even a case statement there to pick everything greater than 300..
Let me know if it makes sense
Similar Messages
-
How to assign the parameter in case condition?
Dear All,
I have one procedure say p1. I want to pass one parameter like d_forcd = 'IN' and ALC_Code in ('11', '21', '31') from this procedure.
In begin part I have one query. Can I pass this paramer in case condition in that query.
If possible please hint me.
Thanks in advance,
Prathamesh.
[pre]There is probably a way using CASE, but it seems easier without:
SQL> var param varchar2(10)
SQL>
SQL> exec :param := 'IN'
PL/SQL procedure successfully completed.
PARAM
IN
SQL> SELECT * FROM dual
2 WHERE ( :param = 'IN' AND dummy IN ('A','B','X','Y')
3 OR NVL(:param,'?') != 'IN' );
D
X
1 row selected.Substitute your actual PL/SQL parameter for the SQL*Plus variable. -
Case condition in where clause
Hi
I'm a SQL beginner but according to my reading of the manual, this syntax should be acceptable. I'm using it in application express but tested it also in SQL developer and finding that it fails with an "invalid relational operator" error, with the line number pointing to the ELSE clause of the CASE condition, but a column number pointing to white space.
This is a nested select but that shouldn't matter. The fragment in question is this:
select some columns
from res
where res.villaid = :P605_VILLAID
and INSTR(:P604_RES_STATES, res.states) != 0
and (CASE :P604_EXCLUDE_ZERO
WHEN 'Y' THEN 'res.rate > 0'
ELSE 'res.rate >= 0'
END)
and res.DEPARTDATE > :P605_STARTDATE
and res.ARRIVEDATE <= :P605_ENDDATE,
I hope this formats ok when posted. The code is looking for reservations in a date range (which works fine when the other conditions are commented out), and should only output zero-rate rows (complimentary nights) if the user asks for this in the P604_EXCLUDE_ZERO bind variable. There are probably other ways to go about this, but I am too stubborn to give up on this. Well, not yet anyway.
Thanks and regards
CSselect some columns
from res
where res.villaid = :p605_villaid
and instr (:p604_res_states, res.states) != 0
and sign (res.rate) >= case when :p604_exclude_zero = 'Y' then 1 else 0 end
and res.departdate > :p605_startdate
and res.arrivedate <= :p605_enddate -
Diff between two timestamp in minutes on case condition
Hi Gurus,
I wanted to find the diff between two timestamp in minutes on case condition
when type=1 then min(col2) and type=2 then max(col2)
type-(datatype-numeric)
col2-(datatype-timestamp)
how can i do this
ThanksHi,
Is this a question about the Berkeley DB SQL product?
If so, you should look at the documentation on time fields in the SQLite page here:
http://www.sqlite.org/datatype3.html#datetime
Otherwise, you are asking this on the wrong forum.
Regards,
Alex Gorrod
Oracle Berkeley DB. -
I am trying to create a calculation based on the following case condition. For some reason its working fine on a existing report but now when I am creating a new report based on this, its giving error "Invalid Combinations of Condition and Calculations" any thoughts?
I have also found out that when I am adding "ELSE" in the condition then only it's throwing that error otherwise working fine. I have also tried to recreate this case condition in my select statement and tested in TOAD and it runs without a problem.
CASE WHEN ( Reference Date < TO_DATE(:Date) ) AND ( Support Start Date > TO_DATE(:Date) ) THEN TO_DATE(:Date) ELSE Reference Date ENDTry to wrap the "AND" and re-select the items into
the case since it looks like the " are missing.
CASE WHEN ( "Reference Date" < TO_DATE(:Date) AND
"Support Start Date" > TO_DATE(:Date) ) THEN
TO_DATE(:Date) ELSE "Reference Date" ENDTried that too but no luck :(. As I said before the same condition is working fine in another existing similar report. I am working on some changes in the existing report and when I am putting this condition it just throws this error message.
Message was edited by:
DiscoverDiscoverer -
Timestamp in minutes on case condition
Hi Gurus,
I wanted to find the diff between two timestamp in minutes on case condition
when type=1 then min(col2) and type=2 then max(col2)
type-(datatype-numeric)
col2-(datatype-timestamp)
how can i do this
ThanksHi,
Sorry, the question could means diozens of different things.
This is the answer to one of them:
SELECT id
, ... -- other columns from individual rows
, MAX ( CASE
WHEN type = 2
THEN col2
END
) OVER (PARTITION BY id)
- MIN ( CASE
WHEN type = 1
THEN col2
END
) OVER (PARTITION BY id) AS dif
FROM table_x
;The dif column will be an INTERVAL DAY TO SECOND., which you can convert to a number if you need to.
Whenever you have a question, post a little sample data (CREATE TABLE and INSERT statements) and the results you want from that data. Explain how you get those results from that data.
Always say what version of Oracle you are using. -
Hi,
i wanted to add a case condition in which it sshould change the colour of the row else none. it possible in obiee
thanksHi,
Create a new column in your report with your case statement : CASE condition then 1 else 0 End (and hide it)
Go to your column for which you want to change the color of the row the go to the properties and specify a conditional formatting based on your condition.
If 1 then apply color elso nothing.
Regards
Adil -
To remove error from error message screen stnd SAP in case condition met
Hi all,
I have a strange problem may be not to you guys,
well i am showing an error message in ME21N on certain condition in EXIT 'EXIT_SAPMM06E_012' this is working fine i am using simple stmt Message e(MSGno) text-01, but when i correct the problematic part and again want to save my data it is aloowing me to save data but my error message(In red) still remains there if i see in EDIT ( of faulty messages pop-up window). Can any body know if we have to remove error message manually in case our program satify the condition.
if yes what portion i m missing to delete the error message.
any reply will be highy appreciated <REMOVED BY MODERATOR>
Mandy
Edited by: Alvaro Tejada Galindo on Feb 7, 2008 10:51 AMWell we can remove message from message text tray using pre-defined macros to erase message.its like
IF SY-SUBRC NE 0.
IF NOT tekpo-id IS INITIAL.
"This is to remove error message later on
"if problem corrected.
mmpur_business_obj_id tekpo-id.
ENDIF.
mmpur_metafield mmmfd_item_userexit.
mmpur_message 'E' 'ZMM02' '050'
'Intraplant STO :' '' '' ''.
else.
"To erase message
mmpur_metafield mmmfd_item_userexit .
mmpur_remove_messages_by_id tekpo-id.
ENDIF.
its working for one line item, if more then one line item you need to verify it.
ENJOY SAP -
CASE Condition Causing Plan Difference
Version
SQL> SELECT * FROM v$version;
BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE 11.2.0.2.0 Production
TNS for Linux: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production
5 rows selected.
Optimizer
SQL> show parameter optimizer
NAME TYPE VALUE
_optimizer_autostats_job boolean FALSE
_optimizer_join_elimination_enabled boolean FALSE
optimizer_capture_sql_plan_baselines boolean FALSE
optimizer_dynamic_sampling integer 2
optimizer_features_enable string 11.2.0.2
optimizer_index_caching integer 0
optimizer_index_cost_adj integer 100
optimizer_mode string ALL_ROWS
optimizer_secure_view_merging boolean FALSE
optimizer_use_invisible_indexes boolean FALSE
optimizer_use_pending_statistics boolean FALSE
optimizer_use_sql_plan_baselines boolean TRUE
Problem
We have a vendor system that constructs the SQL to be sent to the database. These queries have the following generic structure for a WHERE clause condition:
CASE WHEN <condition> THEN <table>.<column> END = '<value>'We've noticed significantly different execution plans when the optimizer compares numbers instead of varchar2. The examples below demonstrate that. In our case this difference is impacting the execution plans of a larger query effectively doubling buffer gets and resulting in execution times that are 2-4 times as worse.
Query 1
SQL> SELECT /*+gather_plan_statistics*/ * FROM DUAL WHERE CASE WHEN 1=1 THEN 'X' END = dummy;
D
X
1 row selected.
SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(null,null,'ALLSTATS LAST'));
PLAN_TABLE_OUTPUT
SQL_ID 6ghjubgpwpr61, child number 0
SELECT /*+gather_plan_statistics*/ * FROM DUAL WHERE CASE WHEN 1=1 THEN
'X' END = dummy
Plan hash value: 272002086
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads |
| 0 | SELECT STATEMENT | | 1 | | 1 |00:00:00.02 | 2 | 2 |
|* 1 | TABLE ACCESS FULL| DUAL | 1 | 1 | 1 |00:00:00.02 | 2 | 2 |
Predicate Information (identified by operation id):
1 - filter("DUMMY"='X')
Query 2
SQL> SELECT /*+gather_plan_statistics*/ * FROM DUAL WHERE CASE WHEN 'A'='A' THEN 'X' END = dummy;
D
X
1 row selected.
SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(null,null,'ALLSTATS LAST'));
PLAN_TABLE_OUTPUT
SQL_ID gcpwzksqr2w9n, child number 0
SELECT /*+gather_plan_statistics*/ * FROM DUAL WHERE CASE WHEN 'A'='A'
THEN 'X' END = dummy
Plan hash value: 272002086
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
| 0 | SELECT STATEMENT | | 1 | | 1 |00:00:00.01 | 2 |
|* 1 | TABLE ACCESS FULL| DUAL | 1 | 1 | 1 |00:00:00.01 | 2 |
Predicate Information (identified by operation id):
1 - filter("DUMMY"=CASE WHEN ('A'='A') THEN 'X' END )In my eyes the condition 'A' = 'A' is equivalent to the condition 1=1. How come in the case of the numeric comparison Oracle can eliminate the case expression but in the character comparison it cannot?
Thanks!Centinul wrote:
In my eyes the condition 'A' = 'A' is equivalent to the condition 1=1. Well, issue is Oracle looks at 'A' = 'A' as string comparison. And string comparison result is NLS setting dependent. That's why it can't evaluate CASE at compile time.
SY. -
Please help to build an sql from the given expression to build case condition with Where clause
if @rollno is not null then
Select top 1 studentid from student where rollno =:@rollno
else
if @regno is not null then
Select top 1 studentid from student where regno =:@regno
Please help me to create the above condition as a single sql statement . I will pass two argument in to the sql.
With Thanks
Pol
polachanSelect top 1 studentid from student
where (rollno =:@rollno or :@rollno is null)
and (regno =:@regno or :@regno is null)
The above expression will work in SQL server
I'm not sure you're using sql server as syntax like :@regno are not t-sql valid
so you can try the above and if it doesnt work please try in the relevant forums for more help in case you're using a different RDBMS
Please Mark This As Answer if it solved your issue
Please Mark This As Helpful if it helps to solve your issue
Visakh
My MSDN Page
My Personal Blog
My Facebook Page -
How to avoid Huge scan with case condition in the select statement.
Hi ,
I have the below sql and add condition such that c.ROLE = 18 then tibex_fixrestorestateview needs to be queried else it should not query the tibex_fixrestorestateview.
tibex_fixrestorestateview contains 10 to 20 millions records so to avoid scanning when c.ROLE <> 18
SELECT c.connectionid, u.participantid, c.port, u.PASSWORD, cg.gatewayid,
c.ROLE, u.cancelonlogout, NVL (f.tofixsequence, 1) AS tofix,
NVL (f.fromfixsequence, 1) AS fromfix
FROM tibex_connectiongroup cg INNER JOIN tibex_connection c
ON cg.connectiongroupid = c.connectiongroupid
LEFT JOIN tibex_user u ON c.connectionid = u.userid
*LEFT JOIN tibex_fixrestorestateview f*
ON u.useralias = f.useralias AND c.ROLE = 18;Regards
NMHi;
I suggest close your issue here as answered than move your issue Forum Home » Database » SQL and PL/SQL which you can get more quick response
Regard
Helios -
Case condition is not giving the correct result in Union query
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(FeeDate,Clerk,ClrComm,ExComm,TagComm) as select
20120102,'Mathews',25,56,245 from dual union all select
20120102,'Lika',0,56,245 from dual union all select
20120102,'Ram',0,0,245 from dual union all select
20120102,'Angilo',NULL,NULL,245 from dual union all select
20120102,'Nikki',225,NULL,245 from dual union all select
20120103,'Ram',NULL,556,2345 from dual union all select
20120103,'Lika',NULL,NULL,245 from dual union all select
20120103,'Mathews',25,56,245 from dual;DML Used -
SELECT FEEDATE,CLERK,SUM(ClrComm) ClrComm,null ExComm,null TagComm,sum(case when ClrComm>0 then ClrComm end) Comm
from tt2
group by FEEDATE,CLERK
UNION
SELECT FEEDATE,CLERK,null ClrComm,sum(ExComm) ExComm,null TagComm,sum(case when ExComm>0 then ExComm end) Comm
from tt2
group by FEEDATE,CLERK
UNION
SELECT FEEDATE,CLERK,null ClrComm,null ExComm,sum(TagComm) TagComm,sum(case when ClrComm=0 or ClrComm is NULL and ExComm=0 or ExComm is NULL then TagComm end) Comm
from tt2
group by FEEDATE,CLERKResult Output -
FEEDATE CLERK CLRCOMM EXCOMM TAGCOMM COMM
20120102 Angilo 245 245
20120102 Angilo
20120102 Lika 0
20120102 Lika 56 56
20120102 Lika 245 245
20120102 Mathews 25 25
20120102 Mathews 56 56
20120102 Mathews 245
20120102 Ram 225 225
20120102 Ram 0
20120102 Ram 490 490
FEEDATE CLERK CLRCOMM EXCOMM TAGCOMM COMM
20120103 Lika 245 245
20120103 Lika
20120103 Mathews 25 25
20120103 Mathews 56 56
20120103 Mathews 245
20120103 Ram 556 556
20120103 Ram 2345
20120103 Ram
Output expected -
FEEDATE CLERK CLRCOMM EXCOMM TAGCOMM Comm
20120102 Mathews 25 56 245 81
20120102 Lika 0 56 245 56
20120102 Ram 0 0 245 245
20120102 Angilo 245 245
20120102 Nikki 225 245 225
20120103 Ram 556 2345 556
20120103 Lika 245 245
20120103 Mathews 25 56 245 81I sincerely thanks to all of you for your effort in advance.This way:
select feedate, clerk, clrcomm, excomm, tagcomm,
sum(decode(nvl(clrcomm, 0) + nvl(excomm, 0), 0, tagcomm, nvl(clrcomm, 0) + nvl(excomm, 0))) over (partition by feedate, clerk) comm
from tt2
order by feedate, clerk;
FEEDATE CLERK CLRCOMM EXCOMM TAGCOMM COMM
20120102 Angilo 245 245
20120102 Lika 0 56 245 56
20120102 Mathews 25 56 245 81
20120102 Nikki 225 245 225
20120102 Ram 0 0 245 245
20120103 Lika 245 245
20120103 Mathews 25 56 245 81
20120103 Ram 556 2345 556
8 rows selected -
SQL CASE statement in XML template- End tag does not match start tag 'group
Hi All,
I am developing a report that has the SQL CASE statement in the query. I am trying to load this into RTF with report wizard and it gives me below error
oracle.xml.parser.v2.XMLParseException: End tag does not match start tag 'group'
Does XML publisher support CASE statement?
My query is something like this
SELECT customercode,
SUM(CASE WHEN invoicedate >= current date - 30 days
THEN balanceforward ELSE 0 END) AS "0-30",
SUM(CASE WHEN invoicedate BETWEEN current date - 60 days
AND current date - 31 days
THEN balanceforward ELSE 0 END) AS "31-60",
SUM(CASE WHEN invoicedate < current date - 60 days
THEN balanceforward ELSE 0 END) AS "61>",
SUM(balanceforward) AS total_outstanding
FROM MyTable
GROUP BY customercode
ORDER BY total_outstanding DESC
Please advice if the CASE statement or the double quotes are causing this error
Thanks,
PPI got this to work in the XML but the data is returning zeros for all the case statements. When I run this in toad I get results for all the case conditions but when ran in XML the data displayed is all zeros. I am not sure what I am missing. Can someone shed some light on this please
Thanks!
PP -
FM for creating Condition Records
Is there FM for creating Condition Records for output type like transaction VV11 (Create Output - Condition Records: Sales)
Hi there,
FM for condition records? Why do you want to think of a FM for creating conditon records?
Condition records is a master data that has to be manually created as per the business requirement.
But what we can do is that if the data ia existing in an external legacy system, we can migrate that into SAP through a BAPI / BDC or LSMW depending on the volume of data.
But in other cases condition records are manually created in SAP system.
If at all you have a function module to create condition records, how would you ensure that it feeds correct data into the condition records? What key combination would the system take & what data would you want the FM to upload?
How will you monitor what data is uploaded,.
Regards,
Sivanand -
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
Maybe you are looking for
-
Cool trick for 10.4.8 users you may not know about....
if you have a mighty mouse or a scroll wheel, hold down control and twizzle it. cool eh? now move the mouse around. very cool. you can turn the same setting on to be used with key commands in system preferences -> universal. but i think this new feat
-
CRMXIF_ORDER_SAVE no data in result XML
Hi, I am trying export order from SAP CRM to another system, I need export XML, not IDoc. I have made all customizing in tranzactions SM59, SMOEAC, CRMXIF_C1. But after I create Order and press save, I have no order data in XML file, I have this file
-
Flash Builder 4.7, eGit and the 'Label Decorations'
Used 4.6 along with eGit and there were little arrows if you were pushes or pulls behing your remote tracking branch. Now with 4.7 they are gone. Looked it up under Preferences->Team->Git->Label Decorations->Text Decorations and the { branch_status}
-
No Manual iCloud Backup in 8.1?
Sigh, I love updates. But when something is gone that we have gotten used to using it is quite annoying, especially when I spend my time searching for it. Where is the manual backup to iCloud????? It used to be in Settings, iCloud. Now it is not
-
Where do I find non Alphanumeric symbols for Appleworks spreadsheets, documents &etc (e.g. Symbols for "Euros" : "Pounds Sterling" : Greek : Cyrillic: C-cedilla ?? iMAC5 Mac OS X (10.4.3)