Unix Technical Forum

Retrieving database record with the lowest value in field aaa?

This is a discussion on Retrieving database record with the lowest value in field aaa? within the DB2 forums, part of the Database Server Software category; --> How do I code a SQL SELECT statement so that always only this record is retrieved which matches a ...


Go Back   Unix Technical Forum > Database Server Software > DB2

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 02:45 AM
George Dainis
 
Posts: n/a
Default Retrieving database record with the lowest value in field aaa?

How do I code a SQL SELECT statement so that always only this record is retrieved
which matches a certain criteria AND has the lowest ID (= value in key field aaa)?

It must me something like

SELECT * FROM .... WHERE somefield='somevalue' AND aaa=lowestkey(column(aaa))

As a result either zero or at most 1 record should be passed back.

George

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 02:45 AM
Florian Boldt
 
Posts: n/a
Default Re: Retrieving database record with the lowest value in field aaa?

Hi George,

how about:

select *
from ...
where somefield='somevalue'
and aaa=(select min(aaa) from ...)

or:

select aaa,...,...,...
from ...
where somefield='somevalues'
order by aaa
fetch first 1 row only

index on aaa would help in both cases
and don't use the asterisk

hope that helps


cheers
Florian

George Dainis wrote:
> How do I code a SQL SELECT statement so that always only this record is retrieved
> which matches a certain criteria AND has the lowest ID (= value in key field aaa)?
>
> It must me something like
>
> SELECT * FROM .... WHERE somefield='somevalue' AND aaa=lowestkey(column(aaa))
>
> As a result either zero or at most 1 record should be passed back.
>
> George
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 02:45 AM
DA Morgan
 
Posts: n/a
Default Re: Retrieving database record with the lowest value in field aaa?

George Dainis wrote:

> How do I code a SQL SELECT statement so that always only this record is retrieved
> which matches a certain criteria AND has the lowest ID (= value in key field aaa)?
>
> It must me something like
>
> SELECT * FROM .... WHERE somefield='somevalue' AND aaa=lowestkey(column(aaa))
>
> As a result either zero or at most 1 record should be passed back.
>
> George


I am going to assume, given that you have posted this to every usenet
group you can spell, among them comp.databases.oracle.misc,
microsoft.public.sqlserver.programming, comp.databases.oracle, and
comp.databases.ibm-db2, that you are trying to find someone to do your
homework for you.

The optimal solution will vary by product and even version so posting
as you have says something about what you are trying to do.

As we don't do other people's homework for them and you seemingly have
made no attempt to solve this on your own ... go talk to your faculty
advisor about what you have done and ask for help there.
--
Daniel A. Morgan
University of Washington
damorgan@x.washington.edu
(replace 'x' with 'u' to respond)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:47 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com