Subject | RE: [firebird-support] max count question |
---|---|
Author | Dunbar, Norman (Capgemini) |
Post date | 2011-06-02T07:43:42Z |
Morning Olaf,
I may not be understanding exactly what you want, but I'll try:
To get the count or rows for field_name A, B, C ... you would use this:
select field_name,count(*) as kount
from table_name
group by field_name;
This would give something like this for your example:
FIELD_NAME KOUNT
-----------------
A 100
C 30
B 50
To get the field_name where the count is lowest, you would do this:
with whatever as
(select field_name,count(*) as kount
from table_name
group by field_name)
select field_name from whatever
where kount = (select min(kount) from whatever);
The result would be as follows for your example:
FIELD_NAME
----------
C
However, consider this example:
FIELD_NAME KOUNT
-----------------
A 100
C 30
B 50
D 30
The result would now be:
FIELD_NAME
----------
C
D
How would you choose which one is "correct" - both have the same count.
HTH
Cheers,
Norm.
Norman Dunbar
Contract Senior Oracle DBA
Capgemini Database Team (EA)
Internal : 7 28 2051
External : 0113 231 2051
Information in this message may be confidential and may be legally privileged. If you have received this message by mistake, please notify the sender immediately, delete it and do not copy it to anyone else.
We have checked this email and its attachments for viruses. But you should still check any attachment before opening it.
We may have to make this message and any reply to it public if asked to under the Freedom of Information Act, Data Protection Act or for litigation. Email messages and attachments sent to or from any Environment Agency address may also be accessed by someone other than the sender or recipient, for business purposes.
If we have sent you information and you wish to use it please read our terms and conditions which you can get by calling us on 08708 506 506. Find out more about the Environment Agency at www.environment-agency.gov.uk
I may not be understanding exactly what you want, but I'll try:
To get the count or rows for field_name A, B, C ... you would use this:
select field_name,count(*) as kount
from table_name
group by field_name;
This would give something like this for your example:
FIELD_NAME KOUNT
-----------------
A 100
C 30
B 50
To get the field_name where the count is lowest, you would do this:
with whatever as
(select field_name,count(*) as kount
from table_name
group by field_name)
select field_name from whatever
where kount = (select min(kount) from whatever);
The result would be as follows for your example:
FIELD_NAME
----------
C
However, consider this example:
FIELD_NAME KOUNT
-----------------
A 100
C 30
B 50
D 30
The result would now be:
FIELD_NAME
----------
C
D
How would you choose which one is "correct" - both have the same count.
HTH
Cheers,
Norm.
Norman Dunbar
Contract Senior Oracle DBA
Capgemini Database Team (EA)
Internal : 7 28 2051
External : 0113 231 2051
Information in this message may be confidential and may be legally privileged. If you have received this message by mistake, please notify the sender immediately, delete it and do not copy it to anyone else.
We have checked this email and its attachments for viruses. But you should still check any attachment before opening it.
We may have to make this message and any reply to it public if asked to under the Freedom of Information Act, Data Protection Act or for litigation. Email messages and attachments sent to or from any Environment Agency address may also be accessed by someone other than the sender or recipient, for business purposes.
If we have sent you information and you wish to use it please read our terms and conditions which you can get by calling us on 08708 506 506. Find out more about the Environment Agency at www.environment-agency.gov.uk