dbTalk Databases Forums  

finding last date

comp.databases.oracle.server comp.databases.oracle.server


Discuss finding last date in the comp.databases.oracle.server forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Robert Hicks
 
Posts: n/a

Default finding last date - 05-23-2007 , 01:14 PM






I have a table (simplified):

NAM E VOY DATE


The NAME column can have the same entry up to 10 times based on the
VOY. So it could look something like this:

NAME VOY DATE
123456 0 <some date>
123456 1 <some date>
345677 0 <some date>
345677 1 <some date>
345677 2 <some date>
345677 3 <some date>
098766 0 <some date>

Some of the NAME items have VOYS up to 9 but not all of them do.

I need to pull out the NAME and the LAST <some date> for each
"distinct" NAME. So in the above case I would get:

123456 1 <some date>
345677 3 <some date>
098766 0 <some date>

The <some date> is always later as the VOY increments up.

Any help would be appreciated. I have been banging my head all day.

Robert


Reply With Quote
  #2  
Old   
Robert Hicks
 
Posts: n/a

Default Re: finding last date - 05-23-2007 , 01:23 PM






On May 23, 2:14 pm, Robert Hicks <sigz... (AT) gmail (DOT) com> wrote:
Quote:
I have a table (simplified):

NAM E VOY DATE

The NAME column can have the same entry up to 10 times based on the
VOY. So it could look something like this:

NAME VOY DATE
123456 0 <some date
123456 1 <some date
345677 0 <some date
345677 1 <some date
345677 2 <some date
345677 3 <some date
098766 0 <some date

Some of the NAME items have VOYS up to 9 but not all of them do.

I need to pull out the NAME and the LAST <some date> for each
"distinct" NAME. So in the above case I would get:

123456 1 <some date
345677 3 <some date
098766 0 <some date

The <some date> is always later as the VOY increments up.

Any help would be appreciated. I have been banging my head all day.

Robert
If this is the wrong usenet group let me know. I noticed the "server"
and that might mean admin and not sql stuff.

Robert



Reply With Quote
  #3  
Old   
ErikYkema
 
Posts: n/a

Default Re: finding last date - 05-23-2007 , 01:27 PM



On May 23, 8:14 pm, Robert Hicks <sigz... (AT) gmail (DOT) com> wrote:
Quote:
I have a table (simplified):

NAM E VOY DATE

The NAME column can have the same entry up to 10 times based on the
VOY. So it could look something like this:

NAME VOY DATE
123456 0 <some date
123456 1 <some date
345677 0 <some date
345677 1 <some date
345677 2 <some date
345677 3 <some date
098766 0 <some date

Some of the NAME items have VOYS up to 9 but not all of them do.

I need to pull out the NAME and the LAST <some date> for each
"distinct" NAME. So in the above case I would get:

123456 1 <some date
345677 3 <some date
098766 0 <some date

The <some date> is always later as the VOY increments up.

Any help would be appreciated. I have been banging my head all day.

Robert
Hi Robert,
What about

select name
, max(date) as max_date
from table_name_voy_date
group by name

another one could be:

select t2.name, t2.date
from (select name, max(voy) as max_voy from table_name_voy_date group
by name) t1
join table_name_voy_date t2
on t1.name = t2.name
and t1.max_voy = t2.voy

Please be aware that date is a reserved oracle word, so better name it
something different.

If this helps, I recommend you buy a good book on basic SQL. If not,
give us some more clues about what the challenge is.
Regards,
Erik Ykema



Reply With Quote
  #4  
Old   
ErikYkema
 
Posts: n/a

Default Re: finding last date - 05-23-2007 , 01:27 PM



On May 23, 8:14 pm, Robert Hicks <sigz... (AT) gmail (DOT) com> wrote:
Quote:
I have a table (simplified):

NAM E VOY DATE

The NAME column can have the same entry up to 10 times based on the
VOY. So it could look something like this:

NAME VOY DATE
123456 0 <some date
123456 1 <some date
345677 0 <some date
345677 1 <some date
345677 2 <some date
345677 3 <some date
098766 0 <some date

Some of the NAME items have VOYS up to 9 but not all of them do.

I need to pull out the NAME and the LAST <some date> for each
"distinct" NAME. So in the above case I would get:

123456 1 <some date
345677 3 <some date
098766 0 <some date

The <some date> is always later as the VOY increments up.

Any help would be appreciated. I have been banging my head all day.

Robert
Hi Robert,
What about

select name
, max(date) as max_date
from table_name_voy_date
group by name

another one could be:

select t2.name, t2.date
from (select name, max(voy) as max_voy from table_name_voy_date group
by name) t1
join table_name_voy_date t2
on t1.name = t2.name
and t1.max_voy = t2.voy

