Needing a maximum date using the group value of

Create table student (dept number(10), dep_name varchar2(10),join_date date,years_attended number(10),end_date date);
 
insert into student values (1,'I',to_date('3/7/1917','MM/DD/YYYY'),4,to_date('8/26/1987','MM/DD/YYYY'));
insert into student values (1,'I',to_date('1/1/1900','MM/DD/YYYY'),4,to_date('8/26/1932','MM/DD/YYYY'));
insert into student values (1,'D',to_date('1/1/1920','MM/DD/YYYY'),5,to_date('8/26/1994','MM/DD/YYYY'));
insert into student values (1,'C',to_date('1/1/1920','MM/DD/YYYY'),6,to_date('8/26/1945','MM/DD/YYYY'));
insert into student values (2,'I',to_date('7/1/1900','MM/DD/YYYY'),3,to_date('8/26/1932','MM/DD/YYYY'));
insert into student values (2,'I',to_date('8/16/1916','MM/DD/YYYY'),9,to_date('8/26/1923','MM/DD/YYYY'));
insert into student values (2,'D',to_date('8/16/1916','MM/DD/YYYY'),10,to_date('8/26/1987','MM/DD/YYYY'));
insert into student values (3,'I',to_date('3/7/1917','MM/DD/YYYY'),4,to_date('8/26/1987','MM/DD/YYYY'));
insert into student values (3,'D',to_date('7/28/1920','MM/DD/YYYY'),6,to_date('8/26/1945','MM/DD/YYYY'));
insert into student values (3,'I',to_date('7/28/1920','MM/DD/YYYY'),8,to_date('8/26/1965','MM/DD/YYYY'));
insert into student values (4,'I',to_date('12/31/1924','MM/DD/YYYY'),2,to_date('8/26/1998','MM/DD/YYYY'));
insert into student values (4,'I',to_date('6/10/1929','MM/DD/YYYY'),1,to_date('8/26/1943','MM/DD/YYYY'));
insert into student values (4,'C',to_date('1/17/1927','MM/DD/YYYY'),4,to_date('8/26/1955','MM/DD/YYYY'));
insert into student values (4,'C',to_date('6/10/1929','MM/DD/YYYY'),30,to_date('8/26/1967','MM/DD/YYYY'));
insert into student values (5,'D',to_date('2/10/1931','MM/DD/YYYY'),2,to_date('8/26/1943','MM/DD/YYYY'));
insert into student values (5,'I',to_date('2/10/1931','MM/DD/YYYY'),24,to_date('8/26/1962','MM/DD/YYYY'));
commit;
I need a join_date of date value maximum for each Department. If max (join_date) has two records for each dept max (end_date) are to be considered. I used a select query
select * from student where join_date in (select 
max(join_date) from student group by dept);
gives me the following result
1     D     1/1/1920     5     8/26/1994
1     C     1/1/1920     6     8/26/1945
2     I     8/16/1916     9     8/26/1923
2     D     8/16/1916     10     8/26/1987
3     D     7/28/1920     6     8/26/1945
3     I     7/28/1920     8     8/26/1965
4     I     6/10/1929     1     8/26/1943
4     C     6/10/1929     30     8/26/1967
5     D     2/10/1931     2     8/26/1943
5     I     2/10/1931     24     8/26/1962
But I'm looking for the result that gives me only a maximum value for each column dept. First of all, it should look like to a maximum value of join_date, so two records even join_date max (end_date) are to be considered. The result should be sumthing like this
1     D     1/1/1920     5     8/26/1994
2     D     8/16/1916     10     8/26/1987
3     I     7/28/1920     8     8/26/1965
4     C     6/10/1929     30     8/26/1967
5     I     2/10/1931     24     8/26/1962
Can you please tell me how to rewrite the select query for results above.

Published by: user11872870 on August 2, 2011 17:29

Published by: user11872870 on August 2, 2011 17:36

Hello

This is called a Query Top - N , and this is a way to do it:

