Query with DECODE function

My table contains columns A, B and C with the data as below

A, B AND C
1 1 X
1 X 1
1 1 X

Result must be the right of all 'Z' inserted 'X' & should look like as below

A, B AND C
X Z Z
1 X Z
1 1 X

If it please check the query below & help me to change the query as the result of column b is not correct.

Select a, decode (b, '1', 'Z', 'X') b, decode (c, '1', 'Z', 'X') c
of the test;

Try

With Data As
(
Select 'X' A,'1' B, '1' C From Dual Union All
Select '1','X','1' From Dual Union All
Select '1','1','X' From Dual
)
Select A, (Case When A ='X' And B='1' Then 'Z' Else B End) B,
(Case When (A='X' Or B='X') And C='1' Then 'Z' Else C end)C
from data

Vivek L

Tags: Database

Similar Questions

  • With regard to the decode function

    Hi all

    I want to know abt decode function in sql-oracle
    I... e passing parameters to decode using decode using the ': ' bind the parameter

    Select the WORK, decode(:j,'CLERK','MANAGER','ANALYST','EXEC',JOB) of EMP;
    I declared the variable j in sql environment
    VARCHAR2 (20) OF THE VARIABLE J;
    I have the query exec
    and had the clerk as an input parameter
    but the o/p as iam
    JOB DECODE(:J)
    --------- ---------
    MANAGER
    MANAGER
    MANAGER
    SELLER SELLER
    SELLER SELLER
    SELLER SELLER
    CLERK CLERK
    SELLER SELLER
    THE ANALYST ANALYST
    CLERK CLERK
    THE ANALYST ANALYST

    JOB DECODE(:J)
    --------- ---------
    CLERK CLERK

    12 selected lines.

    SQL > PRINT J;

    J
    --------------------------------------------------------------------------------

    im not getting o/p
    so please hlelp me only: should be used not &
    This is my first thread.
    execuse me if no errors

    tons of thanks in advance to all

    Hello

    We can try like this...

    Begin
    : j: = 'CLERK ';
    end;
    /

    then, we execute your SQL query as below...

    Select the WORK, decode(:j,'CLERK','MANAGER','ANALYST','EXEC',JOB) OF the EMP
    where JOB =: j;

    I think that this will solve your problem

    Kind regards
    Soak the Sevin rana

  • Query regarding oracle functions

    Hi all

    I tried to work with decode them and output of NVL to get the following:

    Output value
    1 NULL
    0 1
    1 1
    2 2
    3 3


    to do this, I wrote the following query:
    SELECT DECODE (NVL(high_value,1), 0.1, NVL(high_value,1)) in tab1.

    Can there be a replacement of this using other functions?

    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
    PL/SQL Release 10.2.0.4.0 - Production
    CORE 10.2.0.4.0 Production
    AMT for Solaris: release 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - Production

    Thank you
    Nikhil

    Can be with decoding

    with t_data as
    (
    select null as val from dual union all
    select 0 from dual union all
    select 1 from dual union all
    select 2 from dual union all
    select 3 from dual
    )
    SELECT
         val,decode(val,null,1,0,1, val) output
    from t_data
    
  • Decode function in the Update statement

    Hi all

    I'm writing a query where I can update a pastdue_fees column in a table of book_trans based on a difference between the return_dte and due_dte columns.

    I use Oracle SQL. That's what I have so far for my decoding function:

    SQL > SELECT
    2 DECODE (SIGN ((return_dte-due_dte) * 2),)
    3 '-1 ', ' 0',
    4 '1', '12', 'Null')
    5 FROM book_trans;

    DECO
    ----
    Null value
    12
    Null value
    0

    If the logic is that, if the sign is - 1, the value in the return_dte column must be 0; If it is + 1, then it has 12 and everything else is Null.

    So now, I have to conclude my function of decoding of the update statement to update the columns. However, I get the error messages.

    The logic should be:
    UPDATE book_trans SET PastDue_fees = decode (expression)

    I gave him a few different tests 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 on line 2:
    ORA-00936: lack of 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.
    OF book_trans
    *
    ERROR on line 6:
    ORA-00933: SQL not correctly completed command.

    Any help or advice would be greatly appreciated I got SQL for about six weeks and not very competent!

    Thank you!

    882300 wrote:
    Hi all

    I'm writing a query where I can update a pastdue_fees column in a table of book_trans based on a difference between the return_dte and due_dte columns.

    I use Oracle SQL. That's what I have so far for my decoding function:

    SQL > SELECT
    2 DECODE (SIGN ((return_dte-due_dte) * 2),)
    3 '-1 ', ' 0',
    4 '1', '12', 'Null')
    5 FROM book_trans;

    DECO
    ----
    Null value
    12
    Null value
    0

    If the logic is that, if the sign is - 1, the value in the return_dte column must be 0; If it is + 1, then it has 12 and everything else is Null.

    So now, I have to conclude my function of decoding of the update statement to update the columns. However, I get the error messages.

    The logic should be:
    UPDATE book_trans SET PastDue_fees = decode (expression)

    I gave him a few different tests 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 on line 2:
    ORA-00936: lack of 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.
    OF book_trans
    *
    ERROR on line 6:
    ORA-00933: SQL not correctly completed command.

    Any help or advice would be greatly appreciated I got SQL for about six weeks and not very competent!

    Thank you!

    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 store all numbers in there so it must be reported as a NUMBER column and not a column of type character (varchar2).

    ALWAYS use the appropriate data type, it will save you a ton of headaches in the future.

    Also, since you are new to the forum, please read the FAQ to learn etiquette and what not.

    http://wikis.Sun.com/display/Forums/Forums+FAQ

  • What is the best way to optimize a SQL query: call a function or doing a join?

    Hi, I want to know what is the best way to optimize a SQL query, call a function inside the SELECT statement, or make a simple join?

    It depends on.  Could be a.  Could be the other.  Could be no difference.  You would need to compare with your tables in your environment with your settings.

    If you put a gun to my head, I was given no other information and required that I answered the question, I would tend to wait that the join would be more effective.  In general, if you can do something in pure SQL, it will be more effective than if you call PL/SQL.

    Justin

  • Please suggest a select query / under query with using any routines or

    source table: three columns ORIGIN, DESTINATION, MILES

    Origin Destination Miles

    Sydney Melbourne 1000
    Perth, Adelaide 3000
    Canberra Melbounre 700
    Melbourne Sydney 1000
    Brisbane Sydney 1000
    Perth, Darwin 4000
    Sydney, Brisbane 1000

    out put: three columns ORIGIN, DESTINATION, MILES

    Duplicate routes should be ignored, so the output is

    Origin Destination Miles

    Sydney Melbourne 1000
    Perth, Adelaide 3000
    Canberra Melbounre 700
    Brisbane Sydney 1000
    Perth, Darwin 4000

    Please suggest a select query / under query with using subroutines or functions/pkgs to get put out table.

    Hello

    user9368047 wrote:
    ... Please suggest a select query / under query with using subroutines or functions/pkgs to get put out table.

    Why? If the most effective way to achieve the desired results is to use a function, why would you not use it?

    Here's a way, without all the features:

    SELECT     a.*
    FROM           source_table  a
    LEFT OUTER JOIN      source_table  b  ON   a.origin          = b.destination
                                          AND  a.destination       = b.origin
                          AND  a.miles          = b.miles
    WHERE   b.origin  > a.origin    -- Not b.origin > b.origin
    OR     b.origin  IS NULL
    ;
    

    If you would care to post CREATE TABLE and INSERT statements for your sample data, and then I could test this.

    Published by: Frank Kulash, November 6, 2012 19:39
    Fixed WHERE after MLVrown clause (see below)

  • Get data query with java files

    Hay,
    I'm trying to get a list of data file by a query with CRMI or wcm to java.
    My goal is to get list < DataFile > for the content by query server.

    I can't find a function or a method to do this, I just success to get the metadata of the cdf fields.

    I want to create objects that are customized from the cdf...

    Please, can someone help me?
    Eran

    What service 11g SS_GET_ELEMENT_DATA? It doesn't seem to be documented but must dDocName as a parameter which is the CDF.

    Jonathan
    http://redstonecontentsolutions.com
    http://corecontentonly.com

  • Update with Decode and Dates of business statement

    Hi all

    I need to write a statement of up to DATE using the date of business rule. In general, the due date is 30 days from the date of payment. If the due date falls on a Saturday or Sunday then the term of the loan is 32 days.
    I know that to test a day of week, I would need to use the function to_char in Oracle with the format of a '. I did some research and found that to test what the week falls on November 12, 2007, I would need to use the expression to_char('12-NOV-2007','D'). This function returns a number between 1 and 7. 1 represents Sunday, 2 Monday,..., 7 Saturday.

    What I really would need to do, is to write a single UPDATE statement using an expression with the Decode function and to_Char function:
    UPDATE book_trans SET due_dte = expression

    These are operations that will need to be updated:
    ISBN 0-07-225790-3 retrieved on November 15, 2007 per employee 101 (book_trans_id = 1)
    ISBN 0-07-225790-3 retrieved on November 12, 2007 by employee 151 (book_trans_id = 2)
    ISBN 0-201-69471-9 retrieved on 14 November 2007 by employee 175 (book_trans_id = 3)
    ISBN 0-12-369379-9 checked on November 16, 2007 by employee 201(book_trans_id=4)

    Manually, I calculated the maturity dte and writes the update to each book_trans_id statement:

    UPDATE book_trans SET due_dte is 17-dec-07' WHERE book_trans_id = 1;.
    UPDATE book_trans SET due_dte is 12-dec-07' WHERE book_trans_id = 2;.
    UPDATE book_trans SET due_dte is 14-dec-07' WHERE book_trans_id = 3;.
    UPDATE book_trans SET due_dte is 18-dec-07' WHERE book_trans_id = 4;.

    As you can see, it is very heavy and I just want to know how to integrate everything into a single Update statement:
    UPDATE book_trans SET due_dte = expression
    So if a due date falls on Saturday or Sunday, the loan period 32 days; day of week, length of the loan is 30 days.

    Advice or assistance will be greatly appreciated. Thank you!
    due_date := checkout_date + 30 + decode(to_char(checkout_date + 30,'DY'),'SAT',2,'SUN',1,0)
    

    Concerning

    Etbin

  • using utl_encode.base64_encode to the breast to decode function

    Hi, I want to use utl_encode.base64_encode to decode function. However, the result is not as expected.

    Select 'a', decode ('a', ' 1 ',' EQUAL to 1',)
    "Is NOT EQUAL to 1'.
    utl_encode.base64_encode ('a')
    ) result
    of the double


    A RESULT
    a 43673D3D

    43673D3D is not a value encoded as a base64

    What is the problem with sql?

    Thank you
    SQL> select 'a'
      2  ,      utl_encode.base64_encode(utl_raw.cast_to_raw('a')) raw_result
      3  ,      utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw('a'))) string_result
      4  from   dual;
    
    ' RAW_RESULT      STRING_RESULT
    - --------------- ---------------
    a 59513D3D        YQ==
    
    1 row selected.
    

    Keep in mind that utl_encode.base64_encode returns a RAW and needs FIRST entries.
    You will probably need UTL_RAW to get the desired results.
    http://download.Oracle.com/docs/CD/B19306_01/AppDev.102/b14258/u_encode.htm#CACECFHF

  • Query with a subquery should return a value but does not work

    When I run this SQL, it does not return value:

    SELECT vfn.cat
    OF vfn, valid_fishery vf vps_fishery_ner
    WHERE vfn.plan = vf.plan
    AND vfn.cat = vf.cat
    AND vf.permit_year = 2010
    AND vf.moratorium_fishery 't ='
    AND vfn.vp_num = 211652
    AND vfn.ap_year = 2010
    AND vfn.plan = 'MUL '.
    AND vfn.date_issued = (SELECT MAX (date_issued)
    OF vps_fishery_ner
    WHERE vp_num = 211652
    AND ap_year = 2010);

    To test, I remove the subquery and run it separately:
    SELECT MAX (date_issued)
    OF vps_fishery_ner
    WHERE vp_num = 211652
    AND ap_year = 2010;

    Returns 2 April 10

    Then I paste the date into the original query (using her TRUNCATES the function, of course, since I'm only part DDMMYY hardcode the date):

    SELECT vfn.cat
    OF vfn, valid_fishery vf vps_fishery_ner
    WHERE vfn.plan = vf.plan
    AND vfn.cat = vf.cat
    AND vf.permit_year = 2010
    AND vf.moratorium_fishery 't ='
    AND vfn.vp_num = 211652
    AND vfn.ap_year = 2010
    AND vfn.plan = 'MUL '.
    AND TRUNC (date_issued) = TO_DATE('02-APR-10');

    And returns the required value, "A".

    So why the complete query with a subquery does not work, if the value returned by the subquery is valid and works when you just pasted in there?
    Thank you.

    Hello

    Maybe you should include this in the subquery as well?

    AND vfn.plan = 'MUL'
    
  • in combination with the function sequence

    Hello

    I have a problem with a sequence in combination with a function.

    I want to generate a kind of a hardware inventory report that contains some kind of a hardware ID (a number not repetitive, auto-increment, allowed deviations). However, I have to keep track of what material ID I used to which material of the inventory, for reasons of traceability. Another important requirement is that whenever the report is run, that the returned hardware IDs are never repeated. If repeatedly runs for the same hardware in the inventory must always return new material ID.

    So I was about to create a simple function from PL/SQL where I spend my data of traceability as parameters. Then the function uses internally a sequence (nextval) to generate a new hardware ID, then stores the new ID of material by related values in the input in a table (autonomous transaction) parameters and returns the new ID.

    It works pretty well so far.

    Now the tricky part: I want where I want to go back the same ID in different columns of a SQL statement.

    Let me give you an example:
    CREATE SEQUENCE XXX_TEST_SEQ
    MINVALUE 1 MAXVALUE 999999999999999999999999999
    INCREMENT BY 1 START WITH 1
    NOCACHE NOORDER NOCYCLE;
    
    
    CREATE PACKAGE XXX_TEST_PKG AUTHID CURRENT_USER AS
    FUNCTION get_id(p_someparameter NUMBER) RETURN NUMBER;
    END XXX_TEST_PKG;
    
    
    CREATE PACKAGE BODY XXX_TEST_PKG AS
    FUNCTION get_id(p_someparameter NUMBER) RETURN NUMBER IS
      l_new_id NUMBER;
    BEGIN
      SELECT XXX_TEST_SEQ.NEXTVAL
        INTO l_new_id
        FROM DUAL;
        /* do some more logic, e.g. store the freshly generated ID 
            together with the input parameter values in some tracing table */
      RETURN l_new_id;
    END get_id;
    END XXX_TEST_PKG;
    It is a simplified version of the function for demo.

    We will use:
    -- 1) this works as expected:
    select xxx_test_pkg.get_id(1) from dual;
    
    XXX_TEST_PKG.GET_ID(1) 
    ------------------------ 
    1                        
    
    
    -- 2) this returns two different numbers:
    select xxx_test_pkg.get_id(1) as id_one
        ,  xxx_test_pkg.get_id(1) as id_two
    from dual;
    
    ID_ONE                 ID_TWO                 
    ---------------------- ---------------------- 
    2                      3                      
    
    
    -- 3) This is the problematic case: I want to return the same ID two times:
    select my_id as id
         , my_id as same_id
     from (select xxx_test_pkg.get_id(1) as my_id from dual);
    
    ID                     SAME_ID                
    ---------------------- ---------------------- 
    4                      5                      
    
    
    -- 4) CURVAL on these sequence does not work:
    select my_id as id
         , xxx_test_seq.curval as same_id
     from (select xxx_test_pkg.get_id(1) as my_id from dual);
    
    SQL Error: ORA-00904: "XXX_TEST_SEQ"."CURVAL": invalid identifier
    
    
    -- 5) This gives the output that I need, but why?
    select my_id as id
         , xxx_test_seq.nextval as same_id
     from (select xxx_test_pkg.get_id(1) as my_id from dual);
     
    ID                     SAME_ID                
    ---------------------- ---------------------- 
    7                      7                      
    Query 3 is one who gives me headaches. I need to return the same ID in different columns. But it returns only two different pieces of identification. I know that the optimizer will merge the two select statements into one and therefore perform the function twice. Someone knows how can I avoid this?

    Which is a little strange to me, it's the query 5 is really what I need, but at first glance I would say that he should not do. I would say that using nextval would be me again another number sequence.

    It's confusing... Can someone explain this and advise how can I build a query if I can get the same ID twice by line?


    I know that I can totally get rid of the sequence in my function and use something different, but I was wondering if I can avoid to change my function.


    Thank you
    David.

    Oracle's FUSION query inline with the outer query block, which causes the function being called twice.

    Instead of MATERIALIZING suspicion, or relying on ROWNUM = 1 effect predicate aside, I would explicitly tell the optimizer to not do so via the NO_MERGE indicator.

    select my_id as id
         , my_id as same_id
    from (select /*+ NO_MERGE */ xxx_test_pkg.get_id(1) as my_id from dual);
    
  • Can I use data value references with a functional overall?

    Can I use data value references with a functional global?  I am trying, but it causes problems:  I run the attached VI with "new DVR" selected.  Then I run it again with "return array" and I get the error 1556: "The reference is invalid. This error might occur because the reference has been deleted."  It seems to have forgotten the DVR, but I thought it should be stored in the shift register.
    
    Another odd thing:  In my VI, if I try to connect the wire shown in "something strange", then the type changes (from U16 array to DBL), i.e. it doesn't recognize the type referenced by the DVR.
    
    I want to pass a very large 4-dimensional array between parallel loops with a functional global.  To avoid memory problems, I want to use a DVR and in-place element structures.  If that's not allowed in Labview, I will try passing it through a user event or notifier.  Any problems with those alternatives?
    
    Thanks.
    

    I received a response from an engineer applications OR on this subject, and it seems that it is perhaps a problem with Labview.  They are working on it, so I'll go ahead and close this discussion.

    Thanks for the help,

    Allan

  • Error with the function "Open/create/overwrite file" with cancel button

    I have attached a very simple VI who embodies what I want to do with my function to open the file. I just want to stop the rest of my program (theoretically embedded in the Structure box) to run if the user deigns to do not to specify a file location.

    However, if you click on the button cancel an error is produced before the rest of the program is running. If ignore you the error and continue, the "canceled" properly true made variable and the Structure of business runs correctly. "" I just want to remove the error message given to me by LabVIEW.

    Any ideas on why or how?

    Read the help of LabVIEW on automatic error handling.

    In your case, you can just wire the output of cluster of error to the structure of your business.

  • Fitting with integral functions

    Hello

    I try to fit the data with full functions.  It is impossible to obtain analytical solutions of the integrations.

    Integration is integral [(T - abs [x]) f (x) dx, T, - T]

    Here, f (x) is one of the functions of autocorrelation for different model systems.

    Thank you

    Doogie

    Hi Doogie,

    We certainly have the integration and curve build VIs in LabVIEW. For example, you can find several kinds of system integrators in the mathematics > integration & differentiation palette. Also, if you use our design Simulation tools by & control, that built the integration as well. However, because you are dealing with an equation as opposed to sampled signals, I'd start by programming your equation in LabVIEW. Take a look at this forum thread that discusses this, and examples of code.

    Once you have programmed the equation and can generate an array of points for your stove, you might try to enter a function of integration as the "Integrate x (t) .vi" and then use a function as the "general polynomial Fit.vi" curve to generate an equation fitting for him. I would try getting started in this way. Please let me know if I address not the functionality that you are interested in.

    Best regards

  • Kalonline-works until I get the anti-piracy icon so stop saying: there is a problem with the function of anti-hacking

    I play kalonline (RPG game), when to start the game it works up until I get the anti-piracy icon so stop saying: there is a problem with the function of anti-hacking. the program stops. I am the admin of it say kal computer run it in administrator mode and I did and I delete and reinstall the game. Solve the problem in win 7 by a right click and run as administrator, they wanted to work in win xp it all fits on how to solve this problem. These are the things, I was told to do to fix it.
    Hello

    It comes to KalOnline.

    Your error message can be corrected by following these steps:

    1. run KalOnline in Administrator Mode.

    2 uninstall KalOnline d_lete the entire Inixsoft folder, then reinstall the game.
    I did it and and it has not worked. then I was told that this was causing the problem

    Please check your computer system.

    This problem has occurred by a kind of problem of piracy.

    And if you have used a kind of memory-editing program, it could be happen too.

    So if your receipt similar to, then please remove it.
    I have none of these things on my computer. All I know is that people who have not updated windows xp can play the game and people who win 7 can play the game if they right click icon and run as administrator, that I still say side here but no way to prove it.

    Hello

    I suggest you to get in touch with the manufacturer of games and see if there are any other fix for this problem.

Maybe you are looking for