Help with Decode function

Hello!!
Can anyone please help me with the decode statement
Table 1 Table2 Table3
Id Id Code
Volume Code
For each of the codes in Table3 I need to find the corresponding Volume. Can I use decode in the select statement to this,
Eg Code Volume
ABC 20
XYZ 10 etc etc.
Thankyou all in advance.

Your table structure is a little unclear from your post. I am assuming that what you posted was:
Table 1
Id
Table2
Id
Code
Table3
Volume
Code You can use decode if there are only a few values for table3. For example,
SELECT table2.code,
DECODE(table2.code,'ABC',10,'XYZ',20,'UNKNOWN') volume
FROM table2
or even
SELECT code,DECODE(code,null,'NONE',
                  (SELECT volume FROM table3 where code=table2.code)) volume
FROM table2However, the first will break, or at least be incorrect, if anyone adds a new code/volume pair, and is tedious if you have more than a few values. The second will likely be pretty slow if there are a large number of records in table3. The best solution would be to use a simple join rather than decode. Something like:
SELECT table1.id, table2.code, table3.volume
FROM table1, table2, table3
WHERE table1.id = table2.id and
      table2.code = table3.code

Similar Messages

  • Need help with DECODE function

    Hello,
    I am trying to use default within the decode function and every time I get a missing expression. I have searched everywhere and cant figure out what I'm doing wrong. Thanks
    select decode (request_id,0,'No files found', DEFAULT)

    Hi,
    Welcome to the forum!
    When you use a default value, the last argument to DECODE is the actual value you want as a default.
    For example:
    SELECT       ename
    ,       deptno
    ,       DECODE ( deptno
                 , 30     , 'Sales'
                      , 'All others'     -- Default value
                  )                 AS dname
    FROM      scott.emp
    ORDER BY  ename
    ;Output:
    ENAME          DEPTNO DNAME
    ADAMS              20 All others
    ALLEN              30 Sales
    BLAKE              30 Sales
    CLARK              10 All others
    FORD               20 All others
    JAMES              30 Sales
    JONES              20 All others
    KING               10 All others
    MARTIN             30 Sales
    MILLER             10 All others
    SCOTT              20 All others
    SMITH              20 All others
    TURNER             30 Sales
    WARD               30 Sales 
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    If you can show the problem using commonly available tables (such as those in the scott schema) then you don't need to post any sample data; just the results and the explanation.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ {message:id=9360002}

  • Error while replacing IF statements with DECODE function in procedure

    Hi All,
    I have created a procedure which has nested IF statements. Now I want to replace the IF statements with DECODE functions to improve performance.
    Procedure:
    IF (var_int_sev = '0')
    THEN
    var_sev := '2';
    ELSE
    SELECT sev
    INTO var_int_sev
    FROM errorconfig
    WHERE errorcode = var_errorcode;
    var_sev := var_int_sev;
    END IF;
    I converted the above IF statement into DECODE function as mentioned below:
    var_Sev := DECODE(var_int_sev,0,2,SELECT severity FROM errorconfig WHERE errorcode=var_ErrorCode)
    But it throws below error at the select statement used inside DECODE.
    Error(58,51): PLS-00103: Encountered the symbol "SELECT" when expecting one of the following: ( - + case mod new not null others <an identifier> <a double-quoted delimited-identifier> <a bind variable> avg count current exists max min prior sql stddev sum variance execute forall merge time timestamp interval date <a string literal with character set specification> <a number> <a single-quoted SQL string> pipe <an alternatively-quoted string literal with character set specification> <an alternativ
    Can someone help me in converting the IF to DECODE in the above case. Also how can we use a select statement inside decode.

    instead of trying to rewrite all your code and hoping that the performance will be better, it's a better option to investigate and find out which part of your application is slow
    read this:
    When your query takes too long ...

  • How to convert rows into columns with decode function

    Hi,
    How to convert rows into columns with the help of decode function in oracle.
    thanks and regards
    P Prakash

    say
    col1 col2
    1 10
    2 20
    3 30
    then use
    select col1,
    sum(decode(col2,10,10)) "new1"
    sum(decode(col2,20,20))"new2"
    sum(decode(col2,30,30))"new3"
    from table_name
    group by col1;
    we used sum u can use ny function if wont u have to give the column name i.e col2 name also
    so i think u got it nw
    regards

  • OWB3i - Validate with DECODE function

    Hi,
    in OWB 3i when I validate an expression defined inside an expression operator with DECODE() function (which is not include in default transformation) come this error:
    Line 1, Col 1:
    PLS-00204: function or pseudo-column 'DECODE' may be used inside a SQL statement only
    but it seems only a warning because when i generate the scripts and run no other error happen and all works fine.
    Regards

    Hi,
    Welcome to the forum!
    When you use a default value, the last argument to DECODE is the actual value you want as a default.
    For example:
    SELECT       ename
    ,       deptno
    ,       DECODE ( deptno
                 , 30     , 'Sales'
                      , 'All others'     -- Default value
                  )                 AS dname
    FROM      scott.emp
    ORDER BY  ename
    ;Output:
    ENAME          DEPTNO DNAME
    ADAMS              20 All others
    ALLEN              30 Sales
    BLAKE              30 Sales
    CLARK              10 All others
    FORD               20 All others
    JAMES              30 Sales
    JONES              20 All others
    KING               10 All others
    MARTIN             30 Sales
    MILLER             10 All others
    SCOTT              20 All others
    SMITH              20 All others
    TURNER             30 Sales
    WARD               30 Sales 
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    If you can show the problem using commonly available tables (such as those in the scott schema) then you don't need to post any sample data; just the results and the explanation.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ {message:id=9360002}

  • Help with stored function

    Hi...I was wondering if I could get help with this function. How do i write a function to return hours between a begin date and an end date for an employee. Thanks so much

    EdStevens wrote:
    AlexeyDev wrote:
    sb92075 wrote:
    select (date2-date1)*24 from dual;not as above but as below
    select (date2-date1)/24 from dual;date2-date1 is amount of days. Divide it by 24 and what? if you multiply it on 24 you will have a chance to know how many hours between these two dates. :-)Don't forget that a DATE type also includes a time component.I suppose it doesn't matter if you did a difference between two dates. The result is always number of days.

  • Help on Decode function in Toplink

    Hi,
    I have a sql statement like this:
    select field1, field2 from table1 where upper(field1) like 'ADELPHI%'
    order by field1,
    DECODE(TO_CHAR(NVL(LENGTH(TRANSLATE(field1,'A1234567890','A')),0)),'0',LPAD(field1 ,50),field1)
    How would I converted this to toplink query?
    I appreciate any help you guys can extend to me.

    I just spent about a half hour writing a response here, and the damned forum timed out on me. #$^!@$#!^!@$%.
    Here is the summary:
    - Using TL Expressions and EJBQL are to allow Java developers who don't necessarily know SQL to write queries and to facilitate portability/maintainability of the apps. For REALLY gnarly queries, using TL Expressions or EJBQL won't buy you this. I.e., SQL is more expressive and easier for this kind of quer, so it's not easier to use, and frankly with the code needed to support it, it won't be easier to maintain or port... Bottom line, maybe SQL is the best option here?
    - If you want to experiment, search for TO_CHAR on this forum, and the decode function in the javadoc. You need to build the expressions in pieces and you can treat expressions as arguments to other expressions -- in other words, you can build an expression like builder.leftPad(50, builder.get("field1")) and use it as one of the key/values in the hashtable to the decode function (in theory).
    - Don

  • Help for decode function

    Hi all,
    I want to use decode function in RTF template.
    I know i can use if statement to deal with it ,but if the conditions are over 3, if statement is not good choice.
    Here is the if condition statement
    <?if:answer='Y'?>Yes<?end if?> <?if:answer='N'?>No<?end if?>
    how can i translate this if statement to use decode function
    I tried to use the statement as below, but it doesn't work.
    <?xdofx:decode(:answer,Y,'YES',N,'NO')?>
    using this statement i got empty in this field.
    I appreciate any responds, thanks in advance.
    appcat

    Hi,
    it should work,coz there is no xsl equivalent for this function that i have seen on blogs,
    the syntax that i have got it from other xmlp blogs, sounds like you put correct syntax, try to give multiple conditions to check the result
    <?xdofx:decode(’xxx’,’bbb’,’ccc’,’xxx’,
    ’ddd’)?>
    srinuk

  • Prob with decode function

    hi iam having prob with the following decode function
    declare
    c varchar2(20);
    begin
    select decode(deptno,
    10, 'accounting',
    20,'Research',
    30 ,'sales',
    40,'operations','UNKNOWN') into c from dept where loc='DALLAS';
    dbms_output.put_line('DEPARTMENT in DALLAS'||c);
    select decode(deptno,
    10, 'accounting',
    20,'Research',
    30 ,'sales',
    40,'operations','UNKNOWN') into c from dept where loc='INDIA';
    dbms_output.put_line('DEPARTMENT IN INDIA'||c);
    end;
    iam getting no_data_found exception which is reasonable..but what happened to 'unknown' clause in decode function.
    thank u
    rajiv

    Please see the responses to your prob in decode function
    John

  • Problem with decode function.

    Hi,
    Can anyone of you help me out in solving this?
    It is like i wish to give different select statements according to the value of a parameter entered by user USING DECODE FUNCTION.The select statement contains some other select statements inside it.So when i execute it,it is giving error like 'ORA-00913-too many values(even when i enclose select statements within brackets).

    ORA-00913 too many values
    Cause: The SQL statement requires two sets of values equal in number. This error occurs when the second set contains more items than the first set. For example, the subquery in a WHERE or HAVING clause may return too many columns, or a VALUES or SELECT clause may return more columns than are listed in the INSERT.
    Action: Check the number of items in each set and change the SQL statement to make them equal.
    the above is from oracle documentation. the brackets is not the problem, u must be using multiple items in the integrated decode queries. If the problem still exists post ur DML for further analysis.
    zaibi.

  • Between clause with DECODE function.

    Hi All,
    I have one table which has object, date and quantity columns, Now based on date range I can find out the objects and quantity but the question is: I want a query which returns all the objects and should show quantity 0(zero or null) for those objects which don't falls under that date range. So I thought to use a decode function but it only uses the relational operation, I thought something like this but it's wrong,
    select object,decode(created_date,BETWEEN '01/01/2008' TO '08/01/2008',quantity,0) from table;
    Does anyone have any idea how can I make this work in single sql statement?
    Thanks for your help.

    select object,decode(created_date,BETWEEN '01/01/2008' TO '08/01/2008',quantity,0) from table;
    Does anyone have any idea how can I make this work in single sql statement?If you're still interested in using DECODE over Case when, please try this.
    Check for employees hired between 6/17/1987 and 9/30/1987
    SQL> select first_name||' '||last_name as emp_name, hire_date,
      2         decode(sign((to_date('06/17/1987','MM/DD/YYYY')-1)-hire_date)
      3           + sign((to_date('09/30/1987','MM/DD/YYYY')+1)-hire_date)
      4                   ,0,'Between','Not between') as IsBetween
      5    from employees
      6   where hire_date <to_date('01/01/1991','MM/DD/YYYY')
      7  /
    EMP_NAME                                       HIRE_DATE ISBETWEEN
    Steven King                                    17-JUN-87 Between
    Neena Kochhar                                  21-SEP-89 Not between
    Alexander Hunold                               03-JAN-90 Not between
    Jennifer Whalen                                17-SEP-87 BetweenMessage was edited by:
    Bobbydj

  • Help with ASO function

    Hi all,
    I need some help with ASO mdx function.
    Avg({Leaves([Employees].Currentmember)}, [Calculated_Field]). This will give me the average for Calculated_Field for all levels of Employees. But i want to add more dimensions like Region and year.
    Please advice how can I achieve this.
    Thanks
    Andy

    you have to use cross join in order to add more dimension members to the formula.This will give you some idea
    Re: Writing formula in Outline??????
    Regards,
    RSG

  • Need help with DECODE

    Hi,
    I need to write a DECODE function to which if I pass 'mystring' and if it has the value 'M' it should output 'M', if it has a value of 'F' it should output 'F' and if mystring is not same as string_coded then it should output string_coded.
    I know the below statement is incorrect.
    decode (mystring, 'M', 'M', 'F', 'F', (mystring <> string_coded), string_coded)
    Is there a way to correct this DECODE? Please suggest.
    Thanks.

    It is not clear of you want it to match the string 'string_coded' or if it is some column name, but with the example below you should be able to figure out what to adjust to make it work in your case.
    SQL> create table mytable
      2  as
      3  select 'M' mystring, 'X' string_coded from dual union all
      4  select 'F', 'X' from dual union all
      5  select 'X', 'X' from dual union all
      6  select 'Z', 'X' from dual
      7  /
    Tabel is aangemaakt.
    SQL> select mystring
      2       , string_coded
      3       , decode(mystring,'M','M','F','F',string_coded,'mystring = string_coded','mystring != string_coded') your_expression
      4    from mytable
      5  /
    M S YOUR_EXPRESSION
    M X M
    F X F
    X X mystring = string_coded
    Z X mystring != string_coded
    4 rijen zijn geselecteerd.Regards,
    Rob.

  • I need help with Analytic Function

    Hi,
    I have this little problem that I need help with.
    My datafile has thousands of records that look like...
    Client_Id Region Countries
    [1] [1] [USA, Canada]
    [1] [2] [Australia, France, Germany]
    [1] [3] [China, India, Korea]
    [1] [4] [Brazil, Mexico]
    [8] [1] [USA, Canada]
    [9] [1] [USA, Canada]
    [9] [4] [Argentina, Brazil]
    [13] [1] [USA, Canada]
    [15] [1] [USA]
    [15] [4] [Argentina, Brazil]
    etc
    My task is is to create a report with 2 columns - Client_Id and Countries, to look something like...
    Client_Id Countries
    [1] [USA, Canada, Australia, France, Germany, China, India, Korea, Brazil, Mexico]
    [8] [USA, Canada]
    [9] [USA, Canada, Argentina, Brazil]
    [13] [USA, Canada]
    [15] [USA, Argentina, Brazil]
    etc.
    How can I achieve this using Analytic Function(s)?
    Thanks.
    BDF

    Hi,
    That's called String Aggregation , and the following site shows many ways to do it:
    http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php
    Which one should you use? That depends on which version of Oracle you're using, and your exact requirements.
    For example, is order importatn? You said the results shoudl include:
    CLIENT_ID  COUNTRIES
    1        USA, Canada, Australia, France, Germany, China, India, Korea, Brazil, Mexicobut would you be equally happy with
    CLIENT_ID  COUNTRIES
    1        Australia, France, Germany, China, India, Korea, Brazil, Mexico, USA, Canadaor
    CLIENT_ID  COUNTRIES
    1        Australia, France, Germany, USA, Canada, Brazil, Mexico, China, India, Korea?
    Mwalimu wrote:
    ... How can I achieve this using Analytic Function(s)?The best solution may not involve analytic functions at all. Is that okay?
    If you'd like help, post your best attempt, a little sample data (CREATE TABLE and INSERT statements), the results you want from that data, and an explanation of how you get those results from that data.
    Always say which version of Oracle you're using.
    Edited by: Frank Kulash on Aug 29, 2011 3:05 PM

  • Help with Sort function in Terminal

    Hello all... this is my first post on here as I'm having some trouble with some Termianl commands. I'm trying to learn Terminal at the moment as it is but I would appreciate some help with this one....
    I'm trying to sort a rather large txt file into alphabetical order and also delete any duplicates. I've been using the following command in Terminal:
    sort -u words.txt > words1.txt
    but after a while I get the following error
    sort: string comparison failed: Illegal byte sequence
    sort: Set LC_ALL='C' to work around the problem.
    sort: The strings compared were `ariadnetr\345dens\r' and `ariadnetr\345ds\r'.
    What should my initial command be? What is Set LC_ALL='C'?
    Hope you guys can help?

    Various languages distinct sorting - collation - sequences. 
    The characters can and variously do sort differently, depending on what language is involved. 
    Languages here can include the written languages of humans, and a few settings associated with programming languages.  This is all part of what is known as internationalization and localization, and there are are various documents around on that topic.
    The LC_ALL environment variable sets all of the locale-related settings en-mass, including the collation sequence that is established via LC_COLLATE et al, and the sort tool is suggesting selecting the C language collation.
    Here, the tool is suggesting the following syntax:
    LC_ALL=C sort -u words.txt > words1.txt
    This can also be done by exporting the LC_ALL, but it's probably better to just do this locally before invoking the tool.
    Also look at the lines of text in question within the files, and confirm the character encoding of the file.
    Files can have different character encodings, and there's no reliable means to guess the encoding.  For some related information, see the file command:
    file words.txt
    ...and start reading some of the materials on internationalization and localization that are posted around the 'net. Here's Apple's top-level overview.
    In this case, it looks like there's an "odd" character and probably an å character on that line and apparently the Svenska ariadnetrådens. 
    Switching collation can help here, or - if the character is not necessary - removing it via tr or replacing it via sed can be equally effective solutions. 
    Given it appears to be Svenska, it might work better to switch to Svenska collation thanto  the suggested C collation.
    I think that's going to be sv_SE, which would make the command:
    LC_ALL=sv_SE sort -u words.txt > words1.txt
    This is all generic bash shell scripting stuff, and not specific to OS X.  If you haven't already seen them, the folks over at tldp have various guides including a bash guide for beginners, and an advanced bash scripting guide - both can be worth skimming.  They're not exactly the same as bash on OS X and some specific commands and switches can differ, and as bash versions can differ, but bash is quite similar across all the platforms.

Maybe you are looking for

  • My MCB Pro keeps booting when I switch it on after a shutdown???

    Hello Friends, My MCB pro (late 2011) was running perfectly on 10.8.2 for 2 months but yesterday i shut it down and when I shitched it on it kept on booting. After 10-15 minutes i had to switch it off by pressing and holding power switch. this happen

  • Disappearing color profile when saved for the Web

    Hello. I have a problem with the "Save for the Web" results using Photoshop CS6 running on Win 7 x64. My photos are already in 8 bits and in sRGB and when I save for the Web I check the option "embed color profile", uncheck "convert to sRGB", metadat

  • Where can i buy original iPhone 5 replacement parts?

    Where can i buy original iPhone 5 replacement parts, like: iphone 5 back housing (black&slate)

  • Url encoding and getUrl

    I've created a flash search widget that needs to use the google clickTag. It works fine if I search with one term. But if I search for two terms, the resulting url is ...com/search/term1%20term2. For Google tracking, this needs to be the '+' characte

  • Database not open error - ORA-01219

    Hi experts, I am new to oracle. I have created a database call 'Play' and also created a table 'tbPlay'. I also inserted some rows here. After that all the database operations were working fine here. After restarting the machine, I logged into the da