| Author |
Topic |
|
callawayx14
Yak Posting Veteran
73 Posts |
Posted - 2007-03-16 : 08:34:32
|
| Hello all. This is my second posting here. The replies to my last post were extremely helpful....thanks!!!AnywayI'm trying to select the 2nd most recent entry by date. I would like to select the MAX minus 1 to eventually put in #table and perform a DATEDIFF on. Any help would be greatly appreciated.ThanksCallaway |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-16 : 08:38:39
|
| OrSELECT TOP 1 * FROM (SELECT TOP 2 * FROM Table1 ORDER BY EntryDate DESC) AS x ORDER BY EntryDatePeter LarssonHelsingborg, Sweden |
 |
|
|
callawayx14
Yak Posting Veteran
73 Posts |
Posted - 2007-03-16 : 08:45:35
|
| Thanks PESO...That works..I should have mentioned this in the first place.....after slecting the last 2 most recent entries, is there a way to a datediff on them w/o having to use a #table? That's what I'm ultimatley tryting to accomplish. Thank you. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-16 : 08:49:29
|
| Do you need all columns from the table?Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-16 : 08:54:57
|
| [code]-- Prepare sample dataDECLARE @Sample TABLE (Col1 INT, Col2 INT, EntryDate DATETIME)INSERT @SampleSELECT 1, 2, '20070301' UNION ALLSELECT 2, 7, '20070310' UNION ALLSELECT 7, 3, '20070315'-- Show the expected outputSELECT TOP 1 Col1, Col2, EntryDate, DATEDIFF(DAY, EntryDate, (SELECT MAX(EntryDate) FROM @Sample)) AS DaysFROM ( SELECT TOP 2 Col1, Col2, EntryDate FROM @Sample ORDER BY EntryDate DESC ) AS xORDER BY EntryDate[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
callawayx14
Yak Posting Veteran
73 Posts |
Posted - 2007-03-16 : 08:59:51
|
| Wow..Thanks. I'll give it a goBrendanNew Jersey, USA |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-03-16 : 10:11:49
|
If no other columns needed:SELECT [First] = MIN(EntryDate), [Last] = MAX(EntryDate), [Diff] = DATEDIFF(DAY, MIN(EntryDate), MAX(EntryDate))FROM ( SELECT TOP 2 EntryDate FROM @Sample ORDER BY EntryDate DESC ) AS x Kristen |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-03-16 : 12:36:41
|
My new Jersey has exits for head, each arm and my torso ... |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
callawayx14
Yak Posting Veteran
73 Posts |
Posted - 2007-03-16 : 14:08:30
|
| Exit 98haaha |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|