Get result satisfy both condition in Decode function
I have code working as long as set of codes total is not the same (code1(21)+code2(22)=43, code1(18)+code2(25)=43)
I got wrong data based on following
select dt,
avg(decode(code1+code2,'21'+'22',midpoint)) C1,
avg(decode(code1+code2,'18'+'25',midpoint)) C2
from tablename
where dt>=to_date('02/27/2000','mm/dd/yyyy')
and dt<=to_date('2/26/2001','mm/dd/yyyy')
and ( (code1=21 and code2=22)
or (code1=18 and code2=25)
) group by dt
How come I can use decode function based on code1 and code2.
Linda
Evening Linda,
I have tried yourt sql with the following sample data from table T1 ...
select C1,D1,N1,N2,N3 from T1;
C1 D1 N1 N2 N3
A 02/27/2000 18 25 -1
B 02/27/2000 21 22 -1
Note : N3 is your midpoint column from your sql.
Test SQL below
select C1,D1,N1,N2,
avg(decode(N1+N2,'21'+'22',N3,N1+N2)) AVG_A,
avg(decode(N1+N2,'18'+'25',N3,N1+N2)) AVG_B
from T1
where D1 BETWEEN to_date('02/27/2000','mm/dd/yyyy') AND to_date('2/26/2001','mm/dd/yyyy')
and
((N1=21 and N2=22) or (N1=18 and N2=25))
group by C1,D1,N1,N2
Based on that data, I always get N3 (midpoint) back as N1+N2 = 43 as shown below ...
C1 D1 N1 N2 AVG_A AVG_B
A 02/27/2000 18 25 -1 -1
B 02/27/2000 21 22 -1 -1
When I change the decode(N1+N2.. to say decode(N1+N2+1.. then I get the second part of the decode back (I shorted the SQL since I only have 2 test records anyway) ...
select C1,D1,N1,N2,
avg(decode(N1+N2 +1 ,'21'+'22',N3,N1+N2)) AVG_A,
avg(decode(N1+N2 +1 ,'18'+'25',N3,N1+N2)) AVG_B
from T1
group by C1,D1,N1,N2
C1 D1 N1 N2 AVG_A AVG_B
A 02/27/2000 18 25 43 43
B 02/27/2000 21 22 43 43
When the N1+N2 do not match to 43, the FOURTH part of the decode is called, which you did not specify in your original sql (there is only 3, nothing came after midpoint). Maybe I am not understanding what you want for an answer based on this sample data that I have tried. Maybe show a data set that you have showing what you are wanting for an answer. Remember DECODE has 4 parts to it, if they dont match the search ('21'+'22') then the FOURTH part is returned, which you did not define, so a NULL would get returned (see below).
select C1,D1,N1,N2,
avg(decode(N1+N2 +1 ,'21'+'22',N3)) AVG_A,
avg(decode(N1+N2 +1 ,'18'+'25',N3)) AVG_B
from T1
group by C1,D1,N1,N2
C1 D1 N1 N2 AVG_A AVG_B
A 02/27/2000 18 25
B 02/27/2000 21 22
Thanks,
Tyler D. ([email protected])
Similar Messages
-
How to get result of Select from stored function.
I need to get result of select from a stored function.
In the end of my stored function I makes final select (four columns).
How it can be retrived from function?Hi,
A function can only return one value, but it sounds like you want to return 4 values.
The one value that you return can be a record, with many columns, such as a ROWTYPE, or a TYPE that you define.
You can return an XMLTYPE that has whatever elements you want.
You can write a procedure that has several OUT parameters. (You can have OUT parameters in a function, but a lot of people find that confusing.)
In very special circumstance, you might consider returning a string that is a delimited list of values, such as '7639,SMITH,,17-DEC-1980'.
Someoneelse has a good point.
We could give a better answer if you ask a specific question, like:
"I have this table ...
I want a function such that, if I call it with these parameters ... I get ...
but if I call it like this ... then I get ..." -
How to write decode function in plsql
hi
guys
can u plz help me in this topic
.........Hi ,
You could use the decode function in an SQL statement as follows:
SELECT supplier_name,
decode(supplier_id, 10000, 'IBM',
10001, 'Microsoft',
10002, 'Hewlett Packard',
'Gateway') result
FROM suppliers;
The above decode statement is equivalent to the following IF-THEN-ELSE statement:
IF supplier_id = 10000 THEN
result := 'IBM';
ELSIF supplier_id = 10001 THEN
result := 'Microsoft';
ELSIF supplier_id = 10002 THEN
result := 'Hewlett Packard';
ELSE
result := 'Gateway';
END IF;
The decode function will compare each supplier_id value, one by one.
Regards,
xaheer -
Retrn result only if both conditions are true
I have this query to get list of names that are associated with
a keyword called 123 but do not belong to a item named 456
select distinct t1.lname, t1.sname , t5.rname
from table1 t1, table2 t2, tale3 t3,
table4 t4, table5 t5,
TABLE(xmlsequence(extract(t4.xmlcol,'/cs/lo/c'))) k
where t1.id = a.id
and t3.id = a.id
and t4.id = t3.id
and t5.id = t3.id
<--1st condition -->
and extractValue(k.column_value,'c/L/keyword/@id') in ('123')
<-- 2nd conditon-->
and t5.item not in (select distinct t6.name from table7 t7, table6 t6
where t7.id= t6.id
and t6.ref_id = 456)
In my database, I have 2 names. for a
given lname in the output, it is associated
with 2 rnames.One rname is associated with
keyword 123 and the other rname is associated
with item 456. Something like this:
lname sname rname
jakal jkl r123 --> associated with keyword 123
jakal jkl r456 --> associated with item 456
So when I run without condition1 and condition2 above is what I get.
That is jakal is associated with both r123 and r456.
However in my first query, I am trying to get the lnames and snames that are associated with keyword 123 but are not associated with item 456.
I want both conditions true to be displayed.
However when I write the query as mentioned initially, I get the row back.Not sure if I understand the problem correctly.
But simplistically boolean algebra says that if you want a result only if both conditions are true, use AND.
E.g.
select * from table where <condition1> and <condition2>
Now if this does not work, it means that the conditions do not do what you think these should be doing. -
Case statement and Decode function both are not working in Select cursor.
I have tried both the Case statement and Decode function in Select cursor, but both the things are not working. On the other hand both the things work in just select statement.
See the first column in select (PAR_FLAG), I need to have this evaluated along with other fields. Can you please suggest some thing to make this work. And also I would like to
know the reason why decode is not working, I heard some where Case statement do not work with 8i.
Author : Amit Juneja
Date : 06/20/2011
Description:
Updates the Diamond MEMBER_MASTER table with the values from
INC.MEM_NJ_HN_MEMBER_XREF table.
declare
rec_cnt number(12) := 0;
commit_cnt number(4) := 0;
cursor select_cur is
Select DECODE(1,
(Select 1
from hsd_prov_contract R
where R.seq_prov_id = PM.seq_prov_id
and R.line_of_business = H.line_of_business
and R.PCP_FLAG = 'Y'
and R.participation_flag = 'P'
and SYSDATE between R.EFFECTIVE_DATE AND
NVL(R.TERM_DATE,
TO_DATE('31-DEC-9999', 'DD-MON-YYYY'))),
'Y',
'N') PAR_FLAG,
H.SEQ_ELIG_HIST,
H.SEQ_MEMB_ID,
H.SEQ_SUBS_ID,
H.SUBSCRIBER_ID,
H.PERSON_NUMBER,
H.EFFECTIVE_DATE,
H.TERM_DATE,
H.TERM_REASON,
H.RELATIONSHIP_CODE,
H.SEQ_GROUP_ID,
H.PLAN_CODE,
H.LINE_OF_BUSINESS,
H.RIDER_CODE_1,
H.RIDER_CODE_2,
H.RIDER_CODE_3,
H.RIDER_CODE_4,
H.RIDER_CODE_5,
H.RIDER_CODE_6,
H.RIDER_CODE_7,
H.RIDER_CODE_8,
H.MEDICARE_STATUS_FLG,
H.OTHER_STATUS_FLAG,
H.HIRE_DATE,
H.ELIG_STATUS,
H.PREM_OVERRIDE_STEP,
H.PREM_OVERRIDE_AMT,
H.PREM_OVERRIDE_CODE,
H.SEQ_PROV_ID,
H.IPA_ID,
H.PANEL_ID,
H.SEQ_PROV_2_ID,
H.SECURITY_CODE,
H.INSERT_DATETIME,
H.INSERT_USER,
H.INSERT_PROCESS,
H.UPDATE_DATETIME,
H.UPDATE_USER,
H.UPDATE_PROCESS,
H.USER_DEFINED_1,
H.SALARY,
H.PEC_END_DATE,
H.REASON_CODE,
H.PEC_WAIVED,
H.BILL_EFFECTIVE_FROM_DATE,
H.BILLED_THRU_DATE,
H.PAID_THRU_DATE,
H.SUBSC_DEPT,
H.SUBSC_LOCATION,
H.USE_EFT_FLG,
H.BENEFIT_START_DATE,
H.SEQ_ENROLLMENT_RULE,
H.MCARE_RISK_ACCRETION_DATE,
H.MCARE_RISK_DELETION_DATE,
H.MCARE_RISK_REFUSED_DATE,
H.COMMENTS,
H.USER_DEFINED_2,
H.USER_DEFINED_3,
H.RATE_TYPE,
H.PCPAA_OCCURRED,
H.PRIVACY_ON,
H.PCP_CHANGE_REASON,
H.SITE_CODE,
H.SEQ_SITE_ADDRESS_ID,
PM.seq_prov_id rendered_prov
from hsd_member_elig_history H,
INC.PCP_REASSIGN_RPRT_DATA P,
hsd_prov_master PM
where P.subscriber_id = H.subscriber_id
and P.rendered_pcp = PM.provider_ID
and H.elig_status = 'Y'
and (H.term_date is NULL or H.term_date >= last_day(sysdate))
order by H.Seq_memb_id;
begin
for C in select_cur loop
rec_cnt := rec_cnt + 1;
update hsd_member_elig_history
set term_date = TRUNC(SYSDATE - 1),
term_reason = 'PCPTR',
update_datetime = SYSDATE,
update_user = USER,
update_process = 'TD33615'
where seq_elig_hist = C.seq_elig_hist
and seq_memb_id = C.seq_memb_id;
INSERT INTO HSD_MEMBER_ELIG_HISTORY
(SEQ_ELIG_HIST,
SEQ_MEMB_ID,
SEQ_SUBS_ID,
SUBSCRIBER_ID,
PERSON_NUMBER,
EFFECTIVE_DATE,
TERM_DATE,
TERM_REASON,
RELATIONSHIP_CODE,
SEQ_GROUP_ID,
PLAN_CODE,
LINE_OF_BUSINESS,
RIDER_CODE_1,
RIDER_CODE_2,
RIDER_CODE_3,
RIDER_CODE_4,
RIDER_CODE_5,
RIDER_CODE_6,
RIDER_CODE_7,
RIDER_CODE_8,
MEDICARE_STATUS_FLG,
OTHER_STATUS_FLAG,
HIRE_DATE,
ELIG_STATUS,
PREM_OVERRIDE_STEP,
PREM_OVERRIDE_AMT,
PREM_OVERRIDE_CODE,
SEQ_PROV_ID,
IPA_ID,
PANEL_ID,
SEQ_PROV_2_ID,
SECURITY_CODE,
INSERT_DATETIME,
INSERT_USER,
INSERT_PROCESS,
UPDATE_DATETIME,
UPDATE_USER,
UPDATE_PROCESS,
USER_DEFINED_1,
SALARY,
PEC_END_DATE,
REASON_CODE,
PEC_WAIVED,
BILL_EFFECTIVE_FROM_DATE,
BILLED_THRU_DATE,
PAID_THRU_DATE,
SUBSC_DEPT,
SUBSC_LOCATION,
USE_EFT_FLG,
BENEFIT_START_DATE,
SEQ_ENROLLMENT_RULE,
MCARE_RISK_ACCRETION_DATE,
MCARE_RISK_DELETION_DATE,
MCARE_RISK_REFUSED_DATE,
COMMENTS,
USER_DEFINED_2,
USER_DEFINED_3,
RATE_TYPE,
PCPAA_OCCURRED,
PRIVACY_ON,
PCP_CHANGE_REASON,
SITE_CODE,
SEQ_SITE_ADDRESS_ID)
values
(hsd_seq_elig_hist.nextval,
C.SEQ_MEMB_ID,
C.SEQ_SUBS_ID,
C.SUBSCRIBER_ID,
C.PERSON_NUMBER,
trunc(SYSDATE),
C.TERM_DATE,
C.TERM_REASON,
C.RELATIONSHIP_CODE,
C.SEQ_GROUP_ID,
C.PLAN_CODE,
C.LINE_OF_BUSINESS,
C.RIDER_CODE_1,
C.RIDER_CODE_2,
C.RIDER_CODE_3,
C.RIDER_CODE_4,
C.RIDER_CODE_5,
C.RIDER_CODE_6,
C.RIDER_CODE_7,
C.RIDER_CODE_8,
C.MEDICARE_STATUS_FLG,
C.OTHER_STATUS_FLAG,
C.HIRE_DATE,
C.ELIG_STATUS,
C.PREM_OVERRIDE_STEP,
C.PREM_OVERRIDE_AMT,
C.PREM_OVERRIDE_CODE,
C.SEQ_PROV_ID,
C.IPA_ID,
C.PANEL_ID,
C.SEQ_PROV_2_ID,
C.SECURITY_CODE,
SYSDATE,
USER,
'TD33615',
SYSDATE,
USER,
'TD33615',
C.USER_DEFINED_1,
C.SALARY,
C.PEC_END_DATE,
C.REASON_CODE,
C.PEC_WAIVED,
C.BILL_EFFECTIVE_FROM_DATE,
C.BILLED_THRU_DATE,
C.PAID_THRU_DATE,
C.SUBSC_DEPT,
C.SUBSC_LOCATION,
C.USE_EFT_FLG,
C.BENEFIT_START_DATE,
C.SEQ_ENROLLMENT_RULE,
C.MCARE_RISK_ACCRETION_DATE,
C.MCARE_RISK_DELETION_DATE,
C.MCARE_RISK_REFUSED_DATE,
C.COMMENTS,
C.USER_DEFINED_2,
C.USER_DEFINED_3,
C.RATE_TYPE,
C.PCPAA_OCCURRED,
C.PRIVACY_ON,
C.PCP_CHANGE_REASON,
C.SITE_CODE,
C.SEQ_SITE_ADDRESS_ID);
commit_cnt := commit_cnt + 1;
if (commit_cnt = 1000) then
dbms_output.put_line('Committed updates for 1000 records.');
commit;
commit_cnt := 0;
end if;
end loop;
commit;
dbms_output.put_line('Total number of MEMBER_ELIG_HISTROY records inserted : ' ||
rec_cnt);
exception
when others then
raise_application_error(-20001,
'An error was encountered - ' || sqlcode ||
' -error- ' || sqlerrm);
end;user10305724 wrote:
I have tried both the Case statement and Decode function in Select cursor, but both the things are not working. Please define what you mean by not working even if your computer screen is near the internet we can't see it.
You should also look at the FAQ about how to ask a question
SQL and PL/SQL FAQ
Particularly *9) Formatting with {noformat}{noformat} Tags* and posting your version.
know the reason why decode is not working, I heard some where Case statement do not work with 8i.
Does this mean you are using 8i? Then scalar sub queries - selects within the select list, are not supported, along with CASE in PL/SQL.
Select DECODE(1,
* (Select 1
from hsd_prov_contract R
where R.seq_prov_id = PM.seq_prov_id
and R.line_of_business = H.line_of_business
and R.PCP_FLAG = 'Y'
and R.participation_flag = 'P'
and SYSDATE between R.EFFECTIVE_DATE AND
NVL(R.TERM_DATE,
TO_DATE('31-DEC-9999', 'DD-MON-YYYY')))*,
'Y',
'N') PAR_FLAG,
>
exception
when others then
raise_application_error(-20001,
'An error was encountered - ' || sqlcode ||
' -error- ' || sqlerrm);
http://tkyte.blogspot.com/2008/01/why-do-people-do-this.html -
Wat is wrong in this decode function, it doesn get executed,please help me
wat is wrong in this decode function, it doesn get executed,please help me
decode(DVI.COMPANY||'.')||
DECODE(DVI.COST_CENTRE||'.')||
DECODE(DVI.ACCOUNT||'.')||
DECODE(DVI.LOCATION||'.')||
DECODE(DVI.PRODUCT||'.')||
DECODE(DVI.FUTURE)company_id,Hi,
I could not understand what you are trying to do with decode here.
The standard way of using the decode is like :
SQL>SELECT DECODE (1 , 1 ,'TRUE','FALSE') FROM DUAL;
DECO
TRUE
1 row selected.
1* SELECT DECODE (1 , 0 ,'TRUE','FALSE') FROM DUAL
SQL>/
DECOD
FALSE
1 row selected.
Please explain what you want to do with DECODE ?
Regards -
DECODE function in Oracle gives duplicate data
Hi,
I want to retrieve distinct result using decode function and given below are the queries for that.
SELECT DISTINCT O.*, DECODE(TA,'AP',AMT,'AC1',AMT,'AC2',AMT,'AC3',AMT,'AC4',AMT,NULL) AS CA,
DECODE(TA,'AD1',AMT,NULL) AS DA FROM HT O, HIS H, V_CV
WHERE O.HID=H.HID AND H.HTYP='TRN' AND O.HID = V.HID AND V.CID = '5' ORDER BY CA ASC;
which gives me
HID TA PT AMT CA DA
2 A V 4.1 4.1 null
3 C V 14.02 14.02 null
1 D V 5.1 null 5.1
4 D V 6.21 null 6.21
which is correct but i want to do sorting on two different column which do not exit in the database and they are dynamic.So i Modified the Query but i am getting duplicate results
SELECT DISTINCT O.*,
DECODE(o.TA,cr.TA,o.AMT,NULL) AS CA,
DECODE(o.TA,de.TA,o.AMT,NULL) AS DA
FROM HT O,
HIS H,
V_CV,
(select TA from RTD where Ttype= 'C') cr,
(select TA from RTD where Ttype= 'D') de
WHERE
O.HID=H.HID
AND H.HTYP='TRN'
AND O.HID = V.HID
AND V.CID = '5'
ORDER BY CA ASC
which gives me
HID TA PT AMT CA DA
2 A V 4.1 4.1 null
3 C V 14.02 14.02 null
3 C V 14.02 null null
1 D V 5.1 null 5.1
1 D V 5.1 null null
4 D V 6.21 null 6.21
2 A V 4.1 null null
which is obviously giving HID 2,3,1 as duplicate value since DECODE select a value and compares with all item where i want only single comparison.
Any help regarding this would be appreciated.
Edited by: Pawanchoure on Jul 5, 2011 7:14 AMWhy don't you have any join conditions specified for your two inline views cr and de?
If you want help with a query you should post DDL and inserts statements to create enough test data for someone to produce the set of results that you are looking for.
HTH -- Mark D Powell -- -
DECODE function to validate date value and sort the records
Hi Friends,
I am looking for some query which can give me the required output,
I need to do this using SQL query only and I have tried using the MIN() and MAX() functions it was working fine with limited data, now after inserting the last record(in the below table) which has date for start_range and end_range in(mm/dd/yyyy hh24:mi:ss) format.
Because the data type is VARCHAR2 if I am using the MIN() function it is sorting as a string value, hence the min date is incorrect. I tried using validating the value to date or non date and tried to using MIN() and MAX() functions using the DECODE function, I am getting this error "ORA-01840: input value not long enough for date format".
select table_name,
DECODE(substr(START_RANGE,3,1)||substr(START_RANGE,6,1)||substr(START_RANGE,11,1)||substr(START_RANGE,14,1)||substr(START_RANGE,17,1),'// ::',
to_char(min(to_date(start_range,'mm/dd/yyyy hh24:mi:ss')),'MM/DD/YYYY HH24:MI:SS'),min(start_range)) MIN_RUNS_START_RANGE,
DECODE(substr(END_RANGE,3,1)||substr(A.END_RANGE,6,1)||substr(END_RANGE,11,1)||substr(END_RANGE,14,1)||substr(END_RANGE,17,1),'// ::',
to_char(max(to_date(END_RANGE,'mm/dd/yyyy hh24:mi:ss')),'MM/DD/YYYY HH24:MI:SS'),max(END_RANGE)) MAX_RUNS_END_RANGE
from MY_TABLE
GROUP BY table_name,
(substr(START_RANGE,3,1)||substr(START_RANGE,6,1)||substr(START_RANGE,11,1)||substr(START_RANGE,14,1)||substr(START_RANGE,17,1)),
(substr(END_RANGE,3,1)||substr(END_RANGE,6,1)||substr(END_RANGE,11,1)||substr(END_RANGE,14,1)||substr(END_RANGE,17,1))
Can sombody please advise what is the best way I can query this data with the required output.
The following are the source table and records and the out put records using the sql query.
MY_TABLE
TABLE_NAME(VARCHAR2),START_RANGE(VARCHAR2),END_RANGE(VARCHAR2)
TABLE1,1000,10000
TABLE2,ABCD,EEEE
TABLE3,01/12/2010 00:00:00,12/31/2010 23:59:59
TABLE1,10001,20000
TABLE2,EEEF,GGGG
TABLE3,01/01/2011 00:00:00,01/31/2011 23:59:59
OUTPUT :
TABLE_NAME,MIN(START_RANGE),MAX(END_RANGE)
TABLE1,1000,20000
TABLE2,ABCD,GGGG
TABLE3,01/12/2010 00:00:00,01/31/2011 23:59:59
Thanks
Kalycsi also think this is a very bad table design ...
but if you are not able to change it, you could split the select (date and non-date data) and combine the result with UNION like this:
with t as
SELECT 'TABLE1' table_name,'1000' start_range,'10000' end_range FROM dual UNION
SELECT 'TABLE2','ABCD','EEEE' FROM dual UNION
SELECT 'TABLE3','01/12/2010 00:00:00','12/31/2010 23:59:59' FROM dual UNION
SELECT 'TABLE1','10001','20000' FROM dual UNION
SELECT 'TABLE2','EEEF','GGGG' FROM dual UNION
SELECT 'TABLE3','01/01/2011 00:00:00','01/31/2011 23:59:59' FROM dual
SELECT table_name,
TO_CHAR(MIN(TO_DATE(start_range, 'MM/DD/YYYY HH24:MI:SS')), 'MM/DD/YYYY HH24:MI:SS'),
TO_CHAR(MAX(TO_DATE(end_range, 'MM/DD/YYYY HH24:MI:SS')), 'MM/DD/YYYY HH24:MI:SS')
FROM t
WHERE start_range LIKE '%/%/%:%:%'
GROUP BY table_name
UNION
SELECT table_name,
MIN(start_range),
MAX(end_range)
FROM t
WHERE start_range NOT LIKE '%/%/%:%:%'
GROUP BY
table_name;
TABLE_ MIN_VALUE MAX_VALUE
TABLE1 1000 20000
TABLE2 ABCD GGGG
TABLE3 01/12/2010 00:00:00 01/31/2011 23:59:59 -
Decode function in Update statement
Hello everyone,
I'm trying to write a query where I can update a pastdue_fees column in a book_trans table based on a difference between return_dte and due_dte columns.
I am using Oracle SQL. This is what I have so far for my decode function:
SQL> SELECT
2 DECODE(SIGN((return_dte - due_dte)*2),
3 '-1', '0',
4 '1', '12', 'Null')
5 FROM book_trans;
DECO
Null
12
Null
0
So the logic is that if the sign is -1, the value in return_dte column should be 0; if it's +1 then it's 12 and everything else is Null.
So now, I need to enter my decode function into the update statement to update the columns. However, I get error messages.
The logic should be:
UPDATE book_trans SET PastDue_fees = decode(expression)
I've given it a couple of different tries with the following results:
SQL> UPDATE book_trans
2 SET pastdue_fees = SELECT
3 DECODE(SIGN((return_dte - due_dte)*2),
4 '-1', '0',
5 '1', '12', 'Null')
6 FROM book_trans;
SET pastdue_fees = SELECT
ERROR at line 2:
ORA-00936: missing expression
SQL> UPDATE book_trans
2 SET pastdue_fees =
3 DECODE(SIGN((return_dte - due_dte)*2),
4 '-1', '0',
5 '1', '12', 'Null')
6 FROM book_trans;
FROM book_trans
ERROR at line 6:
ORA-00933: SQL command not properly ended
Any help or tips would be greatly appreciated as I've been taking SQL for about six weeks and not very proficient!
Thanks!882300 wrote:
Hello everyone,
I'm trying to write a query where I can update a pastdue_fees column in a book_trans table based on a difference between return_dte and due_dte columns.
I am using Oracle SQL. This is what I have so far for my decode function:
SQL> SELECT
2 DECODE(SIGN((return_dte - due_dte)*2),
3 '-1', '0',
4 '1', '12', 'Null')
5 FROM book_trans;
DECO
Null
12
Null
0
So the logic is that if the sign is -1, the value in return_dte column should be 0; if it's +1 then it's 12 and everything else is Null.
So now, I need to enter my decode function into the update statement to update the columns. However, I get error messages.
The logic should be:
UPDATE book_trans SET PastDue_fees = decode(expression)
I've given it a couple of different tries with the following results:
SQL> UPDATE book_trans
2 SET pastdue_fees = SELECT
3 DECODE(SIGN((return_dte - due_dte)*2),
4 '-1', '0',
5 '1', '12', 'Null')
6 FROM book_trans;
SET pastdue_fees = SELECT
ERROR at line 2:
ORA-00936: missing expression
SQL> UPDATE book_trans
2 SET pastdue_fees =
3 DECODE(SIGN((return_dte - due_dte)*2),
4 '-1', '0',
5 '1', '12', 'Null')
6 FROM book_trans;
FROM book_trans
ERROR at line 6:
ORA-00933: SQL command not properly ended
Any help or tips would be greatly appreciated as I've been taking SQL for about six weeks and not very proficient!
Thanks!If you really really really want to update the entire table, the syntax would be...
UPDATE book_trans
SET
pastdue_fees = DECODE(SIGN((return_dte - due_dte)*2), -1, 0, 1, 12, Null);I took out all the single quotes. If you actually have a string column and you're storing entirely numbers in it then it should be declared as a NUMBER column and not a character (varchar2) column.
ALWAYS use the proper data type, it'll save you a ton of headaches in the future.
Also, since you're new to the forum, please read the FAQ so you learn the etiquette and what not.
http://wikis.sun.com/display/Forums/Forums+FAQ -
DECODE Function in Oracle Froms 6.0
I have discovered that in Oracle Forms 6.0 (Version 6.0.5.34.0) the DECODE Function performs an implicit TO_CHAR conversion that the DECODE Function in the Oracle 8 database does not.
I discovered this when working with dates on the form. Dates prior to 2000 were not formatting correctly and all dates were not sorting correctly. By adding TO_DATE with the correct format mask outside of the DECODE statement the correct results were returned.
When trouble shooting this through SQL Navigator connected directly to the database, the correct results were returned without having to use the TO_DATE function.Why don't you have any join conditions specified for your two inline views cr and de?
If you want help with a query you should post DDL and inserts statements to create enough test data for someone to produce the set of results that you are looking for.
HTH -- Mark D Powell -- -
Decode function involving 2 fields (urgent)
Hi .. I am trying to use the decode function involving 2 fields.
The query runs but does not produce the correct results. If
anyone knows, please reply .. Usually decode function involves
one column only but I have more
than one. The situation is : If ins_code = 2 or special_code =
MMI, put a 'Y', otherwise, it's a 'N'. Does anyone know how to
put this in a decode statement? ThanksThanks, Suresh ... but I have to put this condition using the
decode statement because that's how the rest of the program is
written. It's part of the script that I have to change. The
current statement is this: decode(ins_code, '2', 'Y', 'N'). I
have to change it to ins_code =2 or special_feature = MMI.
Please reply ... thanks a lot.... -
About Decode Function in oracle(on 28th)
Hi all
Can i use Decode function with Distinct as following.
SUM(DISTINCT DECODE(LR.QUESTIONNAIRE_ID,62, L.WORK_SPACE))
RegardsHi,
The argument to SUM can be any numeric expression, including a DECODE statement that evaluates to a number.
"SUM (DISTINCT ..." is very suspicious. If you have three rows, with values 1, 1 and 3, the "SUM (DISTINCT" is 4. Is that what you want?
Perhaps your problem is that whenever you have more than one row, they will always be identical (e.g., a one-to-may join), so you have rows with 1, 1, and 1, from which you want to return 1. Most people do that by adding the value to the GROUP BY list, or by using MAX or MIN instead of SUM: they get the same results, but the code is clearer, and it works on non-numeric columns. -
Decode Function I asked this in the wrong section so I hope Im right now!
select
a11.SEQ_MEMB_ID,
a11.Auth_Number,
a11.Admit_Primary_Date,
a11.Reviewer,
Decode (a11.Reviewer,
CLC, '2',
HBR, '3',
RAB, '4')
a11.admit_primary_date
from windsoradm.auth_master a11;
I am trying to get if the Reviewer is CLC then they are code 2 HBR Code 3 etc and it does not work. I take out the decode function and the query works. I am getting ORA-00923 FROM KEYWORD NOT FOUND WHERE EXPECTED.Hi,
Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables involved.
Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
Simplify the problem as much as possible. Remove all tables and columns that play no role in this problem.
Always say which version of Oracle you're using.
This is the right forum for this question. Mark your question in the other forum {message:id=10266930} as "Answered", so people won't waste their time.
925518 wrote:
select
a11.SEQ_MEMB_ID,
a11.Auth_Number,
a11.Admit_Primary_Date,
a11.Reviewer,
Decode (a11.Reviewer,
CLC, '2',
HBR, '3',
RAB, '4')
a11.admit_primary_date
from windsoradm.auth_master a11;
I am trying to get if the Reviewer is CLC then they are code 2 HBR Code 3 etc and it does not work. I take out the decode function and the query works. I am getting ORA-00923 FROM KEYWORD NOT FOUND WHERE EXPECTED.That's the correct DECODE syntax, where CLC, HBR and RAB are columns, and you want to return a string (not a number) such as '2'.
If 'CLC', 'HBR' and 'RAB' are literal values, then enclose them in single-quotes. If you want to return a number, don't put it in quotes. (Anyhting inside single-quotes is a string literal.)
Perhaps you meant something like:
, DECODE ( a11.Reviewer
, 'CLC' , 1
, 'HBR' , 2
, 'RAB' , 3
) AS reviewer_codeThere's an error right after the DECODE. You can't use a11.admit_primary_date as a column alias. (This is probably what's causing the ORA-00923 error.) Either a11_admit_primary_date (with an underscore instead of a dot) or admit_primary_date would be okay. If a11.admit_priomary_date is the next column in the SELECT clause, put a comma befor it.
You need a FROM clause. -
Hi,
I am new to Oracle Development.
MY Requirement:
I need to populate a column 'ISMSG' with YES / NO based on the following condition
When code = 'S' or 'O'......the column 'ISMSG' should display YES else NO..
ie output should be like
CODE ISMSG
A NO
S YES
S YES
O YES
My code for Decode function:
CASE
WHEN c.code = 'S' THEN decode( c.code, 'S','YES','NO')
WHEN c.code = 'O' THEN decode( c.code, 'O','YES','NO')
END ISMSG,
Is my code correct? If so is this the best way to do it?
If not please correct the code.
Is there an efficient way to do it?
Thanks in advance.Hi,
You can use either CASE or DECODE for that; there's no need to use both.
Using CASE:
SELECT c.code
, CASE
WHEN c.code IN ('S', '0') THEN 'YES'
ELSE 'NO'
END AS ismsg
FROM table_x c;Using DECODE:
SELECT c.code
, DECODE ( c.code
, 'S' , 'YES'
, '0' , 'YES'
'NO'
) AS ismsg
FROM table_x c;I suggest you forget about DECODE, and always use CASE, at least while you're just starting out.
CASE can do anything that DECODE can do. There are a few, simple situations where DECODE is a little shorter than CASE, and therefore a little clearer, but only a little, and, as I said, these are only simple situations, and CASE is clear enough in them.
DECODE is never much shorter or clearer than CASE. The converse is not true: there are many situations where CASE is much, much shorter and clearer than DECODE. -
Hi All,
There is a Amount column in my table contain both credit(-amount) and debit(+amount) ,i hv to split this Amount in two column of positive amount(debit) and negative amount (credit) as i am using decode function to get output but i am unable to get , can anyone help how to write the decode function.
Thanks...Hi,
Something like this,
SQL> With t As(select -29 amount from dual union all
2 select 5 amount from dual union all
3 select -10 amount from dual)
4 SELECT Amount
5 , Decode(Sign(amount), 1,Amount) As Debit
6 , Decode(Sign(amount),-1,Amount) As Credit
7 FROM t;
AMOUNT DEBIT CREDIT
-29 -29
5 5
-10 -10or
SQL> With t As(select -29 amount from dual union all
2 select 5 amount from dual union all
3 select -10 amount from dual)
4 SELECT Amount
5 , Decode(sign(amount),1,'Debit',-1,'Credit') Amount
6 FROM t;
AMOUNT AMOUNT
-29 Credit
5 Debit
-10 CreditRegards,
Christian Balz
Edited by: Christian Balz on 16/06/2009 21:36
Maybe you are looking for
-
Any way to highlight pdf file in Intermedia?
markup only works for text file,right?any way to highlight pdf file in Intermedia?Thanks.
-
i just bought a new computer and need to deaothorize my old computer but the problem is I can't open up my old computer to do that. So how can authorize my new computer?
-
Open QT window in middle of screen?
I want quicktime movies to stop opening in the far left uppermost corner of my monitor. I have a 32 inch screen and always have to drag it to a more coomfortable viewing area. Somoen told me to use applescript but I can't get this to work. Besides, I
-
Preserving configuration settings in the configuration copy
Hello, Does anybody know how to copy Project Configuration preserving all configuration settings for each mapping in the project? I have about 30 mappings and each mapping has hints for parallel execution. I need to have a copy of the configuration w
-
SCOM 2012 r2 - Add GW Sitename on a later stage
Hi, Does anyone know how to add sitename to a spesific Gateway server without rerunning the Microsoft.EnterpriseManagement.GatewayApproval.exe tool? regards, lostcase