Home » SQL & PL/SQL » SQL & PL/SQL » Decode syntax question
Decode syntax question [message #8908] |
Thu, 02 October 2003 11:18 |
rachana
Messages: 27 Registered: August 2003
|
Junior Member |
|
|
I want to do the following-
if line_facts.category is
1. Not specified, or
2. substandard, or
3. standard
then add up the line_facts.amount, else show a '0'
nvl(sum(Decode(line_facts.category, 'not specified', line_facts.amount, 'standard', line_facts.amount, 'substandard', line_facts.amount, '0')), '0')
Is this the correct syntax?
thanks.
|
|
|
Re: Decode syntax question [message #8909 is a reply to message #8908] |
Thu, 02 October 2003 11:43 |
Art Metzer
Messages: 2480 Registered: December 2002
|
Senior Member |
|
|
Rachana,
Don't put your zeroes in quotes. Otherwise, completely correct.
Here's my little test:SQL> CREATE TABLE t (x VARCHAR2(1), n NUMBER);
SQL> INSERT INTO t VALUES ('A',7);
SQL> INSERT INTO t VALUES ('A',5);
SQL> INSERT INTO t VALUES ('B',3);
SQL> INSERT INTO t VALUES ('B',9);
SQL> INSERT INTO t VALUES ('B',8);
SQL> INSERT INTO t VALUES ('C',1);
SQL> INSERT INTO t VALUES ('D',2);
SQL> INSERT INTO t VALUES ('D',3);
SQL> COMMIT;
Commit complete.
SQL> SELECT NVL(SUM(DECODE(x,'A',n,'C',n,<font color=red>0</font>)),<font color=red>0</font>) FROM t;
NVL(SUM(DECODE(X,'A',N,'C',N,<font color=red>0</font>)),<font color=red>0</font>)
-----------------------------------
13
SQL> Good luck, Rachana.
A.
|
|
|
Re: Decode syntax question [message #8947 is a reply to message #8908] |
Mon, 06 October 2003 03:30 |
Rij
Messages: 12 Registered: September 2003
|
Junior Member |
|
|
well you need to put the nvl after teh decode statement..
the correct statement wud be
sum
( decode
(nvl (line_facts.category,0),line_facts.category, 'not specified', line_facts.amount, 'standard', line_facts.amount, 'substandard', line_facts.amount, '0'))
|
|
|
Goto Forum:
Current Time: Tue Apr 23 09:37:43 CDT 2024
|