Please be aware that date is a reserved oracle word, so better name it
something different.

If this helps, I recommend you buy a good book on basic SQL. If not,
give us some more clues about what the challenge is.
Regards,
Erik Ykema



Reply With Quote
  #5  
Old   
fitzjarrell@cox.net
 
Posts: n/a

Default Re: finding last date - 05-23-2007 , 01:28 PM



On May 23, 1:14 pm, Robert Hicks <sigz... (AT) gmail (DOT) com> wrote:
Quote:
I have a table (simplified):

NAM E VOY DATE

The NAME column can have the same entry up to 10 times based on the
VOY. So it could look something like this:

NAME VOY DATE
123456 0 <some date
123456 1 <some date
345677 0 <some date
345677 1 <some date
345677 2 <some date
345677 3 <some date
098766 0 <some date

Some of the NAME items have VOYS up to 9 but not all of them do.

I need to pull out the NAME and the LAST <some date> for each
"distinct" NAME. So in the above case I would get:

123456 1 <some date
345677 3 <some date
098766 0 <some date

The <some date> is always later as the VOY increments up.

Any help would be appreciated. I have been banging my head all day.

Robert
First if DATE is an actual column name you need to change it since
DATE is a datatype and a reserved word in Oracle. Second, if you
can't use GROUP BY to get these results you should probably take a
refresher course in SQL. This is a simple query to write:

select name, voy, mydate
from mytable
where (name, mydate) in (select name, max(mydate) from mytable group
by name);

I presume since your example is oversimplified the query I just
supplied won't work without severe modification. Such is the price
you pay for not providing accurate details regarding what you are
trying to do.


David Fitzjarrell



Reply With Quote
  #6  
Old   
ErikYkema
 
Posts: n/a

Default Re: finding last date - 05-23-2007 , 01:31 PM



On May 23, 8:14 pm, Robert Hicks <sigz... (AT) gmail (DOT) com> wrote:
Quote:
I have a table (simplified):

NAM E VOY DATE

The NAME column can have the same entry up to 10 times based on the
VOY. So it could look something like this:

NAME VOY DATE
123456 0 <some date
123456 1 <some date
345677 0 <some date
345677 1 <some date
345677 2 <some date
345677 3 <some date
098766 0 <some date

Some of the NAME items have VOYS up to 9 but not all of them do.

I need to pull out the NAME and the LAST <some date> for each
"distinct" NAME. So in the above case I would get:

123456 1 <some date
345677 3 <some date
098766 0 <some date

The <some date> is always later as the VOY increments up.

Any help would be appreciated. I have been banging my head all day.

Robert
On May 23, 8:14 pm, Robert Hicks <sigz... (AT) gmail (DOT) com> wrote:
Quote:
I have a table (simplified):

NAM E VOY DATE

The NAME column can have the same entry up to 10 times based on the
VOY. So it could look something like this:

NAME VOY DATE
123456 0 <some date
123456 1 <some date
345677 0 <some date
345677 1 <some date
345677 2 <some date
345677 3 <some date
098766 0 <some date

Some of the NAME items have VOYS up to 9 but not all of them do.

I need to pull out the NAME and the LAST <some date> for each
"distinct" NAME. So in the above case I would get:

123456 1 <some date
345677 3 <some date
098766 0 <some date

The <some date> is always later as the VOY increments up.

Any help would be appreciated. I have been banging my head all day.

Robert
Hi Robert,
What about

select name
, max(date) as max_date
from table_name_voy_date
group by name

another one could be:

select t2.name, t2.date
from (select name, max(voy) as max_voy from table_name_voy_date group
by name) t1
join table_name_voy_date t2
on t1.name = t2.name
and t1.max_voy = t2.voy

Please be aware that date is a reserved oracle word, so better name it
something different.

If this helps, I recommend you buy a good book on basic SQL. If not,
give us some more clues about what the challenge is.
Regards,
Erik Ykema



Reply With Quote
  #7  
Old   
sybrandb@hccnet.nl
 
Posts: n/a

Default Re: finding last date - 05-23-2007 , 01:46 PM



On 23 May 2007 11:14:57 -0700, Robert Hicks <sigzero (AT) gmail (DOT) com> wrote:

Quote:
I have a table (simplified):

NAM E VOY DATE


The NAME column can have the same entry up to 10 times based on the
VOY. So it could look something like this:

NAME VOY DATE
123456 0 <some date
123456 1 <some date
345677 0 <some date
345677 1 <some date
345677 2 <some date
345677 3 <some date
098766 0 <some date

