473,386 Members | 1,779 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,386 software developers and data experts.

SQL - Oracle (Get second highest date by group)

Hi All,

Currently, I have a table with 2 columns COMPONENTID and COMPLETIONDATE

COMPONENTID COMPLETIONDATE
CBT_HAZCOM 02/26/2007 15:17:00
CBT_HAZCOM 07/31/2007 21:23:00
QS 02/12/2007 13:51:00
SHEN158 02/12/2007 13:29:00

What I want to do from this table is grab the second latest date under the completiondate column ID grouped by the COMPONENTID. If there is only 1 entry for COMPONENTID (like for QS and SHEN158), it should keep the current COMPLETIONDATE

The resulting output should be:

COMPONENTID COMPLETIONDATE
CBT_HAZCOM 02/26/2007 15:17:00
QS 02/12/2007 13:51:00
SHEN158 02/12/2007 13:29:00

NOTE: CBT_HAZCOM with COMPLETIONDATE removed 07/31/2007 because it was the latest date in the CBT_HAZCOM grouping. The second latestdate is 02/26/2007.

Please help as this is critical in what I am trying to accomplish!

Sincerely,

Piyush A.
Aug 4 '07 #1
2 5718
debasisdas
8,127 Expert 4TB
Can you kindly post what / how you have tried to solve this problem.
Aug 6 '07 #2
Here is the SQL code I used to solve the problem. This only gives the MAX date and not the second max date. I have no clue as to how to capture the second max date.

select A.componentid, A.completiondate
from
(
select pa_cpnt_evthst.cpnt_id as componentid,MAX (pa_cpnt_evthst.compl_dte) as completiondate
FROM pa_cpnt_evthst, pa_cpnt
WHERE pa_cpnt_evthst.cpnt_id = pa_cpnt.cpnt_id
AND pa_cpnt_evthst.stud_id = '10203996'
AND pa_cpnt.notactive = 'N'
AND pa_cpnt.retrng_int IS NOT NULL
AND pa_cpnt.retrng_int <> 0
AND ((pa_cpnt_evthst.cpnt_typ_id = 'CBT'AND pa_cpnt_evthst.cmpl_stat_id IN ('09', '10'))OR (pa_cpnt_evthst.cpnt_typ_id <> 'CBT'))
group by pa_cpnt_evthst.cpnt_id
) A


Help as this is urgent. Any assistance is VERY much appreciated.
Aug 6 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: JWM | last post by:
I am trying to implement Oracle connection pooling for the following code, which was written by someone else. Here is my main question -- this java file creates code that is executed every hour,...
6
by: Jean | last post by:
Hi, I am using an Access 2000 front-end to an Oracle 9 backend. I want to write a query that returns all records that are not older than one year for Column "Status_30" (which is a Date). ...
21
by: Jaspreet | last post by:
I was working on some database application and had this small task of getting the second highes marks in a class. I was able to do that using subqueries. Just thinking what is a good way of...
14
by: peteh | last post by:
Hi All; We have many production jobs that "load from cursor" to a UDB/AIX 8.2 (with dpf) data warehouse from source tables residing Oracle 9i. Since Oracle dates are (roughly) equivalent to DB2...
0
by: georges the man | last post by:
The purpose: • Sorting and Searching • Numerical Analysis Design Specification You are to write a program called “StockAnalyser”. Your program will read a text file that contains historical...
3
by: jenipriya | last post by:
Tables ------------ Employee (EmpID, EmpName,DeptID DateOfJoin, Sal, Addr) Finance (EmpID, Sal) Club (Clubname, EmpID, Fee, DateOfJoin) Leave (EmpID, Date) Department (DeptID, DeptName,...
5
by: Martin Solveig | last post by:
Hello, SLES9 SP1 is on PIII with two 800MHz procesors and 2GB RAM, on four SATA 156GBhard disks are created two RAID1 arays, on one RAID is OS with 1GB swap and Oracle 10.1.0.4 enterprise edition,...
30
by: mmgarvey | last post by:
Hi, I'm using python to develop some proof-of-concept code for a cryptographic application. My code makes extended use of python's native bignum capabilities. In many cryptographic...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.