Best way to select distinct values, based on another column?

I have a table with three columns: id (NUMBER), data_dt (DATE) and data_value (NUMBER).
There is a primary key id and data_dt.

I want to choose the folder with the last data_dt for each unique identification number.

What is the best way to do it?
I have three different methods, but there may be something that I am missing.

First of all:
SELECT *
FROM
(
  SELECT id, data_dt, data_value,
         FIRST_VALUE(data_dt)
         OVER (PARTITION BY id ORDER BY data_dt DESC) AS last_data_dt
  FROM the_table
)
WHERE data_dt = last_data_dt;
(I use ORDER BY... DESC, instead of just ORDER BY I don't need the clause BETWEEN the LINES)


Second:
SELECT t1.*
FROM the_table t1
JOIN
(
  SELECT id, MAX(data_dt) AS last_data_dt
  FROM the_table
  GROUP BY id
) t2 ON (t2.id = t1.id AND t2.data_dt = t1.data_dt);
Third:
SELECT t1.*
FROM the_table t1
WHERE t1.data_dt =
(
  SELECT MAX(t2.data_dt)
  FROM the_table t2
  WHERE t2.id = t1.id
);
-Don

As mentioned in hoek, you will need to test all scenarios.

However, in my opinion the more faster will be those who accesses the table that only ONCE instead of TWICE as some scenarios require.

HTH!

Tags: Database

Similar Questions

  • best way to create a table based on another table

    Hello
    I am trying to create a table based on another table with all the data in it. It contains important data.

    create table < tablename > select * from table1.

    Is the best way to do it, or is there another way. Please advice.

    Thank you

    Insert / * + append * / in as select * from ;

    It should be->

    insert /*+ append */ into 
    select * from ;
    

    Kind regards.

    LOULOU.

  • AF:Switcher, handfuls of line column values based on another column in the table

    Hello

    I have a question if you can help me on this. It's on af:switcher.

    What I'm trying to do this - is in a table of the adf, based on the value of a column, I want another column to display as a link or not.
    For example, if the status column is 1, then the id column display as a link (I just put a h:outputLink around outputText to do), if it is then show him without bond.

    For this I put two different facets and opportunities switcher to decide what facet of show which line.

    BUT what is happening is that only on opportunities is made. Either / or scenario. It is not made each line according to the value of the status column.

    What is planned.

    I just tried with the dept table.

                    
                    
                    
                      
                    
                    
                    
                        
    
                      
                    
    

    The lines for which the deptno > 20, I wanted to display the pass as link and for the deptno<=20, log="" will="" be="" an="" input="" text.="" it="" works="" fine="" for="" me.="" probably,="" you="" may="" need="" to="" get="" rid="" of="" h:outputlink="" and="" use="" commandlink="" golink="">

    Arun-

  • Get the cumulative values in a single column based on another column in reports

    Hi all

    I have a requirement to get cumulative values based on another column.
    I 'Sales rep name' in the first column.
    Correspondent "Values of the invoice line" in the second column.
    Want to have cumulative of all the values for each sales invoice line.
    Then apply rank and display the top 10 sales reps based on invoice lines.
    Since there is no rank option in the PivotTable, I do this in the report table.

    Looking for the best entries...

    Thanks in advance...

    Try below
    2nd column: "name of Sales rep.
    column 2: SUM ("invoice line values ' BY 'Name of Sales rep'") and sort this field desc.
    3rd column: fx RANK (SUM ("invoice line values" BY "Sales rep name")), to hide this column, so that you don't confuse your users.

    and put the filter on the 3rd column below 5

    I hope this works for you

  • Select literal values in the same column

    Hello
    like the practice:

    Select 'A', 'B', 'C' for double
    will select these values in their own column on the same line, is there a simple way to select these values in the same column on lines without first their insertion into a table?

    Thank you
    SQL> select 'A' x from dual union
      2  select 'B' x from dual union
      3  select 'C' x from dual ;
    
    X
    -
    A
    B
    C
    

    A bit complicated:

    SQL> select chr(ascii('A')+level-1) x
      2  from dual
      3  connect by level <=3;
    
    X
    -
    A
    B
    C
    

    Other:

    SQL> select regexp_substr('ABC','[[:alpha:]]',1,level) x
      2  from dual
      3  connect by level <= length('ABC');
    
    X
    ---
    A
    B
    C
    

    Max
    http://oracleitalia.WordPress.com

    Published by: Massimo Ruocchio, February 12, 2010 17:55
    added a second

    Published by: Massimo Ruocchio, February 12, 2010 17:58
    Added the third

  • Best way to express Boolean values in a table?

    Hello

    I'm looking for the best way to express Boolean values in a table. Is there a better way than this one?

    Men Women Children
    YesYesNO.
    NO.YesNO.

    Or I guess I could draw that checked sign for Yes and X sign for no.? Or is there anywhere I can download those?

    Thank you!

    That would you say a solid bullet for YES, hollow for no-much easier to see the difference at a glance.

  • What is the best way to migrate your Adobe software on another computer?

    What is the best way to migrate your Adobe software on another computer?

    Install the software, does not migrate/transfer it.  If you need to download links for the software and see you not in your Adobe account online, just determine what applications or Suites is necessary and it might be possible to provide links.

  • Delete based on another column dublicate records

    How can I remove duplicate records, based on another column.

    the table structure
    CREATE TABLE IA_EXPORT_LEVEL
    (
    A_NO VARCHAR2 (22 BYTE),
    IM VARCHAR2 (18 BYTE),
    ctn_level NUMBER (2)
    )

    reviews
    A_NO IM ctn_level

    S1 S1 1
    S1 M1 2
    S1 h1 3
    H2 S2 1
    S2 h2 2
    S3 h1 2
    S3 h1 3
    S4 h6 4
    S4 h6 5

    I want to delete all records other than the minimum value of the ctn_level

    output should like below
    S1 S1 1
    H2 S2 1
    S3 h1 2
    S4 h6 4

    Published by: OraFighter on December 16, 2011 13:44
    DELETE FROM IA_EXPORT_LEVEL where rowid IN
    (select rid from
    (select rowid rid, row_number() over (partition by A_NO,IM order by ctn_level) rn from IA_EXPORT_LEVEL )
    where rn > 1 )
    /
    

    Hope your question.

  • How do column filter based on another column

    In my report, I have column a thru d. I would like to create a filter based on another column where column is column B. I can enter a value that the column cannot be equal to, but I would use a column instead of a value.

    Thank you.
    Jonathan

    Try this: icon on the filter columns->-> convert this advanced filter for SQL
    You can reference any other column

    BTW: Have you had a chance to look at my suggestion for your post
    Tables from a Table

  • What is the best way to select multiple tracks?

    So, imagine that you have a sequence like this:

    Unselected.png

    And you want to accomplish the following selection:

    Selected.png

    What is the best way to do that has no shift + selection 12 times? I had FCP where I could click the top left and then shift + click on the lower right and get a selection of range in this way. But obviously it works differently in first. If I wanted to include A5 - 7 in the selection, I could do just a selection rectangle, but someone at - it a convenient way to select several clips landlocked like that?

    The fastest way I can think to do is to temporarily make the slightly shorter left selected top clip so that it has an ATM next to him, then use your mouse to drag and drop around all clips. You must again extend the left top clip again after the fact. Alternatively, you could catch the entire section, then shift select the ones that you don't need. In addition, shift-make drag a marquee selection that includes selected clips deselects them.

    See: work with items in a Premiere Pro sequence

  • Best way to store the values of the map

    Hi all.

    I have a card <>where I store the values (in this case, I store the IDs of rows in the table). I have an application with a panel tabs and I have a card for each tab. When the user exits the application, I'll go through the card and the identity of any process

    If I use normal in my grain map, once the user changes tab (or opens a pop up or something like that), the map is instantiated again and all data is lost. To resolve this problem, I used a static map. Everything is ok, but if 2 users use the application at the same time, they will share the card data and I don't want that.

    So I have a question: what is the best way to keep my card? I read some people saying to use one Bean of sessionScope, others a pageFlowBean, other parameters of Page, etc...

    Can you help me?

    P.s: jdev version 11.1.2.4.0

    Well, you can create a managed bean in this scope, or you can put your card directly at your fingertips.

    Here you will find several techniques to get the bean managed in java code: http://www.lkakarla.com/2013/06/retrieving-managed-beans.html

    Dario

  • Pre selection ComboBox VALUE-based

    I use a HTTPService request to get XML data and populate a drop-down list box. I want to pre-select the combo box based on a value that is stored in the XML request... I know I need some kind of a loop to find out what the position is that the value is within the Bay... But for some reason, I'm having a hard to loop through my HTTPService request...

    How to make a loop on this:

    MyHTTPServiceRequestID.lastResult.categories.category

    If I don't do any sense, let me know...

    Looks like you call the method too early - even before the combo is finished being created/populated.

    You will probably need to define a method of result for your HTTPService to instead of using lastResult. In this method once you have your result to be assigned as the dataProvider for the combo, then call the method that selects the value in the drop-down list.

  • Best way to accomplish adding navigation based on securities (cfinclude)

    I need to understand the best display of how a navigation model based on login rights.
    I created 4 models of navigation (1 for no connection, 3 for good security connections).
    What I have tried does not work correctly. (Everything works up until the last cfif not IsDefine.) If session.mm_userauthorization is empty then no naviagion model appear at all on the page. If I veiw in the debugging session variables it shows mm_userauthorization =) when a user logs in the model updates correctly based on login rights.
    What is the best way to achieve this? Thank you!

    < cfif IsDefined ('Session.mm_userauthorization') AND ListFindNoCase (1, Session.mm_userauthorization) gt 0 >
    < cfinclude template = "side_buttonsOLDp.cfm" >
    < cfif IsDefined ('Session.mm_userauthorization') AND ListFindNoCase (2, Session.mm_userauthorization) gt 0 >
    < cfinclude template = "side_buttonsOLDf.cfm" >
    < cfif IsDefined ('Session.mm_userauthorization') AND ListFindNoCase (3, Session.mm_userauthorization) gt 0 >
    < cfinclude template = "side_buttonsOLDb.cfm" >
    < cfif NOT ISDefined ('Session.mm_userauthorization') >
    < cfinclude template = "side_buttonsOLD.cfm" >
    < / cfif > < / cfif > < / cfif > < / cfif >

    I need to understand the best display of how a navigation model based on login rights.
    I created 4 models of navigation (1 for no connection, 3 for good security connections).
    What I have tried does not work correctly. (Everything works up until the last cfif not IsDefine.) If session.mm_userauthorization is empty then no naviagion model appear at all on the page. If I veiw in the debugging session variables it shows mm_userauthorization =) when a user logs in the model updates correctly based on login rights.
    What is the best way to achieve this? Thank you!









  • How to concatenate values based on multiple columns of data duplicate in the loop

    Hi all

    Select '100' col_1, '111111' col2 of double

    Union of all the

    Select '100' col_1, '222222' col2 of double

    Union of all the

    Select '101' col_1, '333333' col2 of double

    Union of all the

    Select '102' col_1, '333333' col2 of double

    I need to write by using cursor loop (configurable) so, it must concatenate col_1 or col_2

    If,

    In the table

    col_1, col_2

    100 111111

    100 222222

    101 333333

    102 333333

    Then

    col_1, col_2

    100 111111,222222

    333333 101 102

    Is it possible to compare and concatenate values in the loop

    I have more than 4,000 bytes in the table of col_1,col_2 data values

    Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64 bit Production

    Ok then:

    create a type that makes "grouped" clob

    CREATE or REPLACE TYPE clob_obj IS OBJECT
    (
    CLOB theClob
    ORDER MEMBER FUNCTION equals (in_clob_obj IN clob_obj) RETURN NUMBER
    member function getClobVal return clob)
    /

    CREATE or REPLACE TYPE clob_obj BODY
    IS
    FUNCTION MEMBER ORDER is equal to (in_clob_obj IN clob_obj)
    RETURN NUMBER
    IS
    BEGIN
    RETURN sys.dbms_lob.compare (SELF.theClob, in_clob_obj.theClob);
    END;
         
    Member return clob getClobVal function
    is
    BEGIN
    Return self.theClob;
    end;
    END;
    /

    WITH conc_table AS (SELECT 1111 cust_id, cust_no 'ABC', '101010' ordr_no OF DOUBLE UNION ALL

    SELECT 2222 cust_id, cust_no 'ABC', '101010' ordr_no OF DOUBLE UNION ALL

    SELECT 3333 cust_id, cust_no 'ABC', '101010' ordr_no OF DOUBLE UNION ALL

    SELECT cust_id 4444, "CCC" cust_no, ordr_no ' 123456' OF DOUBLE UNION ALL

    SELECT cust_id 5555, "CCC" cust_no, '454545' ordr_no OF DOUBLE UNION ALL

    Select cust_id 66666, "CCC" cust_no, ordr_no ' 77777' DOUBLE UNION ALL

    SELECT cust_id, "CCC" cust_no, ordr_no ' 77777 66666' FROM DUAL)

    SELECT rtrim (XMLAGG (XMLELEMENT (E, cust_id_no, ',')). EXTRACT ('customer_id //text()').getClobVal (),',')

    clob_obj (ord_number) .getClobVal)

    cust_num

    FROM (SELECT cust_id_no

    cust_num

    rtrim (XMLAGG (XMLELEMENT (E, ord_num, ",")). EXTRACT ('//text ()') .getClobVal (), ',') ord_number

    FROM (SELECT DISTINCT cust_id_no, cust_num, ordr_no AS conc_table FROM ord_num cust_no cust_id)

    GROUP BY cust_id_no

    cust_num)

    GROUP BY clob_obj (ord_number)

    cust_num;

    I got the distinct in the select more intimate for no duplicate of the basetable is only sent to reunification of...

    HTH

  • Selection of data based on 2 columns

    Hello

    I have a table like this,
    Version: 
    
    Oracle9i Enterprise Edition Release 9.2.0.5.0 - Production
    PL/SQL Release 9.2.0.5.0 - Production
    CORE     9.2.0.6.0     Production
    TNS for 32-bit Windows: Version 9.2.0.5.0 - Production
    NLSRTL Version 9.2.0.5.0 - Production
    CREATE TABLE EMPSELECT( EMP NUMBER , CAR1 CHAR(2),CAR2 CHAR(2) );
    
    INSERT INTO EMPSELECT VALUES( 100 ,   'XX',    'YY');
    INSERT INTO EMPSELECT VALUES( 100 ,   'CC',    'YY');
    INSERT INTO EMPSELECT VALUES( 100 ,   'CS',    'XX');
    INSERT INTO EMPSELECT VALUES( 101 ,   'XX',    'YY');
    INSERT INTO EMPSELECT VALUES( 102 ,   '2X',    'YY');
    INSERT INTO EMPSELECT VALUES( 102 ,   'SS',    'Y2');
    INSERT INTO EMPSELECT VALUES( 103 ,   'RR',    'XX');
    INSERT INTO EMPSELECT VALUES( 103 ,   'RC',    'XX');
    INSERT INTO EMPSELECT VALUES( 103 ,   'CC',    'CS');
    INSERT INTO EMPSELECT VALUES( 101 ,   'TC',    'XX');
    INSERT INTO EMPSELECT VALUES( 109 ,   'TC',    'FF');
    I want select records from Emp where XX is not present in the two columns CAR1, CAR2 if find 1 line containing XX

    OUTPUT:
    EMP
    102
    109

    Thank you
    Ananda

    Published by: Ananda on June 4, 2010 16:19

    Published by: Ananda on June 4, 2010 16:20

    Something like that?

    SELECT *
      FROM empselect
     WHERE emp NOT IN (SELECT DISTINCT emp
                                  FROM empselect
                                 WHERE car1 = 'XX' OR car2 = 'XX');
    

    * 009 *.

Maybe you are looking for

  • Safari Preferences

    Is someone can you please tell me where I can find "Préférences" in Safari? Will have a "too many redirections" problem and I figured that the solution is to change the settings through Safari > Preferences > privacy etc, but I can't find preferences

  • Problems with its Satellite A300 controller

    Hi all! I have a weird problem with my sound regulator in A300. When I use 'wheel' in front of the laptop, next to the headphones of entry etc., sound turns upward and downward very irregular - I try to turn up the volume and it jerks around a consta

  • How to export entire graphic story?

    I was reading help ( http://zone.ni.com/reference/en-XX/help/371361L-01/lvconcepts/customizing_graphs_and_charts/#Exporti... ) on the graphic waveform and I came across this little piece: "for graphics, you can only export data that is visible in the

  • I want to close 'request a genuine windows software' from my office

    in my laptop for a few days its display 'request a genuine windows software' in my office. I want to remove it from my office and I don't want to buy genuine software Help, please

  • HP Jet 7 Bluetooth

    Just got a stream 7 and Bluetooth will not work.  Device Manager says it all right, I have the latest drivers and BIOS, but when I go to connect, the flow is to the search and no other device he sees and he sees nothing. I begin to suspect a hardware