Re: Aggregate question - Mailing list pgsql-novice

From Oliver Elphick
Subject Re: Aggregate question
Date
Msg-id 1093762253.29613.316.camel@linda
Whole thread Raw
In response to Aggregate question  (Sean Davis <sdavis2@mail.nih.gov>)
List pgsql-novice
On Fri, 2004-08-27 at 11:54, Sean Davis wrote:
> I'm sorry for the simple question, but....  I have a table (description
> given below) that I want to do something like:
>
> select oligo,target_id,max(2*matches-mismatch) as "score" from hit
> natural join oligo where oligo like 'H200000%' group by
> oligo,target_id;

...
> But, what I actually want is only those target_ids that reach the max
> score like:

The way to restrict the GROUP BY output is with a HAVING clause.  I
think it should go something like this:

select oligo,target_id,max(2*matches-mismatch) as "score" from hit
natural join oligo where oligo like 'H200000%' group by oligo,target_id
HAVING max(2*matches-mismatch) = (SELECT max(2*matches-mismatch) from
hit);

I don't think you can use score as a column name in the having clause,
which is why I have repeated the original definition of that column.

--
Oliver Elphick                                          olly@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
GPG: 1024D/A54310EA  92C8 39E7 280E 3631 3F0E  1EC0 5664 7A2F A543 10EA
                 ========================================
     "Preach the word; be instant in season, out of season;
      reprove, rebuke, exhort with all longsuffering and
      doctrine."          II Timothy 4:2


pgsql-novice by date:

Previous
From: Mike
Date:
Subject: Re: pgsql functions and transactions?
Next
From:
Date:
Subject: Re: pgsql functions and transactions?