Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old April 19th, 2008, 09:30 AM
Member
 
Join Date: Nov 2006
Posts: 36
Default max count

hi all
i have the following query, i need to get the max count from a table

select max(count(*))
from buys
group by v_id

bt i have the error aggregate function calls may not be nested
plzzz help
Reply
  #2  
Old April 19th, 2008, 08:14 PM
Moderator
 
Join Date: Dec 2006
Posts: 250
Default

Quote:
Originally Posted by poopsy
hi all
i have the following query, i need to get the max count from a table

select max(count(*))
from buys
group by v_id

bt i have the error aggregate function calls may not be nested
plzzz help
maybe like that

Expand|Select|Wrap|Line Numbers
  1. select max(column) from (select count(*) as column from buys group by v_id) t;
  2.  
Reply
  #3  
Old April 20th, 2008, 07:19 AM
Member
 
Join Date: Nov 2006
Posts: 36
Default

Quote:
Originally Posted by rski
maybe like that

Expand|Select|Wrap|Line Numbers
  1. select max(column) from (select count(*) as column from buys group by v_id) t;
  2.  

thx a lot it works...but can u help me some more plzz...i need to get the corresponding v_id where the count is maximum..
i tried doing this
Expand|Select|Wrap|Line Numbers
  1. select t.vid,max(c) from (select vid,count(*) as c from buys group by vid) t;
bt i get the error
ERROR: column "t.vid" must appear in the GROUP BY clause or be used in an aggregate function
i tried using top 1 bt i dnt knw how to use it..
i did this
Expand|Select|Wrap|Line Numbers
  1. select TOP 1 t.vid,max(c) from (select vid,count(*) as c from buys group by vid) t group by t.vid;
bt i get a syntax error near 1..

plzz help
Reply
  #4  
Old April 20th, 2008, 06:37 PM
Moderator
 
Join Date: Dec 2006
Posts: 250
Default

Quote:
Originally Posted by poopsy
thx a lot it works...but can u help me some more plzz...i need to get the corresponding v_id where the count is maximum..
i tried doing this
Expand|Select|Wrap|Line Numbers
  1. select t.vid,max(c) from (select vid,count(*) as c from buys group by vid) t;
bt i get the error
ERROR: column "t.vid" must appear in the GROUP BY clause or be used in an aggregate function
i tried using top 1 bt i dnt knw how to use it..
i did this
Expand|Select|Wrap|Line Numbers
  1. select TOP 1 t.vid,max(c) from (select vid,count(*) as c from buys group by vid) t group by t.vid;
bt i get a syntax error near 1..

plzz help
I don't think there is TOP option in postgres, I guess you mean LIMIT clause.
Here is the solution, probably not the best one but I'm to tired to look for better one
Expand|Select|Wrap|Line Numbers
  1. select vid from (select vid ,count(*) as k from buys group by vid)as t where k=(select max(k) from (select count(*) as k from buys group by vid) l);
  2.  
Reply
Reply

Bookmarks

Thread Tools

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 Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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 network members.
Post your question now . . .
It's fast and it's free

Popular Articles