How to get the different records for the date max.

Hi all

Here is the sample sql for sample table and data.

Create table student (dept_id number(10), first_name varchar2(10),last_name varchar2(10),join_date date,years_attended number(10));

insert into student values (1,'Ann','Coleman',to_date('3/7/1917','MM/DD/YYYY'),4);
insert into student values (1,'Ann','Coleman',to_date('3/7/1916','MM/DD/YYYY'),5);
insert into student values (2,'Rock','Star',to_date('1/1/1920','MM/DD/YYYY'),5);
insert into student values (2,'Rock','Star',to_date('1/1/1921','MM/DD/YYYY'),6);
insert into student values (3,'Jack','Smith',to_date('7/1/1900','MM/DD/YYYY'),3);

insert into student values (3,'Jack','Smith',to_date('7/1/1901','MM/DD/YYYY'),4);

commit;

I need to get maximum date records when the name and dep_id corresponds to. I wrote the query below and it becomes the expected result, but I'm not sure it's quite effective.

SELECT s.dept_id, s.first_name,s.years_attended

FROM (SELECT dept, MAX (join_date) join_date

STUDENT GROUP BY dept_id) x

Student JOIN s ON x.dept_id = s.dept_id AND x.join_date = s.join_date;

This above query returns records like below, and this is the goal.

DEPT_ID NAME YEARS_ATTENDED

1                         Ann                                4

2                         Rock                              6

3                         Jack                              4

Can you please let me know the query SQL I wrote is effective or not? This sample table may have less data, but I'm dealing with millions of records.

Hello

Thanks for posting CREATE TABLE and INSERT statement. This really helps.

Here's a solution. I also added a name that seems logical. In which case you can delete:

Select dept_id, first_name, last_name

, max (years_attended) Dungeon years_attended (last dense_rank order by join_date)

the student

Group of dept_id, first_name, last_name;

DEPT_ID FIRST_NAME LAST_NAME YEARS_ATTENDED

---------- ---------- ---------- --------------

1 Ann Coleman 4

Rock 2 star 6

3 Jack Smith 4

Kind regards.

Alberto

Tags: Database

