473,498 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting the second latest date in a group

11 New Member
Hi All,

Currently, I have a SQL query where I need to grab the SECOND latest date by group.

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 CBT_HAZCOM. 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

Please note that 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
6 7831
piyushdabomb
11 New Member
Sorry for the miscommunication. The wording may have been a little off:

"What I want to do from this table is grab the second latest date under the completiondate column ID grouped by COMPONENTID. If there is only 1 entry for COMPONENTID (like for QS and SHEN158), it should keep the current COMPLETIONDATE"
Aug 4 '07 #2
pbmods
5,821 Recognized Expert Expert
Heya, Piyush.

If getting the latest date looks something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `COMPONENTID`,
  3.         `COMPLETIONDATE`
  4.     FROM
  5.         `Table`
  6.     ORDER BY
  7.         `COMPLETIONDATE` DESC
  8.     LIMIT 1;
  9.  
Then getting the second-to last date looks like this (note the LIMIT clause):
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `COMPONENTID`,
  3.         `COMPLETIONDATE`
  4.     FROM
  5.         `Table`
  6.     ORDER BY
  7.         `COMPLETIONDATE` DESC
  8.     LIMIT 1,1;
  9.  
The first number tells MySQL to offset the results by one. The second number tells MySQL to return only one result.
Aug 5 '07 #3
piyushdabomb
11 New Member
Very much appreciated, however is the LIMIT command only for My SQL? Currently, I need my code universal to Oracle Databases and the Limit 1,1 will not work.

Is there an alternative without having to use LIMIT 1,1? I have spent numerous hours trying to figure this out; any help is appreciated.

This is the SQL code I used to determine the max value:

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
Aug 6 '07 #4
pbmods
5,821 Recognized Expert Expert
Heya, piyushdabomb.

I'm going to go ahead and move this thread to the Oracle forum, where our resident Experts will be better able to help you out.
Aug 6 '07 #5
Dave44
153 New Member
Try this out. It uses an analytic function to number the rows by date desc, grouped by the component. then i simply limit the count to 2 rows per group and take the minimum.

Expand|Select|Wrap|Line Numbers
  1. SELECT   MIN( componentid) componentid,
  2.          MIN( completiondate) completiondate
  3. FROM     (SELECT componentid,
  4.                  completiondate,
  5.                  ROW_NUMBER() OVER(PARTITION BY componentid ORDER BY completiondate DESC) my_row_num
  6.           FROM   temp)
  7. WHERE    my_row_num <= 2
  8. GROUP BY componentid;
  9.  
Aug 7 '07 #6
piyushdabomb
11 New Member
Dave44:

After much research you've brought a ton of insight as to using "rows" instead of playing with subselects and outerjoins...Online Analytical Processes are definitely the way to go (from your suggestions).

My questions now bear:-

> Do you have any good links that could help me optimize my code?
> How do I know for example that an outerjoin is going to be quicker than using windows functions?
> What difficulty level would you classify Analytical processes if we were to categorize them - Beginner, Intermediate, Above Average, Guru

I seem to use a TON of subselects but also takes my query 3 minutes to run. Potentially, I could increase the speed of my queries if I knew the correct alternatives.
Aug 7 '07 #7

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

Similar topics

16
2272
by: Robbie | last post by:
Hi All This is a belter that my little brain can't handle. Basically I have 1 SQL table that contains the following fields: Stock Code Stock Desc Reference Transaction Date
2
3368
by: M.Stanley | last post by:
Hi, I have a problem..I'm doing a specific query where I'm joining fields from a table with appednded data (there are duplicate records, except for the date/time), and another query. I want the...
5
5924
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an...
7
6324
by: Scott Frankel | last post by:
Still too new to SQL to have run across this yet ... How does one return the latest row from a table, given multiple entries of varying data? i.e.: given a table that looks like this: color...
5
7190
by: alanspamenglefield | last post by:
Hello group, I have an SQL statement which pulls data from a table as follows: " SELECT tblSites.sites_siteno, " & _ " tblSites.sites_sitename, " & _ " Sum(tblStockResults.stkr_result) AS...
24
19859
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
2
5720
by: piyushdabomb | last post by:
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...
2
4432
by: srusskinyon | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
7
2831
by: Yesurbius | last post by:
I am receiving the following error when attempting to run my query. In my mind - this error should not be happening - its a straight-forward query with a subquery. I am using Access 2003 with all...
0
7124
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
6998
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
7163
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,...
1
6884
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
7375
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
5460
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4904
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...
0
4586
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.