Help in selecting required columns from duplicates records

HI,
I have a table like below
empid,ename,sal,Jdate,Rdate,deptid,designation,Terminated_Emp
1,a,100,15-May-2014,11,SE,NO
1,null,null,15-OCT-14,null,null,YES
I want output like below
1,a,100,15-May-2014,15-OCT-2014,11,SE,YES
Please help me in writing sql query, can we write case condition for this?
Naresh

HI
And I have to select remaing records from first row only
I mean
empid,ename,sal,Jdate,Rdate,deptid,designation,Terminated_Emp
1,a,100,15-May-2014,null,11,SE,NO
1,null,null,null,15-OCT-14,null,null,YES
2,b,200,12-Jan-2014,null,12,SSE,NO
I want records like
1,a,100,15-May-2014,15-OCT-2014,11,SE,YES
2,b,200,12-Jan-2014,null,12,SSE,NO
Naresh

Similar Messages

  • How to select a row from duplicate set of records?

    I want to select a row from a duplicate set of records.
    below is the explanation of my requirement.
    select * from test_dup;
    COL_BILL     COL_SERV     COL_SYS
    b1     s1     c
    b1     s1     g
    b1     s2     c
    b1     s2     g
    b2     s2     g
    b2     s3     c
    b2     s3     g
    b3     s3     c
    Here what I want is, for a distinct col_sys if col_bill and col_serv is same then I need the row where col_sys='c'
    from the above result set I need the following:
    b1     s1     c
    b1     s2     c
    b2     s3     c
    I am using the following SQL query which is giving me the correct result set. But it will hamper the performance because there are total 45 columns in the table and total volume is around 50 million.
    select * from test_dup where col_bill||col_serv in (
    select col_bill||col_serv from (
    select col_bill,col_serv,count(*) from test_dup
    where col_sys in ('c','g')
    group by col_bill,col_serv having count(*) >1)) and
    col_sys='c';
    Can anyone please provide me the optimize SQL query for the same.

    Hi,
    Another way,
    SQL> with test_dup
    as
         select 'b1' col_bill, 's1' col_serv, 'c' col_sys from dual union all
         select 'b1', 's1', 'g' from dual union all
         select 'b1', 's2', 'c' from dual union all
         select 'b1', 's2', 'g' from dual union all
         select 'b2', 's2', 'g' from dual union all
         select 'b2', 's3', 'c' from dual union all
         select 'b2', 's3', 'g' from dual union all
         select 'b3', 's3', 'c' from dual
      select col_bill, col_serv, min(col_sys) col_sys
        from test_dup
       where col_sys in ('c', 'g')
    group by col_bill, col_serv
      having count( * ) > 1
         and count(nullif(col_sys, 'g')) > 0;
    CO CO C
    b1 s1 c
    b2 s3 c
    b1 s2 c
    3 rows selected.Regards
    Peter
    Edited by: Peter on Feb 18, 2009 1:10 AM
    - Added test data, thanks Karthick

  • SQL Query to retrieve one line from duplicate records

    Hi
    I have one table which contains duplicate records in multiple column but the difference is in one column which contains the value 0 or positive. The query i want is to retrieve only the line with the positive value for only the duplicated records.
    here below a sample data for your reference:
    CREATE TABLE TRANS
      CALLTRANSTYPE     NVARCHAR2(6),
      ORIGANI                 NVARCHAR2(40),
      TERMANI                 NVARCHAR2(40),
      STARTTIME               DATE,
      STOPTIME                DATE,
      CELLID                  NVARCHAR2(10),
      CONNECTSECONDS          NUMBER,
      SWITCHCALLCHARGE        NUMBER
    INSERT INTO TRANS VALUES ('REC','555988801','222242850',to_date('05/15/2012 09:15:00','mm/dd/yyyy hh24:mi:ss'),to_date('05/15/2012 09:15:25','mm/dd/yyyy hh24:mi:ss'),null,25,0)
    INSERT INTO TRANS VALUES ('REC','555988801','222242850',to_date('05/15/2012 09:15:00','mm/dd/yyyy hh24:mi:ss'),to_date('05/15/2012 09:15:25','mm/dd/yyyy hh24:mi:ss'),null,25,18000)
    INSERT INTO TRANS VALUES ('REC','555988801','222242850',to_date('05/15/2012 09:18:03','mm/dd/yyyy hh24:mi:ss'),to_date('05/15/2012 09:18:20','mm/dd/yyyy hh24:mi:ss'),null,17,0)
    The output i want to have is:
    CALLTRANSTYPE     ORIGANI          TERMANI          STARTTIME          STOPTIME          CELLID          CONNECTSECONDS          SWITCHCALLCHARGE
    REC          555988801     222242850     05/15/2012 09:15:00     05/15/2012 09:15:25               25               18000
    REC          555988801     222242850     05/15/2012 09:18:03     05/15/2012 09:18:20               17               0 Thank you.

    Hi ekh
    this is the query i want to have, thank you for the help:
    SQL> Select *from
    select CALLTRANSTYPE,ORIGANI,TERMANI,STARTTIME,STOPTIME,CELLID,CONNECTSECONDS,SWITCHCALLCHARGE
    ,row_number() over( partition by     STARTTIME    ,STOPTIME order by    SWITCHCALLCHARGE DESC     ) rn from TRANS
    where rn=1;  
    CALLTR ORIGANI                                  TERMANI                                  STARTTIME STOPTIME  CELLID     CONNECTSECONDS SWITCHCALLCHARGE     RN
    REC    555988801                                222242850                                15-MAY-12 15-MAY-12                        25            18000      1
    REC    555988801                                222242850                                15-MAY-12 15-MAY-12                        17                0      1Regrads
    Lucienot.

  • Selecting all columns from table in a model query

    I have written a model query which joins 4 tables, applies some rules and returns updated rows by selecting 4 columns out of this. Currently it works fine because all 4 columns are from MODEL aliases. Now I need to select 16 more columns in the same query but none of these columns is added in MODEL aliases. When I tried selecting these columns I got oracle error ORA - 32614. Can someone please guide me to include these columns in the same model query?
    I tried couple of options but no luck. Here are those options for ready reference:
    1. I cannot nest existing model query into another select because there are no columns avaiable to join in output of current query to map with other records.
    2. I cannot include all 16 columns in MODEL aliases because some of these columns are actually output of user defined functions.
    I am using Oracle database version 11g Release 11.2.0.1.0.
    Edited by: Anirudha Dhopate on Jan 23, 2011 5:43 PM

    Thank you Avijit for your reply. There is a syntax error on the ON in this part of the statement which I don't know how to fix - I tried messing around with another INNER JOIN but am not confident that I'm doing the right thing:
    SENAlertType.SENAlertTypeIDONClassMember.ClassMemberStudentID
    Thanks for your help! I will need to do some more bedtime reading on joins.
    Daniel

  • Need help using Select statement  to  retrieve one record

    Hi guys, my first post so be gentle please. The basis of this fucntion is to search my dtabase using the select statement to find the record the user wants, by retrieving the name of theitem from the text box. details are then displayed on a joption message box.
    Everytime I run this program it throws an exception 'Exception: null'. Can anyone see where I am going wrong, I have only bn learning java for thepast 6 months so perhaps I am doing something wrong.
    Or perhaps there is another way for me to close the st, con, rs?
    Your help appreciated
    public void searchproducts(){
    Connection con = null;
    Statement st = null;
    ResultSet rs = null;
    try{
    //creating and loading a database connection
    String dbUrl = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=db2.mdb;"; // String dbUrl = "jdbc:odbc:people";
    String user = "";
    String password = "";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection c = DriverManager.getConnection(
    dbUrl, user, password);
    int count;
    st = con.createStatement();
    rs = st.executeQuery("SELECT ItemName, Country, Yearmade, ValuePrice, Forsale FROM Collectman WHERE ItemName="+" '"+txtsearchproduct.getText()+"'" );
    while(rs.next()) {
    String ItemName = rs.getString(1);
    String Country = rs.getString(2);
    String Yearmade = rs.getString(3);
    String ValuePrice = rs.getString(4);
    String Forsale = rs.getString(5);
    JOptionPane.showMessageDialog(null, "product details are: " + ItemName + " " + Country + " " + Yearmade + " " + ValuePrice + " " + Forsale);
    //It keeps on throwing this excpetion with null
    catch (Exception e) {
    System.err.println("Exception: " + e.getMessage());
    } finally {
    try {
    if(rs != null)
    rs.close();
    if(st != null)
    st.close();
    if(con != null)
    con.close();
    } catch (SQLException e) {
    }

    And while we're waiting on that, I'll just say it's nice to see you almost got the general layout of a db call correct...that's a rare thing around here. The finally should have a try/catch round each of the close statements and not around all three in one go. If the resultset throws an exception in your version then you would fail to close either the statement or the connection.
    The second thing is, look up PreparedStatements. They're a better choice for handling SQL requiring variables than using a bog standard Statement.

  • Select LONG column from Remote table

    Hi to all.
    I have the following problem: I can't select a LONG column from remote database, when there are Join operation in the query. Example:
    SELECT long_column FROM remote_table@DB
    -> that's OK, but:
    SELECT long_column FROM remote_table@DB INNER JOIN remote_table2@DB ON ...
    -> returns: "ORA-00997: Illegal use of LONG datatype"
    I cannot even perform (because there a LONG column in the joined table):
    SELECT nonlong_column FROM remote_table@DB INNER JOIN remote_table2@DB ON ...
    That's very strange to me because:
    SELECT long_column FROM local_table INNER JOIN local_table2 ON ...
    -> is OK!
    Can someone help me to SELECT a long column from remote in SELECT query with JOIN clause!
    Thanks a lot!

    Hi
    "Distributed queries are currently subject to the restriction that all tables locked by a FOR UPDATE clause and all tables with LONG columns selected by the query must be located on the same database. " by otn docs.
    I have no idea.
    Ott Karesz
    http://www.trendo-kft.hu

  • Select sql running very long when made to select many columns from a table

    Hi,
    I am using an Oracle DB 10g. I have a table with 250 columns and I need to select 200 columns out of them. The table holds around 0.1 million rows. But when I run the select sql it takes 15 mins to return .1 million rows. Where as if I select only 10 or 15 columns the sql runs in less than a minute returning same number of rows. My sql looks like below:
    select p.col1,p.col2,.......,p.col200
    from table Parent p;
    The table also has a Primary key Index but it does not seem to be using it even when I forced an Index hint. Could you pls help?

    961796 wrote:
    I am using an Oracle DB 10g. I have a table with 250 columns and I need to select 200 columns out of them. The table holds around 0.1 million rows. But when I run the select sql it takes 15 mins to return .1 million rows. Where as if I select only 10 or 15 columns the sql runs in less than a minute returning same number of rows. My sql looks like below:
    As Sven points out, it is likely that most of your time is network (and client) time. You are sending 20 times as much data (based on column counts, at any rate) across the network, and concerned that it's taking 15 times as long.
    If you're testing from SQL*Plus then setting the arraysize to a value larger than the default might help.
    If you have control over the SQL*Net settings then you may get some benefit by adjusting the SDU sizes at both ends of the link.
    If you have control over the tcp configuration (transmit and receive buffer sizes) then you may get some benefit by adjusting these.
    Simple test, by the way, if you're on SQL*Plus
    set autotrace traceonly statistics
    select ...This will dump your data in the bit bucket as it arrives giving you
    a) the database time plus network time
    b) some statistics including the volume of data down the network and the number of network round-trips that Oracle saw.
    Regards
    Jonathan Lewis
    http://jonathanlewis.wordpress.com
    Author: <b><em>Oracle Core</em></b>

  • Select multiple columns from tab with group by

    hi
    i have a table with many columns suppose in that i have 2 columns as date and time
    and in my table i am getting suppose data like
    columns -> date time a b c
    22-may-2207 10
    22-may-2207 17
    23-may-2007 5
    24-may-2007 12
    24-may-2007 15
    i want to select all columns with data with unique data with max of time for that particular date
    answer :-
    22-may-2007 17 a b c
    23-may-2007 5 a b c
    24-may-2007 15 a b c
    with all other cloumns also with this
    plz help me i am trying too much and not able to solve this.

    try this
    select datecol, timecol, a, b, c
    from (
    select datecol, timecol, a, b, c,
            rank() over(partition by datecol order by timecol desc) rn
    from my_table
    where rn = 1
    order by datecol;Message was edited by:
    Jens Petersen

  • How to create a LookUp field, selecting a value from another record type ??

    Hello all,
    I am looking to create a new field in Opportunity in CRM On Demand where we can give the user an option to select one of the records. Data in this search applet should come from another Record Type in CRM On Demand. I am not able to find any similar literature in the Help books.
    I would appreciate if somebody could give me real life steps please.
    Thanks a lot

    Okay. I found the solution to this.
    Now, along with this single reference, we also wish to bring the values of some more joined fields from the other record type in the Opportunities screen.
    We were hoping that the Default value, and using fx, we should be able to achieve this using "joinedfieldvalue". However, it's not bringing back any value. Our expression is -
    JoinFieldValue('<CustomObject3>',[<CustomObject3Id>],'<stState_ITAG>')
    Please help if you have done this before.
    Thanks & Regards

  • [10g] Simple question on selecting a column in same record as a min value

    I have the following sample data to illustrate my question:
    CREATE TABLE     t_1
    (     c_a     VARCHAR2(1)
    ,     c_b     NUMBER(2)
    ,     c_c     DATE
    INSERT INTO     t_1
    VALUES('A',1,SYSDATE);
    INSERT INTO     t_1
    VALUES('A',2,SYSDATE+1);
    INSERT INTO     t_1
    VALUES('A',3,SYSDATE+2);
    INSERT INTO     t_1
    VALUES('A',4,SYSDATE+3);
    INSERT INTO     t_1
    VALUES('B',3,SYSDATE+6);
    INSERT INTO     t_1
    VALUES('B',4,SYSDATE+7);
    INSERT INTO     t_1
    VALUES('B',5,SYSDATE+8);I have the following query which allows me to select the value of c_c that corresponds with the minimum value of c_b grouped by c_a. In other words, my goal is: for each value in c_a, I want the value of c_c that corresponds with the minimum value of c_b.
    SELECT     t1.c_a
    ,     t1.c_c
    FROM     t_1 t1
         SELECT      c_a
         ,      MIN(c_b)     AS min_c_b
         FROM      t_1
         GROUP BY c_a
         ) t2
    WHERE     t1.c_a     = t2.c_a
    AND     t1.c_b     = t2.min_c_b
    ;This query works, but I'm wondering if there is a more efficient way to do this. Something without a subquery? I can't come up with anything else... It just gets a bit ugly with my real data, as there are probably 10 columns of data instead of 3. (Still only 1 minimum value though).

    Hi,
    Since you're using Oracle 10, you can do this:
    SELECT       c_a
    ,       MIN (c_c) KEEP (DENSE_RANK FIRST ORDER BY c_b)     AS c_of_min_b
    FROM       t_1
    GROUP BY  c_a
    ;This won't work in earlier versions.
    What do you want in case of a tie?
    For example, if we add one more row to the sample data:
    INSERT INTO     t_1 (c_a,  c_b,  c_c)
                 VALUES ('A',  1,    DATE '2001-01-01');Now there are two rows, one dated today and one dated 2001, that have an equal claim to being the "first" in group 'A', since they have the exact same value of c_b.
    The query above would display the row from 2001, that is, the earliest of the contenders. That's what MIN means in the query above. Instead of MIN, you can use any aggregate function, but, of course, that function has to work on your data type.
    If you want to show all the contenders in case of a tie, then you can stick with your original query, or you could use the analytic RANK function.
    WITH     got_rnk          AS
         SELECT     c_a, c_c
         ,     RANK () OVER ( PARTITION BY  c_a
                               ORDER BY          c_b
                        )   AS rnk
         FROM    t_1
    SELECT     c_a, c_c
    FROM     got_rnk
    WHERE     rnk     = 1
    ;This approach requires a sub-query, because analytic functions (like RANK) are computed after the WHERE clause is applied. If we want to use the results of an anlytic func tion in a WHERE clause, we have to compute the function in a suib-query first.

  • Selecting a column from a table conditionally

    I'm trying this select statement (example):
    select np.phone_number from name_phone np, reservation_general_view rgv
    where rgv.resort='HVILLA'
    In this table there is a column called "phone_type" and I only want my column to display values that are associated to the column when the "phone_type" = 'EMAIL'
    I tried this:
    select np.phone_number(select np.phone_number from name_phone np where np.phone_type='EMAIL')
    from name_phone np, reservation_general_view rgv
    where rgv.resort='HVILLA'
    This doesn't seem to work...can someone tell me how to do that?
    Message was edited by:
    JonnyOpera

    select phone_number from name_phone
    where resort='HVILLA'
    and phone_type='EMAIL'

  • How do I create a Search Help which selects some rows from a table?

    Well, I'm trying to be specific.
    I have a parameter with a search help that shows columns BUKRS and BUTXT for ALL rows form the table T001. What I need is restrict the search help to show only three Values: BUKRS = 1000 BUKRS = 2000 BUKRS = 3000. This is like a search help with a SELECT sentence. How do I do it?

    Hi.,
    you can go through thomas sir's suggestion. But my suggestion is If you are working in a custom Report , dont change it at Table level or Domail Level., Instead, use Function Module for Custom F4 help.,
    AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_bukrs.
    CALL FM F4IF_INT_TABLE_VALUE_REQUEST.
    check this help for your reference: http://help.sap.com/saphelp_nw04/helpdata/en/9f/dbaac935c111d1829f0000e829fbfe/content.htm.
    If you are using Standard Report Check for Exits.
    hope this helps u.,
    Thanks & Regards,
    Kiran

  • Select a column from an inner select statement

    On my oracle 10g running on solaris, I am running this little query that gives me
    the right results (I needed one more column and not able to get to it, hence looking for some ideas here)
    Select a.othtype, a.date_from, a.date_to,b.process
    from bio_plan_other a,
    bio b,
    Where a.bkey = b.bkey
    and a.date_from <
    (Select min(f.edate)
    from bio_service f
    where f.bkey = a.bkey) ;
    In the inner select table bio_service, I have an additional column called service_name. Hence for the min(f.edate), there is a corresponding service_name.
    How do I get that service_name in my topmost SELECT statement ?
    In other words, I wanted to do something like...
    Select a.othtype, a.date_from, a.date_to,b.process,f.service_name
    from bio_plan_other a,
    bio b,
    Where a.bkey = b.bkey
    and a.date_from <
    (Select min(f.edate)
    from bio_service f
    where f.bkey = a.bkey) ;
    But this gives me,
    ORA-00904 : f.service_name : invalid identifier
    Thanks

    Hi
    Not knowing your datamodel, but you probably have more service names per date, so did you try this?
    WITH t_bio_service AS (SELECT b2.bkey, b2.min_edate, b.service_name
                             FROM bio_service b
                                , (SELECT MIN(edate) min_edate, bkey
                                     FROM bio_service
                                    GROUP BY bkey) b2
                            WHERE b2.bkey = b.bkey
                              AND b2.min_edate = b.edate)
    SELECT a.othtype, a.date_from, a.date_to,b.process, t.service_name
      FROM bio_plan_other a,
           bio b,
           t_bio_service t
    WHERE a.bkey = b.bkey
       AND t.BKEY = b.bkey
       AND a.date_from < t.min_edateHope this helps.
    Regards Auke

  • Selecting only columns from a table

    Could someone help me with a SQL query to select all the columns in a table as an array?

    Hello
    if you are on 9i upward, you can define a pl/sql table of the rowtype and use bulk collect to populate that table, otherwise, you can create separate array types for each column:
    create table dt_test_tab (id number, col1 varchar2(10));
    insert into dt_test_tab values(1, 'X');
    insert into dt_test_tab values(2, 'X');
    insert into dt_test_tab values(3, 'X');
    insert into dt_test_tab values(4, 'X');
    insert into dt_test_tab values(5, 'X');
    commit;
    declare
        type tt_TestTab is table of dt_test_tab%rowtype index by binary_integer;
        type tt_TestTabid IS TABLE OF dt_test_tab.id%type index by binary_integer;
        type tt_TestTabcol1 IS TABLE OF dt_test_tab.col1%type index by binary_integer;
        lt_TestTab     tt_TestTab;
        lt_Id          tt_TestTabId;
        lt_col1        tt_TestTabcol1;
    begin
        --use the rowtype array
        select
             id,
             col1
        BULK COLLECT INTO
             lt_testtab
        FROM
            dt_test_tab;
       --use the individual column arrays
        select
             id,
             col1
        BULK COLLECT INTO
             lt_id,
             lt_col1
        FROM
            dt_test_tab;
        dbms_output.put_line('loop through the rowtype array');
        FOR x in lt_TestTab.FIRST..lt_TestTab.LAST LOOP
           dbms_output.put_line(to_char(lt_TestTab(x).id)||to_char(lt_TestTab(x).col1));
        end loop;
        dbms_output.put_line('loop through the two column arrays');
        FOR x in lt_Id.FIRST..lt_Id.LAST LOOP
           dbms_output.put_line(to_char(lt_Id(x))||to_char(lt_Col1(x)));
        end loop;
    END;

  • Selecting date column from a table

    hi
    I have a problem that looks wierd for me.
    I have a table called blog_post which has a date field called publish_date
    I have done this first.
    alter session set nls_date_format='dd/mm/yyyy';
    then
    SQL> select publish_date from blog_post;
    PUBLISH_DA
    09/07/2007
    02/07/2007
    02/07/2007
    02/07/2007
    09/07/2007
    now the wierd thing is that when i am selectin the same date it is not returning anything
    SQL> select publish_date from blog_post where publish_date='09/07/2007';
    no rows selected
    Thanks for any help.
    Ganesh

    I don't know anything about Hibernate, but the reason why you need to specify the trunc(publish_date) = to_date('09/07/2007','dd/mm/yyyy') is because of the way that Oracle stores date/time internally.
    It's labelled as a date field, but the time is automatically stored as well. So, if you're just wanting to compare the date to another date, you need to ignore the time part of the column - the trunc function does this.
    As for the "to_date(<date>, <date mask>)", well, you want to compare a date to a date, not a string. You could rely on the implicit conversion, governed by the nls settings, but I really, really, really wouldn't. In fact, I can't stress enough how much of a bad idea that would be! It's best to make things explicit.

Maybe you are looking for

  • Full flash browser - background

    I am a very inexperienced actionscripter so any help here would be great. I am trying to load a random background in a flash clip, and resize that into the full browser dimensions. What I have working now is an "unscaled" resize... The background cli

  • Problems burning playlists?

    Oh, boy...what doo-doo. When I want to burn a playlist, it burns some or most (depending on how it "feels") and then spits out the disk as unfinished (having 2 unfinished DVD DL discs and one single layer DVD disc as proof). Between this and the loss

  • Date formats, language

    we are using jdeveloper 10.1.3.4 and rolled out an application to the oc4j server in jdeveoper we specified the dat format in formatinfo.xml, in the oc4j server we get an error because the date format does not match: ....AND ( ( (Dinehmer.DI_GEBDAT =

  • In Design not starting up.

    Hi; I installed the complete CS6 on a Win 7 x64 Acer M5 laptop. Everything works except In Design. I re-installed In Design and it made no difference. The program closes before fully starting and Windows just says the program closed and has a problem

  • Select distinct (field) from [@tab]

    Hello, *i tried select with distinct query in SDK but it id not working... how we will use this functionality in SDK but same query working fine in SQL server* By Firos.C