Similar Questions

  • How to get the date for the first Monday of each month

    Dear members,

    How to get the date for the first Monday of each month.

    I wrote the following code

    SELECT decode (to_char (trunc (sysdate + 30, 'MM'), 'DAY'), 'MONDAY', trunc (sysdate + 30, 'MM'), NEXT_DAY (trunc (sysdate + 30, 'MM'), "LUN")) FROM DUAL

    But he looks at complex bith.

    Abhishek

    Published by: 9999999 on 8 March 2013 04:30

    Use the IW format - it will make independent NLS solution. And all you need is truncated 7th day of each month using IW:

    select  sysdate current_date,
            trunc(trunc(sysdate,'mm') + 6,'iw') first_monday_the_month
      from  dual
    /
    
    CURRENT_D FIRST_MON
    --------- ---------
    08-MAR-13 04-MAR-13
    
    SQL> 
    

    Here is the list of the first Monday of the month of this year:

    with t as(
              select  add_months(date '2013-1-1',level-1) dt
                from  dual
                connect by level <= 12
             )
    select  dt first_of_the_month,
            trunc(dt + 6,'iw') first_monday_the_month
      from  t
    /
    
    FIRST_OF_ FIRST_MON
    --------- ---------
    01-JAN-13 07-JAN-13
    01-FEB-13 04-FEB-13
    01-MAR-13 04-MAR-13
    01-APR-13 01-APR-13
    01-MAY-13 06-MAY-13
    01-JUN-13 03-JUN-13
    01-JUL-13 01-JUL-13
    01-AUG-13 05-AUG-13
    01-SEP-13 02-SEP-13
    01-OCT-13 07-OCT-13
    01-NOV-13 04-NOV-13
    
    FIRST_OF_ FIRST_MON
    --------- ---------
    01-DEC-13 02-DEC-13
    
    12 rows selected.
    
    SQL> 
    

    SY.

  • How to get the Date, month, year of the DateTimePicker

    Hi, I am using the DateTimePicker.

    There are several question about this command:

    1. how to get the Date, month and year?

    -With the help of myDateTime.value, I get this string ' Wed May 08 14:45 ICT 2013 '-> I have to manually analyze this? Because I need in format yyyy/mm/dd. Or can set the format of dateTimePicker.value?

    2 format on dateTimePicker self (on the user interface) is d/m/YY, I can't change to another format?

    Thank you

    dateTimePicker.value return a QDateTime not a string.

    https://developer.BlackBerry.com/Cascades/reference/bb__cascades__datetimepicker.html#property-value

    See more:

    https://developer.BlackBerry.com/Cascades/reference/QDateTime.html#date

    https://developer.BlackBerry.com/Cascades/reference/QDate.html

    You can get

    day = dateTimePicker.value.date().day()
    month = dateTimePicker.value.date().month()
    year= dateTimePicker.value.date().year()
    dateYYYYMMDD = dateTimePicker.value.date().toString(Qt::ISODate)
    dateYYYYMMDD2 = dateTimePicker.value.date().toString("YYYY/MM/DD")
    dateYYYYMMDD3 = dateTimePicker.value.toString("YYYY/MM/DD")
    
  • How to get the data center moref moref VM in c# using

    Hi all

    Could someone tell how to get managedobjectreference of data center using the VM moref? or any other easy way is? If anyone has examples of code in c# please share with me?

    Thank you

    Vijaya

    You can get the data center in which the virtual machine resides by the following search:

    VM Moref-> Parent (vmFolder)

    Moref folder VM-> Parent (data center)

    Parent of the virtual computer object will always be the vmFolder and the vmFolder object will always be a data center.

    For more details on the model of article inventory of vSphere, take a look at this blog post - http://www.doublecloud.org/2010/03/vsphere-inventory-structure-deep-dive/

  • How to get the date of end of week max and its rows of a table

    Hello
    Table [temp2]
     
       id          name        dt
      123        a             2-mar-2010
      124        b            1-feb-2010
      125        c             3-apr-2010
      123        a             13-mar-2010
      125        c             13-mar-2010
      123        a             12-feb-2010
     
    This table how to get this line by id - name (id and the name of the composite key) whose date is the date of last weekend (last Saturday).
    for example, suppose to have IDs 123 values date February 12, 2010, 13-mar-2010 this date falls on the last Saturday 13-mar-2010, how to get the value of the line on Saturday last for all the unique id and the name?

    So I wrote this but probably there a few traps...
    select id,name,dt from (select id,name,dt,max(dt) over(partition by id,name) as 
    max_date from temp2) t where dt=t.max_date and dt in ('3-apr-2010'
    ,'27-mar-2010','20-mar-2010','13-mar-2010')
    Here I have spent in the in clause 4 last weekend dates manually (although either has been calculated by programming) in front.
    Thank you

    Here's another option:

    SELECT     *
    FROM
    (
         SELECT     ID
         ,     NAME
         ,     MAX(CASE WHEN TO_CHAR(DT,'DY','NLS_DATE_LANGUAGE=ENGLISH') = 'SAT' THEN DT END) AS DT
         FROM     TEMP2
         GROUP BY ID, NAME
    )
    WHERE DT IS NOT NULL
    

    It is always useful to provide the following information:

    1. oracle version (SELECT * FROM V$ VERSION)
    2. examples of data in the form to CREATE / INSERT commands.
    3. expected results
    4 explanation of the expected results (alias "business logic")
    5. use.

     tags for #2 and #3. See FAQ (Link on top right side) for details.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
    
  • How to get the rate max one sampling NOR 9263 and other cards?

    Hello!

    I'm using a NI 9263 map and a chassis cDAQ-9172 proyect and im he 8.0 whit CVI programming. IM generating a sine and square waves to do some tests on a radio.

    I want my program to be functional for all cards of this type, and we know that most of the cards have different specifications, for example sampling max tariff, in this case the Pentecost of work NI 9263 100 kech. / s as the maximum. IM generating waves based on the sampling frequency.

    If my program must be compatible with most of the cards, my need to program to acquire max sampling rate using a specific function of NIDAQmx.h.

    Do you know if theres a function or attribute that can return this value?

    I tried this function with different attributes, with no results:

    DAQmxGetTimingAttribute (taskHandle, DAQmx_SampQuant_SampPerChan, & MaxSamp);
    DAQmxGetTimingAttribute (taskHandle, DAQmx_SampClk_Rate, & MaxSamp);
    DAQmxGetTimingAttribute (taskHandle, DAQmx_SampQuant_SampPerChan, & MaxSamp);
    DAQmxGetTimingAttribute (taskHandle, DAQmx_SampClk_TimebaseDiv, & MaxSamp);
    DAQmxGetTimingAttribute (taskHandle, DAQmx_SampClk_Timebase_Rate, & MaxSamp);

    The three first atribbutes gives me the rate real samp which is 1Ks/s (according to me, is the rate of samp set to the default value for all cards you before be initialized for the user), but do not give me samp (100Ks/s) max flow.

    The rest of the attributes only gives me the value of the clk, which is 20 MHz and the divisor of the clk (20000). Also I tried with a card 9264 (max samp rate is 25 ksps / s) and the function returns the same results.

    Any idea?

    Thank you!!

    Hey Areg22,

    I think I've found the service you're looking for:

    http://zone.NI.com/reference/en-XX/help/370471W-01/mxcprop/func22c8/

    This link gives just the syntax for the function, but the following gives you more information about the function:

    http://zone.NI.com/reference/en-XX/help/370471W-01/mxcprop/attr22c8/

    When I used the property of this function node output was 100,000 for the NI 9263. Which is consistent with the plug. I would like to know if it works for you.

    Thank you

    -KP

  • How to get the dates of discontinuity

    If there is discontinuity in dates how we write the query...
    I need to get the dates and the dates of discontinuity.
    I / p:

    item_no first_date second_date
    1 January 1, 10 Aug 1, 10

    1-01 - 7.-10 1er January 11

    1 February 1, 11 1 July 11
    "
    "
    "
    o/p:

    1 1 January 10 01 - aug - 10

    1 02-aug-10 (first_date + 1) 31-aug-10(second_date-1) - dates of discontinuity

    1-01 - sep - 10 (second_date) 1st January 11

    1 January 2, 11 January 31, 11

    1 February 1, 11 1 July 11

    '
    '
    '
    Please let me know is possible
    SQL> select  *
      2    from  tbl
      3  /
    
            ID FIRST_DAT SECOND_DA
    ---------- --------- ---------
             1 01-JAN-10 01-AUG-10
             1 01-SEP-10 01-JAN-11
             1 01-FEB-11 01-JUL-11
    
    SQL> with t1 as (
      2              select  tbl.*,
      3                      lag(second_date) over(partition by id order by first_date) prev_second_date
      4                from  tbl
      5             ),
      6       t2 as (
      7               select  *
      8                 from  tbl
      9              union all
     10               select  id,
     11                       prev_second_date + 1,
     12                       first_date - 1
     13                 from  t1
     14                 where prev_second_date + 1 < first_date
     15             )
     16  select  *
     17    from  t2
     18    order by id,
     19             first_date
     20  /
    
            ID FIRST_DAT SECOND_DA
    ---------- --------- ---------
             1 01-JAN-10 01-AUG-10
             1 02-AUG-10 31-AUG-10
             1 01-SEP-10 01-JAN-11
             1 02-JAN-11 31-JAN-11
             1 01-FEB-11 01-JUL-11
    
    SQL>  
    

    SY.

  • How to get the date of Sunday of the week

    Hello
    I have a column date 'start_date', I need to create a report for all the weeks from Sunday, for example:

    I need to get the dates of the week in the form:

    mm/dd/aa-23/01/11, 01/16/11, 09/01/11 and so on...

    Help, please

    Thank you...

    Hello

    To find the last Sunday before or equal to start_date:

    SELECT     TRUNC ( start_date + 1
               , 'IW'
               ) - 1          AS week_start_date
    ...
    

    You can also use an expression like that in a GROUP BY clause.

    It's not you NLS parameters. Always ISO weeks start on Monday. Your week begins 1 day earlier, that is why the above expression is the + 1 and -1.
    (According to the parameters of the NLS)

    SELECT     TRUNC ( start_date
               , 'D'
               )          AS week_start_date
    ...
    

    could do the same thing, but I still have rocommend with "IW"; a few extra touches is not big thing to pay for a guarantee that it will still work.)

    I hope that answers your question.
    If not, post a small example of data (CREATE TABLE and INSERT statements) and the results desired from these data.

  • How to get the date in the exact day in the field when another?

    Hello

    Please help me how to get the day of the week using another date field.

    In fact, a field (week_day) has 1,2,3,4,5,6,7 here 1-, 2 - KILL like this.
    another field has the date. After this date, go to this particular week and need to pick up the date of the Etiquettemois above. It should be this week there himself.

    Thanks in advance!

    Kind regards
    Florian...

    Published by: Florian on October 29, 2010 03:07

    Maybe it's

    select trunc(date_field,'D') + week_field - 1 from table
    
  • Can someone show me how to get the data of this vi in four columns in a spreadsheet file.

    I'm trying to get the data into four columns on a worksheet. The data consists of two channels and two measures, a maximum voltage and a voltage after x seconds. I enclose the code. Any help would be great. Thank you.


  • How to get the data in the format specified for this table?

    Table:
    ID     NAME     DESC1     COUNTRY_ID     PERFORMANCE
    
    001     abc1     description 1     CON0000006       .67
    001     abc2     descr2            CON0000004       1232.87
    001     abc3     dsc3                     CON0000003        76798
    002     abc4     descr4               CON0000005        8787
    002     abc5     descr5              CON0000001        989.9989
    003     abc6     dfrer                     CON0000001        676.90
    003     abc7     ioioi                    CON0000001        6.8778
    004     abc8     lilli                   CON0000001         334344
    performance column is of type varchar2.

    I want data like this:
    ID     NAME     DESC1     COUNTRY_ID     PERFORMANCE
    001     abc1     description 1     CON0000006       00.67
    001     abc2     descr2            CON0000004       1232.87
    001     abc3     dsc3                     CON0000003        76798.00
    002     abc4     descr4               CON0000005        8787.00
    002     abc5     descr5              CON0000001        989.99
    003     abc6     dfrer                     CON0000001        676.90
    003     abc7     ioioi                    CON0000001        6.87
    004     abc8     lilli                   CON0000001         334344.00
    I use oracle 10g on windows 7.
    can you please how do I get the performance data in this format?

    Try something like:

    SQL> alter session set nls_numeric_characters='.,';
    
    Session altered.
    
    SQL> -- generating sample performance column
    SQL> with t as (
      2  select '.67' perf from dual union
      3  select '1232.87' from dual union
      4  select '76798' from dual union
      5  select '8787' from dual union
      6  select '989.9989' from dual union
      7  select '676.90' from dual union
      8  select '6.8778' from dual union
      9  select '334344' from dual
     10  )
     11  --
     12  -- actual query:
     13  --
     14  select perf
     15  ,      to_char(trunc(to_number(perf), 2), 'fm999900.00') perf2
     16  from   t;
    
    PERF     PERF2
    -------- ----------
    .67      00.67
    1232.87  1232.87
    334344   334344.00
    6.8778   06.87
    676.90   676.90
    76798    76798.00
    8787     8787.00
    989.9989 989.99
    
    8 rows selected.
    
  • How to get the date of deployment for the RT code?

    I'm using LabVIEW 8.5 and MAX 4.4.1f0 and cFP-2110. I want back the date of the deployment. Is it possible to recover the date of deployment for the RT code?

    Thank you

    Martine

    Hi John,.

    You must be able to FTP into your PSC 2110 from a Windows Explorer window and check the date of 'Change' of the startup.rtexe/or-rt/startup folder.

    Aaron P

    National Instruments

    Technical sales engineer

    http://www.NI.com/support

  • How to get the date of request for a date column

    I have a requirement where I need to filter the data between two guests to date. I have only a single column of date and the date in the guests should be chosen by the calendar.
    I saw may approaches to do this, and I have one where two columns of fictitious date must be declared in the repository and used to define the guests of the date.

    but by using other methods I couldn't schedule option in the command prompt.

    but y at - it another way to get the guests with calndar selection without set dummy columns in the repository

    Thank you

    Try adding a column of similar date first and then edit the formula in the column

  • How to get the Date format for a specific date?

    May be it is inept to ask, but I'm aware of the Date.
    which appear on client - it's just that the average customer treats the date.
    Right?
    So what happens if a need to check the format to set the server to date.
    Or need to write common code indicating the date format
    on the server, it is running.

    This requirement came into my mind while using the extract method.
    Actually, I wanted to date of manipulation of the side back end.
    Let's say the time is 04:12:2012 03:05:16.
    Now, if I add 100 minutes, the result will be.
    04:12:2012 04:45:16.

    It's the requirement. Maybe there may be best way to do
    but it's the way I think now.

    Thank you.

    It's something that you don't need to know:
    you have a string containing a date or timestamp, and then he must know the format of this string and not the fornat date used by the server.
    or you have a date or timestamp, and then you can add minutes to it without knowing the format as described before.

  • How to get the data from more than 100 domains in bulk API V2.0?

    Hi all

    I try to get data from Eloqua by APIs in bulk because of big data.

    But my Contact 186 fields (more than the majority of export limitation 100). I think I need to get all the data by 2 exports.

    How could I corresponds to 2 parts of a line and join together?

    I'm afraid that any change of data between 2 relative to exports 2 synchronizations would make different order.

    FOR EXAMPLE:

    1. any document is deleted or modified (if it matches do not filter) after obtaining data of the first part and before getting the second part, then everyone behind it would have back in part result.

    2. the data in some fields (included in both parts) are changed between the 2 synchronizations, then the values of the second part are more recent but the values of the first part are old.

    All suggestions should.

    Thank you

    Biao

    bhuang -

    I don't know that you ever go to work around the fact that things will change in your database while you are synchronizing the data. You have to have a way to create exceptions on the side of the synchronization.

    If I pushed Eloqua data to a different database and had to contend with the problem of matches change while I'm syncing, I would create a few additional columns in my database to track the status of synchronization for this folder. Or create another small table to track the data map. Here's how I'd do.

    1. I would have two additional columns: 'mapped fields 1' and '2 fields' mapped. They would be all two datetime fields.
    2. I would do only one set of synchronization both. First of all, synchronize all records for email + 99 fields. Do the entire list. For each batch, the datetime value of the lot in 'mapped fields 1' column.
    3. I would then synchronize all folders of email + other 86 fields. Repeat the entire list. For this batch of the datetime value of each batch in their 'mapped the 2 fields' column to now().
    4. For all records that had only 'mapped fields filled, 1' but' fields mapped 2' was empty, I would be re - run the second query Eloqua API using e-mail as the search value. If no results were returned, I would remove the line. Otherwise, update and the value 'mapped fields in 2' now
    5. For all the records that were only "fields mapped 2', I re - run against the first email query API Eloqua, fill in the missing data and define 'mapped the fields of 1' of the current datetime object." If the record has not returned, remove the line because it is probably not in the search longer.
    6. Finally, the value 'mapped fields 1' and 'mapped 2 fields' empty for all records, since you know that data is synchronized. This will allow you to use the same logic above on your next synchronization.

    Who is? It is not super clean, but it will do the job, unless your synchronizations take a ridiculous amount of time and your great data changes often.

  • How to get the data in the table if the date is less - than two months from the current date?

    Hi all

    I have a requirement in to retrieve table data. If any of the basic date in the column data.

    I have a date field (expiration date) I need to display data which expires two months.

    Please give me a solution.

    Concerning

    Shankar

    Hi, Shankar,

    Sorry, we don't know what you want.

    If you want something which, when executed on March 13, 2014, finds all the lines where expires and is located between March 13, 2014 and may 13, 2014, inclusive,

    SELECT *- or whatever the columns that you want to

    FROM table_x

    WHERE expire > = TRUNC (SYSDATE)

    Maturity AND< add_months="" (trunc="" (sysdate)="" ,="" 2)="" +="">

    ;

    I hope that answers your question.

    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also publish outcomes from these data.

    Explain, using specific examples, how you get these results from these data.

    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002

Maybe you are looking for