Re: SQL Update with aggregate MIN function
- From: "John Spencer" <spencer@xxxxxxxxxxxxxx>
- Date: Mon, 17 Oct 2005 16:48:44 -0400
IF you are going to run this and want ONLY the specific records marked then
you need to do TWO queries; the first to clear any currently marked items
and the second to find and mark just the items you want marked.
First, clear all the marked records
UPDATE Results
Set Lowest_Result = False
WHERE Lowest_Result = True
Now mark just the lowest
UPDATE results
SET lowest_result=true
WHERE result =
(SELECT MIN(R.result) FROM results As R
WHERE R.Athlete = Results.Athlete AND
R.TrainingSession = Results.TrainingSession)
If you were trying to restrict this to just one athlete and session you
could use a where clause to limit the records further.
UPDATE Results
Set Lowest_Result = False
WHERE Lowest_Result = True
AND Athlete = "Ben"
AND TrainingSession = "Weights1"
Now mark just the lowest
UPDATE results
SET lowest_result=true
WHERE Athlete = "Ben"
AND TrainingSession = "Weights1"
result = (SELECT MIN(R.result) FROM results As R
WHERE R.Athlete = "BEN"AND
R.TrainingSession = "Weights1")
"cjmccann" <ciaranj@xxxxxxxxxxxxxxxxxxx> wrote in message
news:uFt9gb10FHA.3300@xxxxxxxxxxxxxxxxxxxxxxx
> Hi Vincent,
>
>
>
> I hope you don't mind another question... Thank you all the same.
>
>
>
> The suggested solution(s) is/are working better, though I have a slight
> problem in that it is
>
> updating all the records. Let me explain.
>
>
>
> for example, Results table
>
>
>
> athlete, training_sessions, result, lowest_result
>
>
>
> Ben weights1 80 yes/no
>
>
>
> Ben weights1 79 yes/no
>
>
>
> Joe weights2 60 yes/no
>
>
>
> Joe weights2 58 yes/no
>
>
>
> I would like records 2 and 4 to be updated (set to true/yes).
>
>
>
> Can we refine this? Also, as per a point raised to have to maintain this
> in the same table .v. a query, this setup is quite central to how I pull
> other pieces of information together. (the amount of data presently is
> quite small and therefore manageable, manually). So, I plan to update the
> table as the new data entered is 'posted' (via a button) on a form. This
> ensures that the underlying table is always up-to-date.
>
>
>
> Thanks in advance for your help.
>
>
>
> Ciaran
>
> "Vincent Johns" <vjohns@xxxxxxxxxxxxxxxxxx> wrote in message
> news:qQE4f.1733$fc7.1265@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>> John Spencer wrote:
>>
>>> I would try the following. TEST IT FIRST on a copy of your data.
>>> UPDATE results
>>> SET lowest_result=true
>>> WHERE result = (SELECT MIN(R.result) FROM results As R
>>> WHERE R.Athlete = Results.Athlete AND
>>> R.TrainingSession = Results.TrainingSession)
>>>
>>>
>>> One problem is that if the lowest result is a tie, you will get more
>>> than one
>>> record marked as the lowest result for that athlete + training session
>>> combination
>>
>> Another problem with storing this kind of information into a Table is
>> that it may become invalid when some other record is updated, deleted, or
>> added. Getting this information from a Query instead of from a field in
>> the Table would give you fairly current information.
>>
>> OTOH, if you rarely change the contents of records in the Table but
>> frequently read the Table, then updating it as you are planning to do
>> makes sense, but you'll probably have to re-run your Update Query before
>> you read anything from the Table, if any record in it has been changed
>> since the previous time you updated the field. It can be a pain to have
>> to keep track of that.
>>
>> -- Vincent Johns <vjohns@xxxxxxxxxxxxxxxxxx>
>> Please feel free to quote anything I say here.
>>
>
>
.
- Follow-Ups:
- Re: SQL Update with aggregate MIN function
- From: cjmccann
- Re: SQL Update with aggregate MIN function
- References:
- SQL Update with aggregate MIN function
- From: cjmccann
- Re: SQL Update with aggregate MIN function
- From: Vincent Johns
- SQL Update with aggregate MIN function
- Prev by Date: Re: Running long queries?
- Next by Date: Re: SQL Update with aggregate MIN function
- Previous by thread: Re: SQL Update with aggregate MIN function
- Next by thread: Re: SQL Update with aggregate MIN function
- Index(es):
Relevant Pages
|