Sie sind auf Seite 1von 5

SQL Query - Latest date [Archive] - DevX.

com Forums

Page 1 of 5

DevX.com Forums > DevX Developer Forums > Database > SQL Query - Latest date
PDA

Click to See Complete Forum and Search --> : SQL Query - Latest date
ML

10-18-2001, 08:51 AM

Looking for a snip of code to add to a select query to get the latest date.
Example: a person has multipe entries into a table, each has a update_date.
I want to be able to get the record w/ the latest date only.
1,Bob Smith, 10%, 2/7/01
2,Bob Smith, 12%, 5/7/01
3,Bob Smith, 7%, 8/15/01
I want to return only row 3.
Thanks.

DaveSatz

10-18-2001, 10:47 AM

create table #x
( id int
, name varchar(30)
, pct varchar(30)
, date datetime )
go
insert #x
select 1,'Bob Smith', '10%', '2/7/01'
insert #x
select 2,'Bob Smith', '12%','5/7/01'
insert #x
select 3,'Bob Smith', '7%', '8/15/01'
go
SELECT *
FROM #x x
WHERE date = ( SELECT max(x2.date)
FROM #x x2
WHERE x.name = x2.name)
drop table #x
go
-HTH,
David Satz
Principal Software Engineer
Hyperion Solutions
{ SQL Server 2000 SP1/7.0 SP3/6.5 SP5a } { Cold Fusion 5/4.5.1 SP2 } { VSS }
(Please reply to group only - emails answered rarely)
This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.
----------------------------------------------------------------"ML" <mlarson@ptc.com> wrote in message news:3bcec253$1@news.devx.com...
>
>
> Looking for a snip of code to add to a select query to get the latest
date.
>
> Example: a person has multipe entries into a table, each has a
update_date.
> I want to be able to get the record w/ the latest date only.
>
> 1,Bob Smith, 10%, 2/7/01

http://forums.devx.com/archive/index.php/t-24810.html

12/2/2011

SQL Query - Latest date [Archive] - DevX.com Forums

>
>
>
>
>
>

Page 2 of 5

2,Bob Smith, 12%, 5/7/01


3,Bob Smith, 7%, 8/15/01
I want to return only row 3.
Thanks.

Kevin

10-18-2001, 04:04 PM

I think the below would do it.


select top 1 * from table order by update_date desc
This would guarantee you would only get one record. The prior solution would
return multiple records if more than one record had the same update date.
Multiple records may or may not matter to you.
"ML" <mlarson@ptc.com> wrote:
>
>
>Looking for a snip of code to add to a select query to get the latest date.
>
>Example: a person has multipe entries into a table, each has a update_date.
> I want to be able to get the record w/ the latest date only.
>
>1,Bob Smith, 10%, 2/7/01
>2,Bob Smith, 12%, 5/7/01
>3,Bob Smith, 7%, 8/15/01
>
>I want to return only row 3.
>
>Thanks.

Joe \Nuke Me Xemu\ Foster

10-18-2001, 08:11 PM

"Kevin" <kevinv@unisolve.com> wrote in message <news:3bcf27a7$1@news.devx.com>...


>
>
>
>
>
>
>

I think the below would do it.


select top 1 * from table order by update_date desc
This would guarantee you would only get one record. The prior solution would
return multiple records if more than one record had the same update date.
Multiple records may or may not matter to you.

I thought TOP could return multiple records in the case of a tie,


but SET ROWCOUNT would not. In my own code, I usually just assume
I could get 0, 1, or many rows back, so I never bothered to find out
for certain.
-Joe Foster <mailto:jlfoster%40znet.com> Sign the Check! <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above They're coming to
because my cats have apparently learned to type. take me away, ha ha!

Lev

10-22-2001, 04:44 PM

I would prefer the following technique:


SELECT *
FROM <table>
WHERE name = :ar_name and
date =
(SELECT max (
nvl(to_char(trunc(FROM_DATE),'YYYYMMDD:HHMMSS'),'00000000:000000'))
FROM <table> t2
WHERE t2.name = :ar-name)
);

http://forums.devx.com/archive/index.php/t-24810.html

12/2/2011

SQL Query - Latest date [Archive] - DevX.com Forums

Page 3 of 5