Some of the NAME items have VOYS up to 9 but not all of them do.

I need to pull out the NAME and the LAST <some date> for each
"distinct" NAME. So in the above case I would get:

123456 1 <some date
345677 3 <some date
098766 0 <some date

The <some date> is always later as the VOY increments up.

Any help would be appreciated. I have been banging my head all day.

Robert
select name, voy,date
from foo f1
where date =
(select max(date)
from foo f2
where f2.name = f1.name
)

You don't want to know how often this question have been asked.
Banging should be replaced by a compusory search of the archives.

--
Sybrand Bakker
Senior Oracle DBA


Reply With Quote
  #8  
Old   
Mladen Gogala
 
Posts: n/a

Default Re: finding last date - 05-23-2007 , 01:47 PM



On Wed, 23 May 2007 11:14:57 -0700, Robert Hicks wrote:

Quote:
I have a table (simplified):

NAM E VOY DATE


The NAME column can have the same entry up to 10 times based on the VOY.
So it could look something like this:

NAME VOY DATE
123456 0 <some date
123456 1 <some date
345677 0 <some date
345677 1 <some date
345677 2 <some date
345677 3 <some date
098766 0 <some date

Some of the NAME items have VOYS up to 9 but not all of them do.

I need to pull out the NAME and the LAST <some date> for each "distinct"
NAME. So in the above case I would get:

123456 1 <some date
345677 3 <some date
098766 0 <some date

The <some date> is always later as the VOY increments up.

Any help would be appreciated. I have been banging my head all day.

Robert
WITH MISHMASH AS (
SELECT NAME,VOY,SOME_DATE,
MAX(SOME_DATE) OVER ( PARTITION BY NAME) AS LAST_DATE
FROM TABLE )
select NAME,VOY,SOME_DATE
from MISHMASH
where SOME_DATE=LAST_DATE;

If the "VOY" column is not required, then the query is much
simpler:

select name,max(some_date)
from table
group by name;


This is really elementary SQL. Please, do us all a favor
and sto using this group as a free CBT. It's annoying as
heck and denigrating to the people like me who did invest
the time and effort to learn things.


--
http://www.mladen-gogala.com


Reply With Quote
  #9  
Old   
Frank van Bortel
 
Posts: n/a

Default Re: finding last date - 05-23-2007 , 01:49 PM



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

fitzjarrell (AT) cox (DOT) net wrote:
Such is the price
Quote:
you pay for not providing accurate details regarding what you are
trying to do.

Homework?

- --
Regards,
Frank van Bortel

Top-posting is one way to shut me up...
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (MingW32)

iD8DBQFGVIydLw8L4IAs830RAtRyAJ9W0zN79YKLXsiIMhBQwP qvo0DayACeKfgr
CrBJwoWmU88XhjmI7NdaaLM=
=AYxg
-----END PGP SIGNATURE-----


Reply With Quote
  #10  
Old   
Robert Hicks
 
Posts: n/a

Default Re: finding last date - 05-23-2007 , 02:11 PM



On May 23, 2:28 pm, "fitzjarr... (AT) cox (DOT) net" <fitzjarr... (AT) cox (DOT) net> wrote:
Quote:
On May 23, 1:14 pm, Robert Hicks <sigz... (AT) gmail (DOT) com> wrote:



I have a table (simplified):

NAM E VOY DATE

The NAME column can have the same entry up to 10 times based on the
VOY. So it could look something like this:

NAME VOY DATE
123456 0 <some date
123456 1 <some date
345677 0 <some date
345677 1 <some date
345677 2 <some date
345677 3 <some date
098766 0 <some date

Some of the NAME items have VOYS up to 9 but not all of them do.

I need to pull out the NAME and the LAST <some date> for each
"distinct" NAME. So in the above case I would get:

123456 1 <some date
345677 3 <some date
098766 0 <some date

The <some date> is always later as the VOY increments up.

Any help would be appreciated. I have been banging my head all day.

Robert

First if DATE is an actual column name you need to change it since
DATE is a datatype and a reserved word in Oracle. Second, if you
can't use GROUP BY to get these results you should probably take a
refresher course in SQL. This is a simple query to write:

select name, voy, mydate
from mytable
where (name, mydate) in (select name, max(mydate) from mytable group
by name);

I presume since your example is oversimplified the query I just
supplied won't work without severe modification. Such is the price
you pay for not providing accurate details regarding what you are
trying to do.

David Fitzjarrell
Those are simplified column names is all and it isn't actually DATE. I
was just trying to simplify the explanation. The column names are
actually LLY_NUM, VOY_NUM, TP_DATE.

I will try what you have.

Robert



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.