473,414 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Obtaining a Column Which has MAX in Another

Good Day All,

I am using Oracle 7.3. Using the following table definition and sample
data, how can I obtain the PayID which has the maximum PayDate for each
AccountID?

table Payments
==============
PayID (key)
AccountID (number)
PayDate (date)

Payments
PayID AccountID PayDate
================================
1 100 2/7/2005
2 100 5/16/2005
3 213 10/12/2006
4 100 2/17/2005
5 213 9/22/2005

So I'm expecting to retrieve PayId's 2 and 3.

For what it's worth I was born and raised on Access and have worked out
the solution using Access SQL. However it employs the LAST() function,
which is not available to me in O7.3.

In theory I could use a linked table in Access to do this, but in my
real life application I have found this to be entirely to slow.

Thanks in advance.
--
Smartin
Apr 29 '06 #1
4 4777
Smartin wrote:
Good Day All,

I am using Oracle 7.3. Using the following table definition and sample
data, how can I obtain the PayID which has the maximum PayDate for each
AccountID?

table Payments
==============
PayID (key)
AccountID (number)
PayDate (date)

Payments
PayID AccountID PayDate
================================
1 100 2/7/2005
2 100 5/16/2005
3 213 10/12/2006
4 100 2/17/2005
5 213 9/22/2005

So I'm expecting to retrieve PayId's 2 and 3.

For what it's worth I was born and raised on Access and have worked out
the solution using Access SQL. However it employs the LAST() function,
which is not available to me in O7.3.

In theory I could use a linked table in Access to do this, but in my
real life application I have found this to be entirely to slow.

Thanks in advance.


I see this is quite the stumper... not because no one has replied to me,
but because I see the question has been posed several times on Usenet
over the years and no one (that I have seen so far) has received a
solution.

I should revise my question to say I would like to know if there is a
solution in ANSI SQL. Surely someone has done this?

Anyway, thanks for your time.

--
Smartin
May 1 '06 #2
"Smartin" <sm********@yahoo.com> wrote in message
news:44**************@yahoo.com...
Smartin wrote:
Good Day All,

I am using Oracle 7.3. Using the following table definition and sample
data, how can I obtain the PayID which has the maximum PayDate for each
AccountID?

table Payments
==============
PayID (key)
AccountID (number)
PayDate (date)

Payments
PayID AccountID PayDate
================================
1 100 2/7/2005
2 100 5/16/2005
3 213 10/12/2006
4 100 2/17/2005
5 213 9/22/2005

So I'm expecting to retrieve PayId's 2 and 3.

For what it's worth I was born and raised on Access and have worked out
the solution using Access SQL. However it employs the LAST() function,
which is not available to me in O7.3.

In theory I could use a linked table in Access to do this, but in my real
life application I have found this to be entirely to slow.

Thanks in advance.


I see this is quite the stumper... not because no one has replied to me,
but because I see the question has been posed several times on Usenet over
the years and no one (that I have seen so far) has received a solution.

I should revise my question to say I would like to know if there is a
solution in ANSI SQL. Surely someone has done this?

Anyway, thanks for your time.

--
Smartin


Smartin.

My solution is:
SELECT
accountid,
payid
FROM
payments
WHERE
(accountid, paydate) IN (
SELECT
accountid,
MAX( paydate ) as paydate
FROM
payments
GROUP BY
accountid
)
;

ACCOUNTID PAYID
---------------------- ----------------------
100 2
213 3

2 rows selected

I am not certain that the in-line view works in Oracle 7.3. If not, use a
temporary table to store the results of the innermost query and use another
query to perform an inner join between the temporary table and PAYMENTS.

Douglas Hawthorne
May 2 '06 #3

"Smartin" <sm********@yahoo.com> wrote in message
news:44**************@yahoo.com...
: Smartin wrote:
: > Good Day All,
: >
: > I am using Oracle 7.3. Using the following table definition and sample
: > data, how can I obtain the PayID which has the maximum PayDate for each
: > AccountID?
: >
: > table Payments
: > ==============
: > PayID (key)
: > AccountID (number)
: > PayDate (date)
: >
: > Payments
: > PayID AccountID PayDate
: > ================================
: > 1 100 2/7/2005
: > 2 100 5/16/2005
: > 3 213 10/12/2006
: > 4 100 2/17/2005
: > 5 213 9/22/2005
: >
: > So I'm expecting to retrieve PayId's 2 and 3.
: >
: > For what it's worth I was born and raised on Access and have worked out
: > the solution using Access SQL. However it employs the LAST() function,
: > which is not available to me in O7.3.
: >
: > In theory I could use a linked table in Access to do this, but in my
: > real life application I have found this to be entirely to slow.
: >
: > Thanks in advance.
:
: I see this is quite the stumper... not because no one has replied to me,
: but because I see the question has been posed several times on Usenet
: over the years and no one (that I have seen so far) has received a
: solution.
:
: I should revise my question to say I would like to know if there is a
: solution in ANSI SQL. Surely someone has done this?
:
: Anyway, thanks for your time.
:
: --
: Smartin