If there is several entries for the same date - they will be differ by time
and only one will be selected;
If update date is Null (that sometimes happens for first entry - it will
be selected as well.
Good luck! Lev
"Joe \"Nuke Me Xemu\" Foster" <joe@bftsi0.UUCP> wrote:
>"Kevin" <kevinv@unisolve.com> wrote in message <news:3bcf27a7$1@news.devx.com>...
>
>> I think the below would do it.
>>
>> select top 1 * from table order by update_date desc
>>
>> This would guarantee you would only get one record. The prior solution
would
>> return multiple records if more than one record had the same update date.
>> Multiple records may or may not matter to you.
>
>I thought TOP could return multiple records in the case of a tie,
>but SET ROWCOUNT would not. In my own code, I usually just assume
>I could get 0, 1, or many rows back, so I never bothered to find out
>for certain.
>
>->Joe Foster <mailto:jlfoster%40znet.com> Sign the Check! <http://www.xenu.net/>
>WARNING: I cannot be held responsible for the above They're coming
to
>because my cats have apparently learned to type. take me away,
ha ha!
>
>

Kevin

10-23-2001, 12:28 PM

The time on the date would most likely make only one record be found. I wasn't
thinking about the time portion of the date. I have been working with applications
that strip the time off when they update date fields. I have had to deal
with the multiple record problem because our applications don't have a time
with the date.
On the other hand, between the two choices of queries, the 'select top 1..'
uses less SQL Server resources than the query with the sub select. I also
think the syntax is cleaner. Pick your poison.
"Lev" <levrom@hotmail.com> wrote:
>
>I would prefer the following technique:
>
>SELECT *
>FROM <table>
>WHERE name = :ar_name and
> date =
> (SELECT max (
> nvl(to_char(trunc(FROM_DATE),'YYYYMMDD:HHMMSS'),'00000000:000000'))
> FROM <table> t2
> WHERE t2.name = :ar-name)
> );
>
>If there is several entries for the same date - they will be differ by time
>and only one will be selected;
>If update date is Null (that sometimes happens for first entry - it will
>be selected as well.
>Good luck! Lev
>
>
>"Joe \"Nuke Me Xemu\" Foster" <joe@bftsi0.UUCP> wrote:
>>"Kevin" <kevinv@unisolve.com> wrote in message <news:3bcf27a7$1@news.devx.com>...
>>
>>> I think the below would do it.
>>>

http://forums.devx.com/archive/index.php/t-24810.html

12/2/2011

SQL Query - Latest date [Archive] - DevX.com Forums

Page 4 of 5

>>> select top 1 * from table order by update_date desc


>>>
>>> This would guarantee you would only get one record. The prior solution
>would
>>> return multiple records if more than one record had the same update date.
>>> Multiple records may or may not matter to you.
>>
>>I thought TOP could return multiple records in the case of a tie,
>>but SET ROWCOUNT would not. In my own code, I usually just assume
>>I could get 0, 1, or many rows back, so I never bothered to find out
>>for certain.
>>
>>->>Joe Foster <mailto:jlfoster%40znet.com> Sign the Check! <http://www.xenu.net/>
>>WARNING: I cannot be held responsible for the above They're coming
> to
>>because my cats have apparently learned to type. take me away,
>ha ha!
>>
>>
>

Lev

10-23-2001, 04:30 PM

Agree in general.
Just for ORACLE instead of top 1, ROWNUM will be used.
And in case of Null value in the field we need to treat it somehow.
But in clean case your performence will be better.
Lev
"Kevin" <kevinv@unisolve.com> wrote:
>
>The time on the date would most likely make only one record be found. I
wasn't
>thinking about the time portion of the date. I have been working with applications
>that strip the time off when they update date fields. I have had to deal
>with the multiple record problem because our applications don't have a time
>with the date.
>
>On the other hand, between the two choices of queries, the 'select top 1..'
>uses less SQL Server resources than the query with the sub select. I also
>think the syntax is cleaner. Pick your poison.
>
>"Lev" <levrom@hotmail.com> wrote:
>>
>>I would prefer the following technique:
>>
>>SELECT *
>>FROM <table>
>>WHERE name = :ar_name and
>> date =
>> (SELECT max (
>> nvl(to_char(trunc(FROM_DATE),'YYYYMMDD:HHMMSS'),'00000000:000000'))
>> FROM <table> t2
>> WHERE t2.name = :ar-name)
>> );
>>
>>If there is several entries for the same date - they will be differ by
time
>>and only one will be selected;
>>If update date is Null (that sometimes happens for first entry - it will
>>be selected as well.
>>Good luck! Lev
>>
>>
>>"Joe \"Nuke Me Xemu\" Foster" <joe@bftsi0.UUCP> wrote:
>>>"Kevin" <kevinv@unisolve.com> wrote in message <news:3bcf27a7$1@news.devx.com>...
>>>
>>>> I think the below would do it.
>>>>

http://forums.devx.com/archive/index.php/t-24810.html

12/2/2011

SQL Query - Latest date [Archive] - DevX.com Forums

Page 5 of 5

>>>> select top 1 * from table order by update_date desc


>>>>
>>>> This would guarantee you would only get one record. The prior solution
>>would
>>>> return multiple records if more than one record had the same update
date.
>>>> Multiple records may or may not matter to you.
>>>
>>>I thought TOP could return multiple records in the case of a tie,
>>>but SET ROWCOUNT would not. In my own code, I usually just assume
>>>I could get 0, 1, or many rows back, so I never bothered to find out
>>>for certain.
>>>
>>>->>>Joe Foster <mailto:jlfoster%40znet.com> Sign the Check! <http://www.xenu.net/>
>>>WARNING: I cannot be held responsible for the above They're coming
>> to
>>>because my cats have apparently learned to type. take me away,
>>ha ha!
>>>
>>>
>>
>

10-24-2001, 01:08 PM

Helena
Try this:
SELECT TOP 1 FROM <Table> ORDER BY <Col_Date> DESC
"ML" <mlarson@ptc.com> wrote:
>
>
>Looking for a snip of code to add to a select query to get the latest date.
>
>Example: a person has multipe entries into a table, each has a update_date.
> I want to be able to get the record w/ the latest date only.
>
>1,Bob Smith, 10%, 2/7/01
>2,Bob Smith, 12%, 5/7/01
>3,Bob Smith, 7%, 8/15/01
>
>I want to return only row 3.
>
>Thanks.
devx.com
Copyright 2011 Internet.com Inc. All Rights Reserved

http://forums.devx.com/archive/index.php/t-24810.html

12/2/2011

Das könnte Ihnen auch gefallen