Thread: Howto??

Howto??

From
"J.C.M. van der Kwast"
Date:
Hi,
 
After trying for a week now I've come to the conclusion that help is needed. I've got the following problem:
I have a table with id column, another id (id2) column and a value column. For each id there can be more id2. I want id2 for id where the value is min. I tried several things with having but it doesn't produce what I need. Example:
 
select id, id2, min(value) from table
group by id
having count (*) >1 and min(value) = (select min(value) from table)
 
Now it complains about id2 not being part of aggregate.  
In order to get the min value I have to group by id, right? But because id2 is unique it won't do min on those values, right? So how do you tackle a problem like that? Any suggestions?
 
thanks sjors

Re: Howto??

From
chester c young
Date:
> I have a table with id column, another id (id2) column and a value
> column. For each id there can be more id2. I want id2 for id where
> the value is min. 
> thanks sjors

select distinct id2 from tab
where id=x and value = (select max(value) from tab where id=x)


__________________________________________________
Do you Yahoo!?
Yahoo! Web Hosting - establish your business online
http://webhosting.yahoo.com


Re: Howto??

From
Tomasz Myrta
Date:
Uz.ytkownik J.C.M. van der Kwast napisa?:
> Hi,
>  
> After trying for a week now I've come to the conclusion that help is 
> needed. I've got the following problem:
> I have a table with id column, another id (id2) column and a value 
> column. For each id there can be more id2. I want id2 for id where the 
> value is min. I tried several things with having but it doesn't produce 
> what I need. Example:
>  
> select id, id2, min(value) from table
> group by id
> having count (*) >1 and min(value) = (select min(value) from table)
>  
> Now it complains about id2 not being part of aggregate.  
> In order to get the min value I have to group by id, right? But because 
> id2 is unique it won't do min on those values, right? So how do you 
> tackle a problem like that? Any suggestions?
>  
> thanks sjors

select distinct on(id) id2 from table
order by id,value;

Regards,
Tomasz Myrta