WITH     got_r_num     AS
(
     SELECT     student.*
     ,     ROW_NUMBER () OVER ( PARTITION BY  dept
                               ORDER BY          join_date     DESC
                         ,                end_date     DESC
                       )      AS r_num
     FROM    student
)
SELECT       dept, dep_name, join_date, years_attended, end_date
FROM       got_r_num
WHERE       r_num     = 1
ORDER BY  dept
;

Another way is similar to what you posted:

SELECT    *
FROM       student
WHERE        (dept, join_date, end_date)
               IN (
                    SELECT    dept
               ,       MAX (join_date)
               ,       MAX (end_date) KEEP (DENSE_RANK LAST ORDER BY join_date)
               FROM      student
               GROUP BY     dept
               );

I suspect that the first way (using ROW_NUMBER) will be faster.
In addition, the ROW_NUMBER approach is guaranteed to return only 1 line by Department using the approach of GROUP BY, if there is a link on join_date and end_date then it will return all the contenders in this Department. using ROW_NUMBER, it is easy to add expressions to tie-break as much as you want, and, if there is still a tie, it will be arbirarily pick, one lines involved in the tie as #1.

Thanks for posting the CREATE TABLE and INSERT! It is very useful.

Published by: Frank Kulash, August 2, 2011 21:00
Added GROUP BY alternative

Tags: Database

