Decode or Case when
Can above be used inside Expression operator, I am getting error while validating. I am using owb 11g.
"variable not declared"
Thanks,
Ravi
Ravi
Sure, I use CASE all the time. Be sure all referenced variables in your CASE statement are in your INPUT Group.
-gary
Similar Messages
-
Hi,
I had a condition like
test -table
code - column
t --> True
f --> False
I had two choices of writing query
select decode(code,'t','True','False') from test;
or
select case code
when 't' then 'True'
else 'False'
end
from test;
I want to know which one is best performance wiseWrong question really.. unless you writing a very tight loop doing 100's of 1000's of iterations using that type of conditional structure. And even then performance difference will not be that significant.
The right question to ask is which one is more flexible and allows for the programmer that comes after you to read and understand and maintain your code. The CASE statement is in this regard, a lot better than a DECODE statement.
Looking just at performance (which I suggest you do not do in isolation!!), I get mixed results using 10G Enterprise on different platforms (HP-UX vs SUN AMD) and operating systems (HP-UX vs Linux).
On the former the DECODE is slightly faster. On the latter the CASE is slightly faster.
Using the construct you've specified and doing a tight loop of a 100,000 iterations, the elapsed execution times are:
HP-UX DECODE = 00:00:11.83
HP-UX CASE = 00:00:12.32
Linux/AMD DECODE =00:00:02.02
Linux/AMD CASE = 00:00:01.84
Obviosuly the CPU architecture plays a major role here. The AMD is considered as the best 64 CPU on the market. The HP-UX PARISC CPU (also 64bit), does not really compare raw performance wise. In addition this is a RISC CPU whereas the AMD CPU is I believe more CISC than RISC.
Also interesting that the faster Sun AMD/Linux server I used for this benchmark is about 10% the price of the HP-UX server.. and about 5x faster ito raw speed as this benchmark showed. :-)
An interesting exercise, but one with little real world value. Yes performance is important. But within PL/SQL, not to the level about debating whether a CASE or DECODE is faster. As I've mentioned, I believe the right question being one about readability and maintenance and not performance in this case. -
SELECT v_startdate, v_enddate,
(CASE WHEN SYSDATE BETWEEN v_startdate AND v_enddate THEN
‘active’
ELSE
‘inactive’
END) status
FROM correction_tab;
Could you kindly guide us as to how can we use DECODE and get the desired output.
Thanks,
Rami Reddy.You can, like this.
However, the CASE seems a lot clearer to me so why bother with a DECODE?
sql> with correction_tab as
2 ( select trunc(sysdate)-1 as v_startdate, trunc(sysdate) as v_enddate from dual
3 union all
4 select trunc(sysdate) as v_startdate, trunc(sysdate)+1 as v_enddate from dual
5 union all
6 select trunc(sysdate)+1 as v_startdate, trunc(sysdate)+2 as v_enddate from dual
7 )
8 SELECT v_startdate
9 , v_enddate
10 , CASE
11 WHEN SYSDATE BETWEEN v_startdate AND v_enddate
12 THEN 'active'
13 ELSE 'inactive'
14 END status
15 , decode ( sign(sysdate-v_startdate), 1, decode(sign(v_enddate-sysdate), 1, 'active', 'inactive'), 'inactive')
as status2
16 FROM correction_tab
17 /
V_STARTDA V_ENDDATE STATUS STATUS2
12-DEC-12 13-DEC-12 inactive inactive
13-DEC-12 14-DEC-12 active active
14-DEC-12 15-DEC-12 inactive inactive -
Help in changing plsql case when to decode
Hi,
Can anyone help me to change this sql for it to use decode function instead of case when? Below is the sql code
Thanks in advance.
SELECT
parts,
weeks,
SUM(qty) qty
FROM (
SELECT
CASE
WHEN ((is_tbd = 'yes' AND is_tbd_order = 'no') OR ex_fac_date > v_asofdate + 131) THEN 'tbd'
WHEN ex_fac_date BETWEEN v_asofdate - 1 AND v_asofdate + 5 AND is_tbd = 'no' THEN 'wk1'
WHEN ex_fac_date BETWEEN v_asofdate + 6 AND v_asofdate + 12 AND is_tbd = 'no' THEN 'wk2'
WHEN ex_fac_date BETWEEN v_asofdate + 13 AND v_asofdate + 19 AND is_tbd = 'no' THEN 'wk3'
WHEN ex_fac_date BETWEEN v_asofdate + 20 AND v_asofdate + 26 AND is_tbd = 'no' THEN 'wk4'
WHEN (ex_fac_date < v_asofdate - 1) AND is_tbd = 'no' THEN 'past_due'
END weeks,
ffdate,
parts,
SUM(qty) qty
FROM
delivery)I can't use case because my oracle is 8i,Please join 21st Century
Can you help me checnge this to if then elsif insteadSQL does does contain IF statement! -
Hi,
How to convert this decode statements into CASE WHEN syntax
phone_v := DECODE (c_rec.phone, NULL, NULL, DECODE ( is_it_number (c_rec.phone), 'F', DBMS_RANDOM.STRING ('a', LENGTH (c_rec.phone)), SUBSTR (DBMS_RANDOM.VALUE, 2, LENGTH (c_rec.phone))));
thanks, BcjThat should be easiest written like the following :
phone_v := DECODE (c_rec.phone
, NULL, NULL
, DECODE ( is_it_number (c_rec.phone)
, 'F', DBMS_RANDOM.STRING ('a', LENGTH (c_rec.phone))
, SUBSTR (DBMS_RANDOM.VALUE, 2, LENGTH (c_rec.phone))
);Is it ?
Nicolas. -
Using case when statement or decode stament in where clause
hi gems..
i have a problem in the following query..
i am trying to use case when statement in the where clause of a select query.
select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
cr.salary as salary
from customer_details cr
where (case when '>' = '>' then 'cr.salary > 5000'
when '>' = '<' then 'cr.salary < 5000'
when '>' = '=' then 'cr.salary = 5000'
else null
end);
the expression in the when clause of the case-when statement will come from UI and depending on the choice i need to make the where clause.
thats why for running the query, i have put '>' in that place.
so the original query will look like this(for your reference):
select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
cr.salary as salary
from customer_details cr
where (case when variable = '>' then 'cr.salary > 5000'
when variable = '<' then 'cr.salary < 5000'
when variable = '=' then 'cr.salary = 5000'
else null
end);
so, in actual 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 am getting the error "ORA 00920:invalid relational operator"
please help..thanks in advance..Hi,
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
)Edited by: user6806750 on 22.12.2011 14:56
For some reason I can't write in sql '<', '>', '=' -
Using CASE WHEN in PL/SQL package
I am trying to convert the values in a selected column into 1 and 0 so that I can display all 1s in one column, all 0s in another. I am doing this in a PL/SQL package. However ORACLE compiler does not like the CASE construct.
Does anyone know how to group values in a column into several new columns. If CASE WHEN construct is not doable in PL/SQL, what alternatives are there? Thanks.
/******* My package starts here *******/
CREATE OR REPLACE PACKAGE TEST_NEED AS
PROCEDURE procTEST_NEED(STARTING_DATE IN VARCHAR2);
END CVRR_MON_NEED;
CREATE OR REPLACE PACKAGE BODY TEST_NEED
AS
PROCEDURE procTEST_NEED(STARTING_DATE IN VARCHAR2)
IS
TEST_START DATE := TO_DATE(STARTING_DATE,'MM/DD/YYYY');
CURSOR v_Cursor IS
SELECT A.D_CODE, A.M_CODE, TEST_START , C.C_NAME,C.P_ID,
SUM(CASE WHEN MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 > 40 AND MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 <85 AND B.B_CODE IN '11.1','222.2','272.4') THEN 1 ELSE 0 END) QUALIFIED
FROM A, B, C, D
WHERE A.ID = B.B_ID
AND RTRIM(A.P_CODE) = C.P_CODE
AND A.P_ID = D.P_ID
AND A.P_ID < 99999999999999999999
AND A.E_DATETIME < SYSDATE
GROUP BY A.D_CODE, A.M_CODE, TEST_START , C.C_NAME,C.P_ID;
v_RecordHolder v_Cursor%ROWTYPE;
BEGIN
OPEN v_Cursor;
FETCH v_CursorINTO v_RecordHolder ;
WHILE v_Cursor%FOUND LOOP
look for records in another table with matching keys of the cursor
if found then update by incrementing the existing values in the matching records with values of the current currsor row
else insert the current cursor row
FETCH v_Cursor INTO v_RecordHolder ;
END LOOP;
END procTEST_NEED;
END TEST_NEED;I am trying to convert the values in a selected
column into 1 and 0 so that I can display all 1s in
one column, all 0s in another. I am doing this in a
PL/SQL package. However ORACLE compiler does not
like the CASE construct.
Does anyone know how to group values in a column into
several new columns. If CASE WHEN construct is not
doable in PL/SQL, what alternatives are there?
Thanks.
CURSOR v_Cursor IS
SELECT A.D_CODE, A.M_CODE, TEST_START ,
, C.C_NAME,C.P_ID,
SUM(CASE WHEN MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 >
40 AND MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 <85 ANDB.B_CODE IN '11.1','222.2','272.4') THEN 1 ELSE 0
END) QUALIFIEDUse the Decode function. This has been around in oracle SQL for ages and works like a case construct.
You would do something like
select ...
sum( decode (MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 >
40 AND MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 <85 AND
B.B_CODE IN ('11.1','222.2','272.4') 1,0 ) -
Error creating view with CASE -- WHEN statement in SQL*Plus
I am using Oracle 8i 8.1.7
I have an Oracle view which uses CASE...WHEN statements.
The view compiles fine in DBA studio.
Using TOAD I saved the view as an *.sql file.
However, when I try to create the view in SQL*Plus I get the following error:
SP2-0734: unknown command beginning "CASE WHEN ..." - rest of line ignored.
According to the documentation CASE -- WHEN has been implemented since since Oracle 8i rel. 2 (8.1.6)Well I'm using 8.1.6.3 and CASE and DECODE both work for me:
SQL> create or replace view v_accs as select account_name, txn,
2 decode(credit, 0, 'DB', 'CR') t_type
3 from accs;
View created.
SQL> select * from v_accs;
ACCOUNT_NA TXN T_
APC 1 DB
ABC 2 DB
HJJ 3 DB
HJH 4 CR
HJK 5 CR
APC 6 DB
APC 7 DB
ABC 8 DB
ABC 9 DB
HJJ 10 DB
HJJ 11 DB
HJH 12 DB
HJH 13 DB
HJK 14 DB
HJK 15 CR
15 rows selected.
SQL> create or replace view v_accs as select account_name, txn,
2 case when credit = 0 then 'DB' else 'CR'end as t_type
3* from accs
View created.
SQL> select * from v_accs;
ACCOUNT_NA TXN T_
APC 1 DB
ABC 2 DB
HJJ 3 DB
HJH 4 CR
HJK 5 CR
APC 6 DB
APC 7 DB
ABC 8 DB
ABC 9 DB
HJJ 10 DB
HJJ 11 DB
HJH 12 DB
HJH 13 DB
HJK 14 DB
HJK 15 CR
15 rows selected.
SQL>
rgds, APC -
DECODE OR CASE - Which is better and why
Oracle version: 11.2
Problem: We have a huge table with 10M records, which needs to be processed daily.
While loading data in table we have to handle condition if flag is =1 then '111' else '000'
To implement this which one is efficient solution? .. using CASE or DECODE and why?
Example:
SQL> select flag,case when flag='Y' then '111' else '000' end from (select 'Y' as flag from dual union all select 'N' from dual);
F CAS
Y 111
N 000
SQL> select flag,decode(flag,'Y','111','000') from (select 'Y' as flag from dual union all select 'N' from dual);
F DEC
Y 111
N 000Hi,
For this job, they're equally efficient.
For any job, where DECODE doesn't require a lot more code than CASE, they will be equally efficient.
I recommend (almost) always using CASE. It's easier to understand and debug, and, if written correctly, will never be slower than DECODE.
The only situation where I use DECODE is for very simple tasks (like the one you posted) where this is used inside a very complicated expression, and the slightly less coding needed for DECODE makes the larger statement easier to read. -
Decode and case statement in the update..
Its is more to it, but I want to check with you guys, the experts on this, this look busy to me, it should be a more simplify way of doing it, do you think will work
The government decide to change the ethnic codes, and we have to come with certain rules to in our report, anyway, do you think this will work? again It is more to it I declare my variables, this is just one part of the precedure.
BEGIN
UTL_FILE.fclose_all;
v_file_handle := UTL_FILE.fopen (v_out_path, v_out_file, 'a');
UTL_FILE.put_line (v_file_handle,
CHR (10) || TO_CHAR (SYSDATE, 'DD-MON-YYYY HH:MI:SS')
UTL_FILE.put_line (v_file_handle, 'Entering spbpers_update');
SELECT upd_spbpers_upd_cur IS
spriden_pidm,
szscapp_birth_state,
szscapp_birth_city,
DECODE(szscapp_hisp_or_latino_ind,Y,'2',N,'1'),
DECODE(szscapp_hisp_or_latino_options,XCM,'2',CUB,'2',MEX,'2',PRI,'2',XSM,'2',ESP,'2',XOH,'2'),
DECODE(szscapp_amer_indn_alaska_opt,XAN,'1','1',XCW,'1',XCH,'1',XCK,'1',XNV,'1',XSX,'1'),
DECODE(szscapp_amer_indn_alaska_other,XON,'1') (,IND,'1',JPN,'1',KOR,'1',PAK,'1',PHL,'1',VNM,'1',XEA,'1',XIS,'1',XSA,'1'),
DECODE(szscapp_asian_options,IND,'1',JPN,'1',KOR,'1',PAK,'1',PHL,'1',VNM,'1',XEA,'1',XIS,'1',XSA,'1'), ,
DECODE(szscapp_other_east_asia,(IND,'1',JPN,'1',KOR,'1',PAK,'1',PHL,'1',VNM,'1',XEA,'1',XIS,'1',XSA,'1'),
DECODE(szscapp_other_indian_subcont,XIS,'1'),
DECODE(szscapp_other_southeast_asia,XSA,'1'),
DECODE(szscapp_blk_or_afr_amer_opt,XAA,'1',XAF,'1',XCB,'1',XOA,'1'),
DECODE(szscapp_blk_or_afr_amer_other,XOA,'1'),
DECODE(szscapp_natve_hawaian_options,GUM,'1',XHI,'1',ASM,'1',XOP,'1'),
DECODE(szscapp_hawaiian_other,XOP,'1'),
DECODE(szscapp_white_options,XEU,'1',XME,'1',XOW,'1'),
DECODE(szscapp_white_other(XOW,'1')
FROM
saturn_midd.szscapp
WHERE
spriden_id = szscapp_id
AND spriden_ntyp_code = 'CAPL'
IF upd_spbpers_upd_cur%ISOPEN
THEN
CLOSE upd_spbpers_upd_cur;
END IF;
OPEN upd_spbpers_upd_cur;
LOOP
FETCH upd_spbpers_upd_cur
INTO v_pidm,v_birth_state,v_birth_city,v_latino_ind,v_latino_options,
v_indn_alaska_opt,v_indn_alaska_other,v_asian_options,
v_other_east_asia,v_other_indian_subcont,v_other_southeast_asia,
v_blk_or_afr_amer_opt,v_blk_or_afr_amer_other,v_natve_hawaian_options,
v_hawaiian_other,v_white_options,v_white_other;
EXIT WHEN upd_spbpers_upd_cur%NOTFOUND;
IF upd_spbpers_upd_cur%FOUND
UPDATE saturn.spbpers
set SPBPERS_ETHN_CODE = CASE
WHEN v_latino_ind IS NOT NULL THEN (spbpers_ethn_code = v_latino_ind,spbpers_activity_date = sysdate)
WHEN v_latino_options IS NOT NULL THEN (spbpers_ethn_code = v_latino_options,spbpers_activity_date = sysdate)
WHEN v_indn_alaska_opt IS NOT NULL THEN (spbpers_ethn_code = v_indn_alaska_opt,spbpers_activity_date = sysdate)
WHEN v_indn_alaska_other IS NOT NULL THEN (spbpers_ethn_code = v_indn_alaska_other,spbpers_activity_date = sysdate)
WHEN v_asian_options IS NOT NULL THEN (spbpers_ethn_code = v_asian_options,spbpers_activity_date = sysdate)
WHEN v_other_east_asia IS NOT NULL THEN (spbpers_ethn_code = v_other_east_asia,spbpers_activity_date = sysdate)
WHEN v_other_indian_subcont IS NOT NULL THEN (spbpers_ethn_code = v_other_indian_subcont,spbpers_activity_date = sysdate)
WHEN v_other_southeast_asia IS NOT NULL THEN (spbpers_ethn_code = v_other_southeast_asia,spbpers_activity_date = sysdate)
WHEN v_blk_or_afr_amer_opt IS NOT NULL THEN (spbpers_ethn_code = v_blk_or_afr_amer_opt,spbpers_activity_date = sysdate)
WHEN v_blk_or_afr_amer_other IS NOT NULL THEN (spbpers_ethn_code = v_blk_or_afr_amer_other,spbpers_activity_date = sysdate)
WHEN v_natve_hawaian_options IS NOT NULL THEN (spbpers_ethn_code = v_natve_hawaian_options,spbpers_activity_date = sysdate)
WHEN v_hawaiian_other IS NOT NULL THEN (spbpers_ethn_code = v_hawaiian_other,spbpers_activity_date = sysdate)
WHEN v_white_options IS NOT NULL THEN (spbpers_ethn_code = v_white_options,spbpers_activity_date = sysdate)
WHEN v_white_other IS NOT NULL THEN (spbpers_ethn_code = v_white_other,spbpers_activity_date = sysdate)
WHEN v_birth_state IS NOT NULL THEN (spbpers_stat_code_birth = v_birth_state,spbpers_activity_date = sysdate)
WHEN v_birth_city IS NOT NULL THEN (spbpers_city_birth = v_birth_city,spbpers_activity_date = sysdate)
WHERE spbpers_pidm = v_pidm;
END
END IF;
END LOOP;Did the procedure compile ?
Doesn't look like a right Decode syntax.
DECODE (col1,'VAL1','This','VAL2','That','ElseThis')
means
--Psuedocode
IF col1 = 'VAL1' THEN 'This'
IF col1 = 'VAL2' THEN 'That'
ELSE 'ElseThis'You can use CASE statement Instead of DECODE
CASE
when szscapp_amer_indn_alaska_other
in ('XON','IND','JPN','KOR','PAK' ..... ) THEN '1'
when szscapp_hisp_or_latino_options
in ('XCM','CUB','MEX','PRI','XSM','ESP','XOH' ...) THEN '2'
END SS -
Query to find out sum by using decode or case
Hi, I have data like below and required an output as given below, could you please help me to get the same by using decode / case .....Thanks for your help
INSNAME INSID STATUS
AAA 1000 Scheduled
AAA 1000 Processing
BBB 1001 Inspector
BBB 1001 Scheduled
BBB 1001 Processing
BBB 1001 Inspector
CCC 1002 Scheduled
CCC 1002 Processing
CCC 1002 Inspector
CCC 1002 Scheduled
CCC 1002 Processing
CCC 1002 Inspector
Required Output...
INSNAME INSID sum_of_scheduled sum_of_Processing sum_of_Inspector
AAA 1000 1 1 0
BBB 1001 1 1 2
CCC 1002 2 2 2And if you want it with CASE statement:
WITH test_table AS
SELECT 'AAA' insname, 1000 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'AAA' insname, 1000 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Inspector' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Inspector' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Inspector' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Inspector' status FROM DUAL
SELECT insname
,insid
,SUM(CASE WHEN status = 'Scheduled'
THEN 1
ELSE 0
END
) sum_of_scheduled
,SUM(CASE WHEN status = 'Processing'
THEN 1
ELSE 0
END
) sum_of_processing
,SUM(CASE WHEN status = 'Inspector'
THEN 1
ELSE 0
END
) sum_of_inspector
FROM test_table
GROUP BY insname
,insid;
Regards
Arun -
Hi all!
I have a report in Discoverer Plus (Version 10.1.2.48.18) which contains 2 columns: One with actual spendings and one with budget figures. I want to make a third column which holds actual spendings in percentage of the budget. To do this, I need to make a calculation similar to:
CASE WHEN SUM(budget) <> 0 THEN SUM(spendings)/SUM(budget) ELSE NULL END
However, when I apply this calculation to the third column, my report don't return any numbers at all in any column.
What am I doing wrong? -- Is this a bug, and how should I solve my problem?
~MortenI can get something out if I do this (applying an aggregate function to it all):
SUM(CASE WHEN budget <> 0 THEN spendings/budget ELSE NULL END)
However, this is wrong (Summarizing these percentages doesn't give any meaning).
SQL lookes something like this:
SELECT /*+ NOREWRITE */ o100448.ACCOUNT as E100451,(decode((ADD_MONTHS(o100862.DATE1,-4)),null,to_date(null, 'MMDDYYYY'),to_date(to_char(trunc((ADD_MONTHS(o100862.DATE1,-4)),'YYYY'),'YYYY') || '01','YYYYMM'))) as E101004,MAX(o100448.ACCOUNTNUM) as as100473_100451_NEW,CASE WHEN ( SUM(o100862.BUDGET) ) <> 0 THEN ( SUM(o100862.AMOUNT) )/( SUM(o100862.BUDGET) ) ELSE NULL END as C_1,( SUM(o100862.BUDGET) )*o100448.SIGN as C_3,( SUM(o100862.AMOUNT) )*o100448.SIGN as C_2,GROUPING_ID(o100448.ACCOUNT,o100448.SIGN,(decode((ADD_MONTHS(o100862.DATE1,-4)),null,to_date(null, 'MMDDYYYY'),to_date(to_char(trunc((ADD_MONTHS(o100862.DATE1,-4)),'YYYY'),'YYYY') || '01','YYYYMM')))) as GID
FROM TANGO.TANGO_ACCOUNTS o100448,
TANGO.TANGO_SUMS o100862
WHERE ( (o100862.ORG = o100448.ORG AND o100862.ACCOUNTNUM = o100448.SUBACCOUNTNUM))
AND (o100448.ACCOUNTNUM BETWEEN 30000 AND 79999)
AND (o100862.DIM = '50')
AND (o100448.ORG = 'bru')
GROUP BY GROUPING SETS(( o100448.ACCOUNT,o100448.SIGN,(decode((ADD_MONTHS(o100862.DATE1,-4)),null,to_date(null, 'MMDDYYYY'),to_date(to_char(trunc((ADD_MONTHS(o100862.DATE1,-4)),'YYYY'),'YYYY') || '01','YYYYMM'))) ),( o100448.ACCOUNT,(decode((ADD_MONTHS(o100862.DATE1,-4)),null,to_date(null, 'MMDDYYYY'),to_date(to_char(trunc((ADD_MONTHS(o100862.DATE1,-4)),'YYYY'),'YYYY') || '01','YYYYMM'))) ))
HAVING (GROUP_ID()=0)
ORDER BY GID DESC;
I tried to fire this SQL up in TOAD (or whatever SQL-tool you might have), and columns C_2 and C_3 are empty. Seems like I'm doing something awfully wrong here.....
Any ideas? (There must be some SQL sharks out there ;-p)
~Morten -
Returne between dates in case when
i want to return between dates values on case when clauses on where clauses
like
and
(case
when (cc.segment3 NOT like '4%' and cc.segment3 NOT like '5%')
then (between to_date('01/07/2012','dd/mm/rrrr') and to_date('31/07/2012','dd/mm/rrrr'))
when (cc.segment3 like '4%' or cc.segment3 like '5%')
then (between to_date('01/07/2012','dd/mm/rrrr') and to_date('31/07/2012','dd/mm/rrrr'))
end) h.default_effective_date
the problem is in the = operator but i don't know haw to use this
any help??ok here is the full coad
select
DECODE(SUBSTR (CC.segment1, 1, 1), 'J', 'OFFSHORE', 'I', 'OFFSHORE','IN COUNTRY')branch_nature,
decode(cc.segment1,'A6260','Head Office','Branches') branch_type,
cc.segment1,
decode (h.currency_code,'EGP','mahly','agnaby') currency_code ,
cc.segment3,
t.description,
CASE
when h.currency_code in('EGP') then sum(l.entered_cr)
else 0
end entered_cr,--sum(l.entered_cr) entered_cr,
CASE
when h.currency_code in('EGP') then sum(l.entered_dr)
else 0
end entered_dr, --sum(l.entered_dr) entered_dr,
CASE
when h.currency_code NOT in('EGP') then sum(l.accounted_cr)
else 0
end accounted_Dr,--0 accounted_Dr,
CASE
when h.currency_code NOT in('EGP') then sum(l.accounted_dr)
else 0
end accounted_cr --0 accounted_cr
from apps.gl_je_headers h,
apps.gl_je_lines L,
apps.gl_code_combinations cc,
apps.fnd_flex_values_tl t ,
applsys.fnd_user us,
apps.gl_je_batches b
where h.status = 'P'
and us.user_id = h.Created_by
--and h.currency_code in('EGP')
and l.je_header_id = h.je_header_id
and l.code_combination_id =cc.code_combination_id
and cc.segment3 = t.flex_value_meaning
and cc.segment3 in ('31000020','40505020')
and cc.segment1 in ('A5550','B0010')
-- and (DECODE(SUBSTR (CC.segment1, 1, 1), 'J', 'OFFSHORE', 'I', 'OFFSHORE','IN COUNTRY') =:P_branch_nature OR :P_branch_nature is NULL)
--and (decode(cc.segment1,'A6260','Head Office','Branches') = :P_BRANCH_TYPE OR :P_BRANCH_TYPE is NULL)
and t.description is not null
and t.language ='AR'
and h.je_batch_id = b.je_batch_id
and
(case
when (cc.segment3 NOT like '4%' and cc.segment3 NOT like '5%')
then (between to_date(:P_Start_date,'dd/mm/rrrr') and to_date(:P_END_date,'dd/mm/rrrr'))
when (cc.segment3 like '4%' or cc.segment3 like '5%')
then (between to_date(:P_Start_date2,'dd/mm/rrrr') and to_date(:P_END_date,'dd/mm/rrrr'))
end) h.default_effective_date
and (case
when (cc.segment3 like '4%' or cc.segment3 like '5%')
then (h.default_effective_date)
end) between to_date('01/07/2012','dd/mm/rrrr') and to_date('31/07/2012','dd/mm/rrrr')*/
group by cc.segment3 , t.description , cc.segment1,h.currency_code;
the part between tages need to modigied -
Need help converting Excel IF statement into CASE WHEN statement for Oracle
Hi,
Hope someone can help I have tried various ways of getting this to work, to no avail!
Bascially I have some figures that are minus figures, and I need to add them together to get a movement figure, but I need to treat the minus figures as minus, if that makes sense, rather than the usual... a minus and a minus makes a plus.
For example:- Budget Figure = -1% and Actual Figure = -68% so the movement needs to be -69%.
The IF statement I have been using in Excel is the following:-
=IF(FO110<0,(FP110-(IF(FO110=0,1,FO110)*-1)),FP110-IF(FO110=0,1,FO110))
Which when using the figures as above = -69%
Cell FO = The Budget Figure
Cell FP = The Actual Figure
However, when I created the CASE statement in Oracle, the figure in the query comes back as -0.67, which is oviously not what I want to happen when both actual and budget are minus figures; however when they are a minus and a plus, it works perfectly fine.
Any help on this would be most appreciated!
Kind regards,
AnnmarieHappy I did'n mess something up :)
Nevertheless, don't show it too much around because
case when budget < 0
then actual - case when budget = 0 /* will never happen */
then 1 /* will never happen */
else budget
end * (-1) /* -budget * (-1) remains only */
else actual - case when budget = 0
then 1
else budget
end
endso at least try the following (if case is more readable as decode for you). Let's hope it works as I don't have a Database at hand
case when budget < 0
then actual + budget
else actual - case when budget = 0
then 1
else budget
end
end Regards
Etbin -
Using function on decode or case in query
Hi experts
I am using oracle 11G database,I have to check length of name column value from employee table and if length(name) > 39 then value should be substr(name,0,39)
else value should be name only.
i tried below code
select CASE when length(name) > 39,substr(name,0,39)
else name
END
from employee but its not working ..can I do this using decode too ? ,,which one would be better or this is not a right way ?
ThanksHi,
siebelD wrote:
Hi experts
I am using oracle 11G database,I have to check length of name column value from employee table and if length(name) > 39 then value should be substr(name,0,39)
else value should be name only.
i tried below code
select CASE when length(name) > 39,substr(name,0,39)
else name
END
from employee but its not working ..Review the syntax of the CASE expression in the SQL Language manual. A CASE expression always has at least one THEN clause. Commas are not part of the CASE expression syntax.
can I do this using decode too ? Sure, anything you can do with CASE you can also do with DECODE.
,,which one would be betterThis is one of the many situations where CASE is much shorter, clearer and more efficient than DECODE.
or this is not a right way ?How about
SELECT SUBSTR (name, 1, 39) AS employee_name
FROM employee;?
If name is 39 characters (or less), then <tt> SUBSTR (name, 1, 39) </tt> will return it, unchanged.
Edited by: Frank Kulash on Mar 3, 2013 2:07 PM
Maybe you are looking for
-
Hi everyone Can anybody help me with crystal report - sub report problem as i am new to crystal report. I have created two reports"mainreport" and "subreport" using crysta report XI with the same query for both reports using stored procedure. I set
-
Can no longer get into BIOS, always goes to system diagnostics
After changing some settings in BIOS the system won't boot anymore. But the real problem is that I can not get into the BIOS anymore. When started the system goes straight to System Diagnostics. When I continue I get the message: BootDevice Not Found
-
I am trying to migrate from macbook to a new imac and the looking for source keeps spinning
I am trying to migrate from macbook to a new imac and the looking for source keeps spinning
-
Use image as watermark on all pages of a document
Is there a way to make an image appearing on every page of a document automatically as some kind of watermark ? I managed it to appear on one page, but can't find a solution how to get it automatically to other pages as well. This was achieved by ope
-
Right, hopefully i will be able to get to the bottom of my problem with a good resolution. I purchased a iPhone5 recently on the EE network here in the UK. I backed up my iPhone4 on my iTunes and then sync'd the new phone. However, the new phone will