Connecting Tech Pros Worldwide Help | Site Map

can this be done with one query?

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 12th, 2005, 11:58 PM
sparks
Guest
 
Posts: n/a
Default can this be done with one query?

I have 3 fields
id result date
1 1 11/11/02
1 2.3 12/12/03
1
1 4.6 06/04/04

can one query give me all these things?
the min result record 1 1 11/11/02
the first result based on date 1 1 11/11/02
the maximum result info. 1 4.6 06/04/04


thanks for the info

jerry


  #2  
Old November 12th, 2005, 11:58 PM
John Winterbottom
Guest
 
Posts: n/a
Default Re: can this be done with one query?

"sparks" <sparks@here.com> wrote in message
news:iqd6d0dpqst0okn14ovdgt07f7cc0tvugh@4ax.com...[color=blue]
> I have 3 fields
> id result date
> 1 1 11/11/02
> 1 2.3 12/12/03
> 1
> 1 4.6 06/04/04
>
> can one query give me all these things?
> the min result record 1 1 11/11/02
> the first result based on date 1 1 11/11/02
> the maximum result info. 1 4.6 06/04/04
>
>[/color]

This could be slow on large tables. A better way might be to turn the
aggregated list into a derived table and join it back to the original table
to get the related record info - post back if you need that one instead.
Otherwise this should work:


select r.id, min(r.result) as minResult,
(
select r2.[date]
from results r2
where r2.id = r.id
and r2.result =
(
select min(r3.result)
from results r3
where r3.id = r.id
)
) as minResultDate,
(
select r2.result
from results r2
where r2.id = r.id
and r2.[date] =
(
select min(r3.[date])
from results r3
where r3.id = r.id
)
) as firstResult,
max(r.result) as maxResult,
(
select r2.[date]
from results r2
where r2.id = r.id
and r2.result =
(
select max(r3.result)
from results r3
where r3.id = r.id
)
) as maxResultDate
from results as r
group by r.id
order by r.id


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.