Similar Questions

  • How to change the value of string clustered, to implement using the node value of property instead of writing directly on the flow of data or using the variable

    new to labview :-) and I have a problem when I want to change the value of a string in bunches, and I want to implement this using the node value of property instead of writing directly to the stream or by using the variable, enclosed is the picture.   No matter, I have change in cluster (control) or value of Popery out (indicator) cluster, the value (sensor 7) dataflow keeps unchanged even I gave the new value by value of property node. Thank you to give me some advice about this.

    Hi GerdW

    Thanks a lot for your answer. The reason I'm stubbornly tring to break the flow of DATA is: we have a test system that have about 100 screws, they have a few connected flow, some of them will be unbundling a cluster dataflow chain to check the value in order to make the different cases.  Now I want to insert user event by changing the control and influential cases during run time.

    As I initially uses a global variable (to control cases) instead of unbundle string data flow, it works well.  But then, I found there are a lot of screws that are using the string unbundle.  One of the 'lazy' means, I tried is to change the value via the property node (because that way, I did not need to find all the places where using the unbundle string and replace them with the global variable), then I noticed a problem with "dataflow", the value in the stream of cluster in fact will not be changed by changing the value of the property node.

    I did a test with VI simple (like the picture in last post), and after reading your advice, I tell myself that I need to understand the concept of "DATAFLOW" in labview, it seems that my "lazy" way can not work in this scenario.

    I have attached the criterion VI here, have you furthur suggestions on what I can do in this case?

    Mant thanks!

    Minyi

  • Re-sample a signal for the least points of data using the value average

    Hello

    I'm trying to resample a large wave to a smaller wave form. I want to use the average value of all data points in a time interval of the initial wave as the new value of the new waveform. For example I have want to resample a waveform of 100 points from k to a waveform with 1 k points, in the waveform again the value of each data point is the average of 100 data points in the old waveform. Is a convenient way to do this in labview? The VI of waveforms resample seems only provide coerce/linear/spline/FIR interpolation modes. Thank you very much for your help.

    Just use build a waveform is your decimated table, dt is climbed by the decimation factor and t0 can remain the same.

  • How to use the Group feature in insert or update

    Hai All

    How to use the Group feature in insert or update statement

    I'm generating attendance so I have a different set of timing example

    0800,1200,1230, 1700 and I need to insert data into the table that contains the intimate value min and max value for

    outtime and othere to inertval time in or out

    Pls tell me with some examples


    For example

    For INSERT

    Insert into T2 (barcode, empcode, respondent, attend_date)
    values (R2.cardn, R2.enpno, min(R2.) PtIMe), R2.pdate);


    Update

    Update dail_att set outtime = MAX (r2.ptime) where empcode = r2.enpno and barcode = r2.cardn and
    attend_date = r2.pdate;


    Here, in the place where I used to have so pls tell how to use

    Thanks and greetings

    Srikkanth.M

    Hi Srikkanth.M

    to insert:

    insert into test (dummy) values ((select max (dummy) to double));

    Update:

    Update test dummy value = (select max (dummy) to double where to test them. XXXXField = double. XXXXField);

  • Error when you use the Group feature

    Oracle forms6i

    Hai

    While I compile my coding it will compile correctly, but when I tried to run I revealed an error in the function of group

    my code is


    If (NTC <>0) then
    Select the BAR code, RESPONDENT, OUTTIME today_bar, today_in, today_out from dail_att where BARCODE =: bar code
    and ATTEND_DATE =: bardate;
    Update dail_att set outtime = max(:bartime) where barcode =: bar code
    and ATTEND_DATE =: bardate;

    on the other
    If (cnt2 <>0) then

    Select RESPONDENT, OUTTIME in yest_in, yest_out from dail_att where BARCODE =: bar code
    and ATTEND_DATE =: bardate-1;

    If (yest_in is not null and yest_out is null) then
    Update dail_att set outtime = max(:bartime) where barcode =: bar code
    and ATTEND_DATE =: bardate-1;

    on the other
    insert into dail_att(barcode,intime,attend_date)
    values (: Barcode, min(:Bartime),: bardate);
    end if;
    on the other
    If: bartime between 0100 and 0630
    insert into dail_att(barcode,intime,attend_date)
    values (: Barcode, min(:Bartime),: bardate-1);

    Update dail_att set outtime = max(:bartime) where barcode =: bar code
    and ATTEND_DATE =: bardate-1;

    on the other

    insert into dail_att(barcode,intime,attend_date)
    values (: Barcode,: min (Bartime),: bardate);
    end if;
    end if;
    end if;

    while I'm trying this groupfunction it throws error while I use seen tell me how to use the Group feature and where
    to use

    Regadrs

    Srikkanth.M

    Srikkanth,

    For this you need to calculate the max outside of the update statement and then use this value in the update statement.

    The bartime is a control in the block?

    If Yes, then you need to find the max manually, and if it isn't, then you can write a sub query to find the maximum value.

    Kind regards

    Manu.

    If my response or response from another person was helpful, please mark accordingly

  • Examples on how to use the external value of the vro

    I'm trying to use the 'external value' VRA 7 feature to get data for a property definition.  Examples or guides?   I have a quick action that returns the string, but I get this error

    externalvalue.JPG

    Figured it out... the exit on the action must be array/string...

    now working to reproduce the relantionships parent on custom properties that not available in version 7

  • Is it possible to use the default value as sql expression in obiee 11g report?

    Hi all

    Is it possible to use the default value as sql expression in obiee 11g report?. Actually what I'm trying to make is that we send an ibot users. They want the same default values that are assigned in the command prompt in the report. So when we send the ibot it shows the report for default values assigned in the report criteria which is obsolute. As a solution, I kept the report in a dashboard page and send the dashboard like ibot page. But the problem here is that I can send only pdf format. Is it possible to do so at the level of the report (without using session variables). I want something like that

    Date between Date ' @{PV1} {Timestampadd (SQL_TSI_DAY,-90, CURRENT_DATE)} and Date ' @{PV2} {CURRENT_DATE}

    Thanks in advance

    Thank you

    AJ

    http://www.w3.org/2001/XMLSchema-instance"container ="http://www.w3.org/2001/XMLSchema"xmlVersion ="201201160' xmlns:sawx="com.siebel.analytics.web/expression/v1.1" > "

    "Question time '." Date '.

    "Question time '." Date '.

    BOX WHEN (DAYOFWEEK (cast (max (CURRENT_DATE) date)) = 1) THEN TIMESTAMPADD (SQL_TSI_DAY,-2, cast (max (CURRENT_DATE) as date)) WHEN (DAYOFWEEK (cast (max (CURRENT_DATE) date)) = 2) THEN TIMESTAMPADD (SQL_TSI_DAY,-3, cast (max (CURRENT_DATE) as date)) WHEN (DAYOFWEEK (cast (max (CURRENT_DATE) date)) = 3) THEN TIMESTAMPADD (SQL_TSI_DAY,-4, cast (max (CURRENT_DATE) as date)) WHEN (DAYOFWEEK (cast (max (CURRENT_DATE) date)) = 4) THEN TIMESTAMPADD (SQL_TSI_DAY-5 (, cast (max (CURRENT_DATE) date)) WHEN (DAYOFWEEK (cast (max (CURRENT_DATE) date)) = 5) THEN TIMESTAMPADD (SQL_TSI_DAY,-6, cast (max (CURRENT_DATE) as date)) WHEN (DAYOFWEEK (cast (max (CURRENT_DATE) date)) = 6) THEN TIMESTAMPADD (SQL_TSI_DAY, 0, cast (max (CURRENT_DATE) as date)) WHEN (DAYOFWEEK (cast (max (CURRENT_DATE) date)) = 7) THEN TIMESTAMPADD (SQL_TSI_DAY-1, cast (max (CURRENT_DATE) date)) END

  • ORA-03297: file contains data beyond the requested value of RESIZING

    Hi all
    I am trying to increase the size of my space of system tables. The tablespace contains a data size 900MB file and it may increase to 2 GB.
    I would like to change this to autoextend I'll operate on the database csscan.
    When to change the autoextend and unlimited file I get: ORA-03297: file contains data beyond the requested value of RESIZING

    I also tried to increase the data file of 4 GB, but still the same. I see that there is more then enough space on the file system.
    Need help.
    Oracle Enterprise edt 10.2.0.5
    Concerning
    933746

    The first statement is that originally the problem - you try to shrink the file, develops it not.

    The autoextend (second statement) should be fine as is.

  • Use the return value to do step digital multiple success or failure

    Hello

    I use a multiple digital step to check several measurements by an external DMM through a function created in CVI.  I would like to use the return value of the function to replace the global status of the step. The reason is that the return value indicates a communication error, while steps are being taken, and which would lead to a "fake" fake or false 'Pass '.

    What I would do is more or less this >

    If the return value is 0, then check if the measuremets are between the limits

    If return value! 0 = there was a communication error so step continues to operate independently of the measure.

    I tried combining multiple digital step with a step of pass/fail, but it does not work.

    I also trie to change the expression of the status of the multiple digital steps but basically ignores him.

    Any ideas?

    Thank you

    Javier

    Why don't you just make that return the value limits for multiple numerical limit stage.  Comparison of the EQ value and the low limit to 0.

    That would make the most sense.

  • kindly tell how to use the unique value of a table with the index 0

    kindly tell how to use the unique value of a table with the index 0

    Hi
     
    Yep, use Index Array as Gerd says. Also, using the context help ( + h) and looking through the array palette will help you get an understanding of what each VI does.
     
    This is fundamental LabVIEW stuff, perhaps you'd be better spending some time going through the basics.
     
    -CC
  • Receiving data using the device RFCOMM Protocol activate bluetooth

    Hi all

    I want to apply that receive the data of bluetooth device using the RFCOMM Protocol.

    I'm pasting some of my code

    Server StreamConnectionNotifier = null, Server1 = null, server2 = null;

    Hi all

    Can some body having any idea or sample code to receive data using the for Bluetooth RFCOMM Protocol. I am able to find a device to activate bluetooth and also connect with it.

    And ther eis a problem plus I only magnified device does not form some sort of link (link Url SPP, OPP Url link, etc.) for bluetooth.

    But I can verify that this device is correct because I am able to receive data from this device to my computer by checking the connection port and to open this port.

  • using the group name and password group in client anyconnect

    Hello. Is it possible to use the group name/password of the legacy in customer cisco anyconnect vpn client? I checked the AnyConnect Administrator's Guide ' VPN XML Reference"and found nothing on this subject.

    It's true.

    AnyConnect Secure Mobility Client (VPN Module) can be used to connect to both types of VPN remote access:

    1. full SSL VPN tunnel

    2 IKEv2 VPN IPsec.

    The legacy VPN client is used only with the old IKEv1 IPsec VPN and you cannot use this type of VPN client AnyConnect.

  • The number of characters is allowed when we use the ' have value in ' option when you create a filter?

    The number of characters is allowed when you use the option "value in" when you create a filter?

    Hey,.

    300 characters are allowed when we use the option "value in" everything by creating a filter.

    Thank you

  • Application of interactive report of apEx using the last value of the point

    My interactive report is based on a field, but the value of the field is not applied when executing the query

    SELECT *.

    MyTable FROM A

    where A.registration_year =: REGISTRATION_YEAR_ITEM

    I created an item in the page REGISTRATION_YEAR_ITEM

    When I run the query, I want the user to type in a year, and then run the query and display the corresponding results.

    The query always seems to use the default value of points instead of the current value

    How can I do use the value of the current field?

    I also tried the V method, but produce the same results

    where A.registration_year = v('REGISTRATION_YEAR_ITEM')


    The environment is ApEx 312, 10.2.0.4 database

    Unfortunately, the upgrade is not an option


    Thanks for any help you can offer

    S.Bovin wrote:

    My interactive report is based on a field, but the value of the field is not applied when executing the query

    SELECT *.

    MyTable FROM A

    where A.registration_year =: REGISTRATION_YEAR_ITEM

    I created an item in the page REGISTRATION_YEAR_ITEM

    When I run the query, I want the user to type in a year, and then run the query and display the corresponding results.

    The query always seems to use the default value of points instead of the current value

    How can I do use the value of the current field?

    Add the REGISTRATION_YEAR_ITEM to the IR Page elements to submit property. He will submit the current value in session state.

    Why bother with the page element at all? Why users can't create just an IR filter?

    I also tried the V method, but produce the same results

    where A.registration_year = v('REGISTRATION_YEAR_ITEM')

    For performance reasons, use bind variable notation when you reference values from session state in SQL queries.

    The environment is ApEx 312, 10.2.0.4 database

    Unfortunately, the upgrade is not an option

    Why not? That it will provide you with a safe and supported environment and your users with a much better life.

  • Issue while loading data using the file Rules Essbase

    Hi all

    I am facing problem while loading data using the Rules file. In the rules file, I rejected several members in two areas (two dimensions). Now if I load the data using the rules file I'm getting errors for all members in the dataload.err file. If I reject mutiple members of a single field, the data load without settling errors in the dataload.err file.

    I want to know how rmany members of several fields of ejection for loading data using the file Rules Essbase? Is it possible?

    Okay, okay... I think that you must assign Global Select / reject Boolean in the parameters of loading data as 'Or':

Maybe you are looking for

  • A57 abandoned by Sony - Implications for Future Support

    When a Sony camera, in my case the a57, is interrupted, Sony just drop all support (firmware update, etc.) or the company is at least continue to support 'x' amount of time?

  • Windows in reverse

    Hi, I pressed some keys by accident and all my windows are backwards! Any idea which keys would return my normal windows?

  • My printer is turned on if all said that it is not.

    HP Deskjet 3050 a J611 series Windows XP I receive the following message if poster every time I try to print. I have tried the reset of the printer, turn power on and off, checked the connections network etc etc etc. In troubleshooting, it says that

  • All files open in Internet Explorer, how do I fix?

    I open my file using bluetooth internet explore now open all files in the Solution Explorer, what do I do to restore the previous path to open software * original title-i opened my file using bluetooth internet explore now all open files from Explore

  • InDesign CC

    I started a file in Adobe Comp and want to stop working on it in ID cc on my laptop.  Please explain how I can do this.  Thank you!