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 AM

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 --

Similar Messages

  • 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 --

  • 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))
    Regards

    Hi,
    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.

  • How to use decode function in oracle apex 3.2.1

    Hello Friends,
    how to use decode
    var_decode := 'decode'||'('|| var1 ||','|| -800000000000000||','||'.A'||','||
    appreciate your help.
    regards/kumar
    Edited by: kumar73 on Apr 23, 2010 12:25 PM
    Edited by: kumar73 on Apr 23, 2010 12:27 PM

    Hey just on this particular point, there's a handy PL/SQL method for escaping quotation characters which may reduce the complexity of the string.
    For example, if I had a string:
    I can't understand why quoting my 'quote' characters never seems to "work"!and I wanted to pass that into a variable, I could do this:
    vc_foo:= 'I can''t understand why my ''quote'' characters never seems to "work"!';or I could use the q function, whereby you enclose your string within a quote delimiter of your choice, like so:
    vc_foo:= q'^I can't understand why quoting my 'quote' characters never seems to "work"!^';
    note: I used the caret (^) character by convention but any valid character will do
    ...all of which leaves your original string more or less unadulterated.
    I find this particularly useful for generating dynamic SQL and dynamic PL/SQL, as it leave the query looking a tad more readable, although if you are chaining together multiple strings with double-pipes, it can make it a little muddier. I find it particularly useful if I'm going to deal with string data from, for example, a field with Irish-derived surnames such as "O'Reilly"...
    Give it a go!

  • Powershell and oracle and duplicate data in table

    I have created powershell script to  insert data in oracle table   from csv file and I want to know how to stop insert  duplicate row when Powershell script runs multiple time My powershell script is as follow:
    '{0,-60}{1,20}'
    -f
    "Insert TEEN PREGNANCY ICD9 AND ICD10 CODES into the su_edit_detail ",(Get-Date
    -Format yyyyMMdd:hhmmss);
    $myQuery
    =
    SET PAGES 600;
    SET LINES 4000;
    SET ECHO ON;
    SET serveroutput on;
    WHENEVER sqlerror exit sql.sqlcode;
    foreach
    ($file
    in
    dir
    "$($UCMCSVLoadLocation2)"
    -recurse
    -filter
    "*.csv")
    $fileContents
    =
    Import-Csv
    -Path
    $file.fullName
    foreach ($line
    in
    $fileContents)
    $null
    = Execute-NonQuery-Oracle -sql
    insert into SU_EDIT_DETAIL(EDIT_FUNCTION,TABLE_FUNCTION,CODE_FUNCTION,CODE_TYPE,CODE_BEGIN,CODE_END,EXCLUDE,INCLUDE_X,OP_NBR,TRANSCODE,VOID,YMDEFF,YMDEND,YMDTRANS)
    Values
    ('$($line."EDIT_FUNCTION")','$($line."TABLE_FUNCTION")','$($line."CODE_FUNCTION")','$($line."CODE_TYPE")','$($line."CODE_BEGIN")','$($line."CODE_END")',' ',' ', 'MIS', 'C', ' ', 20141001, 99991231,
    20131120)
    Vijay Patel

    please read "PLEASE READ BEFORE POSTING"
    This forum is about the Small Basic programming language.
    Try another forum.
    Jan [ WhTurner ] The Netherlands

  • Function module to give a date based on a particular date and interval.

    Hi there,
    I am writing a code where I need to to find another date
    based on a key date and and an interval.
    Is there any function module where I pass the date and the interval
    (say 30) and it gives me a new date after subtracting 30 from it.
    Or is there any piece of code which does it?
    Thanks in advance.
    Regards,
    Kate

    hi Kate,
    you can try ?
    data : date2 like sy-datum.
    date2 = yourdate - 30.
    hope this helps.

  • Load csv to Oracle gives 'No data Found ' Error

    Hi,
    i am using the steps mentioned in
    http://oraexplorer.com/2007/11/apex-to-upload-text-file-and-write-into
    to load data from CSV to apex.But it is giving me no data found at
    v_data_array := wwv_flow_utilities.string_to_table (v_line);
    Has anyone faced this issue.if so please do help me with this.
    Thanks,
    CKLP

    Hi,
    i am using the steps mentioned in
    http://oraexplorer.com/2007/11/apex-to-upload-text-file-and-write-into
    to load data from CSV to apex.But it is giving me no data found at
    v_data_array := wwv_flow_utilities.string_to_table (v_line);
    Has anyone faced this issue.if so please do help me with this.
    Thanks,
    CKLP

  • MySQL functionality within Oracle SQL Developer Data Modeler

    Hi all,
    I've read a few forum posts here that lead me to believe that MySQL is not currently supported by the data modeler tool (Link #1 I got to by clicking on the Statement Of Direction link on the main page SQL Developer Data Modeler</title><meta name="Title" content="SQL Developer Data Modeler"&g… Link #2 I got to by doing a search on the word MySQL within the forum search):
    Link 1:
    SQL Developer Data Modeler Statement of Direction
    Link 2:
    Can datamodeler be used with MySQL?
    SQL Developer (not the Data Modeler tool, but the database admin-type tool not for modeling) does support MySQL using the latest MySQL Connector J 5.1 driver but, strangely,  it doesn't seem the Data Modeler supports it, though.  Can anyone please confirm that MySQL is currently unsupported for the Data Modeler tool?  Thanks in advance.

    Hi,
    it doesn't seem the Data Modeler supports it, though.
    Data Modeler doesn't have specific support for MySQL - i.e you cannot generate DDL for MySQL. Though you can import form MySQL server using JDBC connection in standalone DM or using created MySQL connection if you are using DM inside SQL Developer.
    You can import definitions for tables(columns, PK and UK constraints, indexes, foreign keys) and views.
    Philip

  • Regarding decode function

    Hi all,
    i want to know abt decode function in oracle-sql
    i..e passing the parameters in decode using decode using ":" bind parameter
    select JOB, decode(:j,'CLERK','MANAGER','ANALYST','EXEC',JOB) FROM EMP;
    i declared the j variable in sql environment
    VARIABLE J VARCHAR2(20);
    i exec the query
    and passed as the clerk as input parameter
    but iam getting the o/p as
    JOB DECODE(:J
    MANAGER MANAGER
    MANAGER MANAGER
    MANAGER MANAGER
    SALESMAN SALESMAN
    SALESMAN SALESMAN
    SALESMAN SALESMAN
    CLERK CLERK
    SALESMAN SALESMAN
    ANALYST ANALYST
    CLERK CLERK
    ANALYST ANALYST
    JOB DECODE(:J
    CLERK CLERK
    12 rows selected.
    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 any mistakes
    tons of thanks in advance to all

    SORRY DAVE,ROD FOR NOT BEING CLEAR,
    IAM WORKING WITH EMP TABLE THE DATA IN IT IS LIKE THIS
    EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
    7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
    7782 CLARK MANAGER 7839 09-JUN-81 2450 10
    7566 JONES MANAGER 7839 02-APR-81 2975 20
    7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
    7499 chaitu SALESMAN 7698 20-FEB-81 1600 300 10
    7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
    7900 JAMES CLERK 7698 03-DEC-81 950 30
    7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
    7902 FORD ANALYST 7566 03-DEC-81 3000 20
    7369 SMITH CLERK 7902 17-DEC-80 800 20
    7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
    EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
    7876 ADAMS CLERK 7788 12-JAN-83 1100 20
    AND I USED DECODE FUNCTION
    select JOB BEFOREPROMOTION, decode(job,'CLERK','MANAGER','ANALYST','EXEC') PROMOTION FROM EMP
    BEFOREPRO PROMOTI
    MANAGER
    MANAGER
    MANAGER
    SALESMAN
    SALESMAN
    SALESMAN
    CLERK MANAGER
    SALESMAN
    ANALYST EXEC
    CLERK MANAGER
    ANALYST EXEC
    I..E ALL THE CLERKS TO MANAGERS AND SAME THING WITH ANALYST
    OK
    NOW I WILL SHOW U ANOTHER QUERY
    select job,decode(job,'CLERK','MANAGER','ANALYST','EXEC',JOB) FROM EMP where job=&job;
    Enter value for job: 'CLERK'
    old 1: select job,decode(job,'CLERK','MANAGER','ANALYST','EXEC',JOB) FROM EMP where job=&job
    new 1: select job,decode(job,'CLERK','MANAGER','ANALYST','EXEC',JOB) FROM EMP where job='CLERK'
    JOB DECODE(JO
    CLERK MANAGER
    CLERK MANAGER
    CLERK MANAGER
    HERE OBSERVE THAT I PASSED JOB ='CLERKS AS PARAMETER AFTER EXECUTING THE QUERY
    IT ASKED LIKE THIS AND SHOWED THE REPLACED VALUES
    Enter value for job: 'CLERK'
    AS SHOWN ABOVE
    FOR THIS TO PASS VALUE
    IN TO THE QUERY BY USING AMPERSAND('&')
    TO TAKE INPUT AS CLERK
    IT CHECKED AND RETURNED THE VALUE
    IN THE SIMILAR WAY CAN I USE ":" COLON
    TO PASS PARAMETERS
    IN TO THE QUERY
    CAN I USE IT
    REGARDS,
    PHANI
    Edited by: user10652894 on Nov 26, 2008 3:24 AM

  • 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

  • Using a C function in Oracle

    Hi,
    I have a field in my table that stores an ID which is encoded using a C function. (third party database)
    I have also been provided the C function that decodes that field to display the actual value. I have compiled this code using Visual Studio .Net 2003 to a dll file, but do not know how to use it in Oracle. Or are there other ways to use this decode function in Oracle?
    I am on 9i database, Windows 2003 server.
    Any help is appreciated.
    Thank you.

    Hi, see:
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:9087161189226

  • 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
    Kalycs

    i 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

  • Convert Date function to oracle

    The follwoing Sql for Sqlserver, what is the equivalent function in oracle. Need help on this.
    YTD:
    YOUR_DATE_FIELD between DATEADD(yy,
    DATEDIFF(yy,0,@Prompt('BEGIN_DATE','D',,mono,free)), 0) AND
    @Prompt('END_DATE','D',,mono,free)
    MTD:
    YOUR_DATE_FIELD between DATEADD(mm, DATEDIFF(mm, 0,
    ,@Prompt('BEGIN_DATE','D',,mono,free)), 0) AND
    @Prompt('END_DATE','D',,mono,free)
    WTD
    YOUR_DATE_FIELD between (,@Prompt('BEGIN_DATE','D',,mono,free) -
    DATEPART(dw, ,@Prompt('BEGIN_DATE','D',,mono,free)))+1 AND
    @Prompt('END_DATE','D',,mono,free)

    Extracting month,year,day from sysdate (You may even opt to use to_char)
    SQL> select extract (month from sysdate) from dual;
    EXTRACT(MONTHFROMSYSDATE)
                            4
    SQL> select extract (year from sysdate) from dual;
    EXTRACT(YEARFROMSYSDATE)
                        2007
    SQL> select extract (day from sysdate) from dual;
    EXTRACT(DAYFROMSYSDATE)
                          5
    Adding 5 days to today's date
    SQL> select sysdate + 5 from dual;
    SYSDATE+5
    10-APR-07
    Adding Months
    SQL> select add_months(sysdate,3) from dual;
    ADD_MONTH
    05-JUL-07
    Date Difference
    SQL> select sysdate - to_date('01/04/2007','dd/mm/yyyy') from dual;
    SYSDATE-TO_DATE('01/04/2007','DD/MM/YYYY')
                                    4.42670139
    Date Difference
    SQL> select trunc(sysdate) - trunc(to_date('01/04/2007','dd/mm/yyyy')) from dual
    TRUNC(SYSDATE)-TRUNC(TO_DATE('01/04/2007','DD/MM/YYYY'))
                                                           4
    SQL>

  • Raw to date function in oracle

    Hi..
    I would like to know the details and usage of Raw to date function in oracle database.Could some one let me know the function details?

    Connected to:
    Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    SQL> set serveroutput on
    SQL> DECLARE
      2   rv RAW(32) := '786B060818023A';
      3   dt DATE := NULL;
      4  BEGIN
      5    dbms_stats.convert_raw_value(rv, dt);
      6    dbms_output.put_line(TO_CHAR(dt, 'DD-MON-YYYY'));
      7  END;
      8  /
    08-JUN-2007
    PL/SQL procedure successfully completed.
    SQL>Regards,
    Jo
    PS: This is not my own example. I don't recollect the link I got this example from. Will post the link if I come across it again. I think its www.psoug.org (not sure)

  • Why this code give me duplicate data

    Hii, all
    Why this code give me duplicate data
    SELECT (G.NAME_1 ||' '||G.NAME_2||' '||G.NAME_3||' '||G.NAME_4) AS NAME,
                  R.RES_NUM
    FROM    GUST G , RESERVATION R,ROOM_DETAILS S,ROOMS RR
    WHERE   G.RES_NUM = R.RES_NUM
    AND     R.RES_NUM = S.RES_NUM
    AND     RR.OCCUPIED = 'Y'
    RES_NUM
    1282
    1282
    1282
    1282
    1280
    1280
    1280
    1280
    1281
    1281
    1281
    1281
    1310
    1310
    1310
    1310
    16 rows selected

    I can solve my problem with another solutions
    like that
    SELECT (G.NAME_1 ||' '||G.NAME_2||' '||G.NAME_3||' '||G.NAME_4) AS NAME,
                  R.RES_NUM
    FROM    GUST G , RESERVATION R,ROOM_DETAILS S
    WHERE   G.RES_NUM = R.RES_NUM
    AND     R.RES_NUM = S.RES_NUM
    AND     TO_CHAR(CHECK_OUT,'DD-MM-YYYY')>=(to_char(sysdate+1,'dd-mm-yyyy','nls_calendar=''arabic hijrah'''))but if this code working he will give me result = 100%
    SELECT (G.NAME_1 ||' '||G.NAME_2||' '||G.NAME_3||' '||G.NAME_4) AS NAME,
                  R.RES_NUM
    FROM    GUST G , RESERVATION R,ROOM_DETAILS S,ROOMS RR
    WHERE   G.RES_NUM = R.RES_NUM
    AND     R.RES_NUM = S.RES_NUM
    AND     RR.OCCUPIED = 'Y'but i can't do that
    Edited by: Ahmed_Arafa on Jan 20, 2012 1:25 PM

Maybe you are looking for