If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > select max(count of rows) (was "SQL")

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-27-05, 05:20
process process is offline
Registered User
 
Join Date: Jan 2005
Posts: 13
Exclamation select max(count of rows) (was "SQL")

Hi there,

I have a problem in extracting data from db. My db is user_table with parameters like code1, code2, number_1, number_2, date_1 and date_2.

I want to extract the number of users to code1 and code2.

My coding are:

SQL> select code1, count(code1) from user_table group by code1;

Result is:

CODE1 COUNT(CODE1)
------------ ----------------
1234567890 5
0987654321 2
1472583690 6
3692581470 1

SQL> select max(code1) from user_table;

MAX(CODE1)
------------
1472583690

When i did a max(code1), instead of giving me the maximum count(code1) which is 6, code1 - 3692581470 is given.

Please advice.

Thanks a million.

Regards,
Reply With Quote
  #2 (permalink)  
Old 01-27-05, 07:33
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
You don't want the max(code1), you want the max(count of rows per code1):
Code:
select max(cnt) from
( select code1, count(code1) cnt
  from user_table
  group by code1
);
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 01-28-05, 01:10
process process is offline
Registered User
 
Join Date: Jan 2005
Posts: 13
Quote:
Originally Posted by andrewst
You don't want the max(code1), you want the max(count of rows per code1):
Code:
select max(cnt) from
( select code1, count(code1) cnt
  from user_table
  group by code1
);
Hi andrewst,

Thank you for the reply. May i know what does max(cnt) means?

Regards,
Reply With Quote
  #4 (permalink)  
Old 01-28-05, 04:09
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
"cnt" is the name of a column in the inline view:

select max(cnt) from
( select code1, count(code1) cnt
from user_table
group by code1
);
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 01-28-05, 05:02
process process is offline
Registered User
 
Join Date: Jan 2005
Posts: 13
Quote:
Originally Posted by andrewst
"cnt" is the name of a column in the inline view:

select max(cnt) from
( select code1, count(code1) cnt
from user_table
group by code1
);
Thank you very much.
Reply With Quote
  #6 (permalink)  
Old 01-28-05, 05:21
process process is offline
Registered User
 
Join Date: Jan 2005
Posts: 13
Hi andrewst,

I have another doubt. I would like to display both code1 and the maximum count at the same time. But i encounted this error prompt.

select code1, max(cnt) from (select code1, count(code1) cnt from user_table group by code1)
*
ERROR at line 1:
ORA-00937: not a single-group group function

Thank you.
Reply With Quote
  #7 (permalink)  
Old 01-28-05, 05:32
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
That's a little harder:
Code:
select code1, count(code1)
from user_table
group by code1
having count(code1) =
( select max(cnt) from
  ( select code1, count(code1) cnt
    from user_table
    group by code1
  )
);
Or (since I believe you are using Oracle) you could use Oracle's RANK analytic function:
Code:
select code1, cnt from
( select code1, cnt, rank() over (order by cnt desc) rnk
  from
  ( select code1, count(code1) cnt
    from user_table
    group by code1
  )
)
where rnk=1;
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #8 (permalink)  
Old 01-28-05, 05:51
process process is offline
Registered User
 
Join Date: Jan 2005
Posts: 13
Thumbs up

Quote:
Originally Posted by andrewst
That's a little harder:
Code:
select code1, count(code1)
from user_table
group by code1
having count(code1) =
( select max(cnt) from
  ( select code1, count(code1) cnt
    from user_table
    group by code1
  )
);
Or (since I believe you are using Oracle) you could use Oracle's RANK analytic function:
Code:
select code1, cnt from
( select code1, cnt, rank() over (order by cnt desc) rnk
  from
  ( select code1, count(code1) cnt
    from user_table
    group by code1
  )
)
where rnk=1;

Wow. That's pretty tough.

andrewst, you are a great help. I am able to get it.
Thank you so much.
Reply With Quote
  #9 (permalink)  
Old 02-14-05, 20:52
process process is offline
Registered User
 
Join Date: Jan 2005
Posts: 13
Hi once again,

I tried this code and it works fine. However, there is a slight problem. The maximum count of the code is empty (majority of the figure goes to this empty field).

I would like to choose the 2nd maximum count which has a valid code number. Is there a possible way?

Code
====
select code1, count(code1)
from user_table
group by code1
having count(code1) =
( select max(cnt) from
( select code1, count(code1) cnt
from user_table
group by code1
)
);

Result
=====
CODE1 COUNT(CODE1)
------ ---------------
(*empty*) 63232


Or shall i include a { where code1 != ' ' } in the code as above.

Please advice.

Thank you very much.
Reply With Quote
  #10 (permalink)  
Old 02-15-05, 05:20
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Use: "where code1 is not null"
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #11 (permalink)  
Old 02-15-05, 21:44
process process is offline
Registered User
 
Join Date: Jan 2005
Posts: 13
Quote:
Originally Posted by andrewst
Use: "where code1 is not null"
Hi andrewst,

1st Doubt,

I still encounter the same result irregardless whether i enter "where code1 is not null".

SQL> select code1, count(code1) from subs_tbl where code1 is not null group by code1 having count(code1) = (select max(cnt) from (select code1, count(code1) cnt from users_table group by code1));

CODE1 COUNT(CODE1)
------- --------------
*empty* 65566

2nd Doubt,

Is there any possiblility where i can extract the 2nd maximum CODE1 and COUNT(CODE1).

Thank you so much.

Regards,
Reply With Quote
  #12 (permalink)  
Old 02-16-05, 05:28
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
OK, so it isn't a NULL in there then. That's nasty. You need to find out what is in there and then exclude that. e.g. if it is a single space then

where code1 != ' '

Sounds like your data needs cleaning up though: a "code" column should never be set to a value that is invisible (other than null of course)!
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #13 (permalink)  
Old 02-23-05, 06:42
jay82 jay82 is offline
Registered User
 
Join Date: Feb 2005
Location: London
Posts: 19
Quote:
Originally Posted by andrewst
That's a little harder:
Code:
select code1, count(code1)
from user_table
group by code1
having count(code1) =
( select max(cnt) from
  ( select code1, count(code1) cnt
    from user_table
    group by code1
  )
);
Or (since I believe you are using Oracle) you could use Oracle's RANK analytic function:
Code:
select code1, cnt from
( select code1, cnt, rank() over (order by cnt desc) rnk
  from
  ( select code1, count(code1) cnt
    from user_table
    group by code1
  )
)
where rnk=1;
Hi,

Why can't we do this way??

SELECT TOP 1 code1, COUNT(code1) AS total_count
FROM user_table
GROUP BY code1
ORDER BY total_count DESC
Reply With Quote
  #14 (permalink)  
Old 02-23-05, 06:49
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally Posted by jay82
Hi,

Why can't we do this way??

SELECT TOP 1 code1, COUNT(code1) AS total_count
FROM user_table
GROUP BY code1
ORDER BY total_count DESC
Because Oracle doesn't support the SQL Server "TOP" syntax. The OP was using Oracle.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #15 (permalink)  
Old 06-20-10, 18:10
KD4 KD4 is offline
Registered User
 
Join Date: Jun 2010
Posts: 1
Hi, thank you loads for the answers provided here, was of great use
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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