Douglas Hawthorne's answer is the one that has been provided for the last 20
years or so -- this is covered in most Intro to SQL classes, and you've
almost described the answer in your description of the problem.

Perhaps your Access SQL background has not included much use of sub-queries
(what Douglas referred to as an in-line view), so the problem may have
seemed tougher than it actually is. But if you think through your
description, and build your SQL piece by piece, it's quite easy to come to
the solution:

: > how can I obtain the PayID which has the maximum PayDate for each
: > AccountID?

what's the first thing you need to determine? maximum PAYDATE, for each
ACCOUNTID -- requires a simple GROUP BY

once you have written the SELECT statement to return your pairs of ACCOUNTID
and PAYDATE values, use it in a straight-forward multi-value subquery to
determine which PAYID records have that set of values, as per Douglas'
solution (which, AFAIR, worked in version 6 of Oracle, perhaps even versions
5 and 4).

++ mcs

May 2 '06 #4
Mark C. Stock wrote:
"Smartin" <sm********@yahoo.com> wrote in message
news:44**************@yahoo.com...
: Smartin wrote:
: > Good Day All,
: >
: > I am using Oracle 7.3. Using the following table definition and sample
: > data, how can I obtain the PayID which has the maximum PayDate for each
: > AccountID?
: >
: > table Payments
: > ==============
: > PayID (key)
: > AccountID (number)
: > PayDate (date)
: >
: > Payments
: > PayID AccountID PayDate
: > ================================
: > 1 100 2/7/2005
: > 2 100 5/16/2005
: > 3 213 10/12/2006
: > 4 100 2/17/2005
: > 5 213 9/22/2005
: >
: > So I'm expecting to retrieve PayId's 2 and 3.
: >
: > For what it's worth I was born and raised on Access and have worked out
: > the solution using Access SQL. However it employs the LAST() function,
: > which is not available to me in O7.3.
: >
: > In theory I could use a linked table in Access to do this, but in my
: > real life application I have found this to be entirely to slow.
: >
: > Thanks in advance.
:
: I see this is quite the stumper... not because no one has replied to me,
: but because I see the question has been posed several times on Usenet
: over the years and no one (that I have seen so far) has received a
: solution.
:
: I should revise my question to say I would like to know if there is a
: solution in ANSI SQL. Surely someone has done this?
:
: Anyway, thanks for your time.
:
: --
: Smartin

Douglas Hawthorne's answer is the one that has been provided for the last 20
years or so -- this is covered in most Intro to SQL classes, and you've
almost described the answer in your description of the problem.

Perhaps your Access SQL background has not included much use of sub-queries
(what Douglas referred to as an in-line view), so the problem may have
seemed tougher than it actually is. But if you think through your
description, and build your SQL piece by piece, it's quite easy to come to
the solution:

: > how can I obtain the PayID which has the maximum PayDate for each
: > AccountID?

what's the first thing you need to determine? maximum PAYDATE, for each
ACCOUNTID -- requires a simple GROUP BY

once you have written the SELECT statement to return your pairs of ACCOUNTID
and PAYDATE values, use it in a straight-forward multi-value subquery to
determine which PAYID records have that set of values, as per Douglas'
solution (which, AFAIR, worked in version 6 of Oracle, perhaps even versions
5 and 4).

++ mcs



The /multi-value/ part of this was eluding me. I see some remedial SQL
training is in my future.

Thanks much to both of you. I'll give it a go!

--
Smartin
May 2 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Roger Withnell | last post by:
I'm inserting a new record into an MS SQL database table and I want to obtain the new records autonumber immediately afterwards, as follows: MadminRS.CursorLocation = adUseServer...
7
by: Privacy Advocate | last post by:
//crossposted to: comp.lang.javascript, alt.comp.lang.javascript in an effort to get factual answers from JavaScript experts// Simply put; Is it possible to obtain the real (actual) IP address of...
5
by: Peter Bailey | last post by:
I have a query that returns , and : 12/05/04 3 Wednesday 13/05/04 0 Thursday and so on what I would like to do now is count the number of bookings by week so from monday to...
0
by: Andrea Trevisan | last post by:
That's a revival of a known thing I suppose.I hope it's useful. My problem was: I want to have a DataGrid with two Template columns: first with TextBox,second with Button.I want to fire an event...
0
by: Fred Nelson | last post by:
I have a stored procedure that returns a one row with large number of columns (350) to a datagrid "datagrid1" on a web form: datagrid1.datasource = getcase.getcaseinfo(case#)...
1
by: UJ | last post by:
We have recently started getting the following error message: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled...
11
by: seannakasone | last post by:
Is there a way to get the callstack level in c++? for example, take the following code: void call3() { //callstack level would be 3 } void call2() { //callstack level would be 2 call3();
1
by: Jeff | last post by:
If I a column name for a datatable in vb.net 2005 and want to know whether the column is a char or int how would I ask? E.g., code starts like this. How do I obtain the type of column? ...
4
by: Smartin | last post by:
Good Day All, I am using Oracle 7.3. Using the following table definition and sample data, how can I obtain the PayID which has the maximum PayDate for each AccountID? table Payments...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.