LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2004, 09:20 AM   #1
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Rep: Reputation: 30
help on finding the number that has max occurrences in a column [sql query]


see here

I have a column in a table with numbers:

1
2
3
2
1

the way I have done the tables relationships etc, I need now to have an SQL query that will return the rows that have the numbers with the max-occurrences
those rows here would be 1st and 5th [containg number 1 which occurrs 2 times {which is max}] and 2nd and 4th [containg number 2 which also occurrs 2 times]

I tried max(count(*)) but max wants column not int

I then tried:

SELECT COLUMN_WITH_NUMBERS, COUNT(*) AS cnt_occur
FROM TABLE
GROUP BY COLUMN_WITH_NUMBERS
ORDER BY cnt_occur desc;

so I now have

Code:
COLUMN_WITH_NUMBERS            cnt_occur
1                                                         2
2                                                         2
3                                                         1
so I want the 1st and the 2nd column of the results, can I somehow filter the results? I only use SQL
thank for reading and helping!

Last edited by zeppelin; 06-15-2004 at 10:59 AM.
 
Old 06-13-2004, 04:10 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Does your database support subqueries? If it does you can use
SELECT column_with_numbers, occur FROM table where occur= (select count(*) as occur from table group by column_with_numbers).
Don't remember if you can use 'occur' this way, but I think you can. If your database doesn't have subqueries, you need to use two - first find the maximum (SELECT count(*) INTO variable...) and then find all that have the same.
 
Old 06-14-2004, 12:53 AM   #3
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
SQL is not my strong point, and this is really ugly, but it works in PostgreSQL. There MUST be a better way!

I have this table:

Code:
CREATE TABLE testvalues (
   value INT
);
After populating it with a bunch of numbers, i did this query.

Code:
SELECT * FROM testvalues WHERE value=
(SELECT value
 FROM (SELECT value, count(*) as cnt FROM testvalues GROUP BY value) AS foo
 WHERE foo.cnt=(SELECT MAX(c) FROM (SELECT value,count(*) AS c FROM testvalue GROUP BY value) AS bar))
 
Old 06-14-2004, 09:37 AM   #4
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
have you tried using a view?
 
Old 06-15-2004, 10:33 AM   #5
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
no. I did not. is this the only way? I tried what you proposed guys but none worked for me. let me get back to the main problem i have. I want this kind of SQL code to work [this time I'll use some better names for variables]

SELECT customer_ID, MAX(COUNT(*))
FROM ORDERS;

the above sql query has what i call MAX(COLUMN_HERE) problem :P
this is supposed to count the occurrences of customer_ID in ORDERS table and give the MAXIMUM of the occurrences. What I want to do, is to have the one or more customers that have equal occurrences so I could print their names and their telephones so we can give them a prize or something like it.
damn!

so if ORDERS has this data:
Code:
customer_ID          product
1                      Debian
2                      ArchLinux
3                      Gentoo
1                      Fedora
3                      Slackware
I somehow want to be able with an SQL query to find the names and telephone numbers [via another table] of the customers that have done the most orders. so I need their customer_ID. in this case I want to get customer 1 and customer 3 [both ordered 2 products which is the max here]

I really hope this time I made myself clear and that you can help me on this. thanks in advance

Last edited by zeppelin; 06-15-2004 at 10:39 AM.
 
Old 06-15-2004, 01:11 PM   #6
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
If I understand your problem correctly...this may help
select customer_id from orders group by customer_id having count(*)=(select max(number) from (select count(*) as number from orders group by customer_id) as something);

The subquery (select max(number) from (select count(*) as number from orders group by customer_id) as something) should return you the maximum number of orders from one client.
 
Old 06-15-2004, 01:41 PM   #7
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
the way to do it with a view is to make one from your first query:

create view v1 as
select id, count(*) ct
from table
group by id;

then add to your main query:

select ...
from...
where id in (select id from v1 where ct = select max(ct) from v1)
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL question: Need to add a column from another table Hivemind Programming 3 09-19-2005 06:02 PM
Finding the name and data type of a column in SQL Travis86 Programming 11 08-15-2004 04:20 PM
max number of tty alaios Linux - Newbie 3 07-14-2004 12:23 AM
max socket number sector Programming 1 06-30-2003 09:11 AM
max socket number sector Linux - Software 0 06-30-2003 08:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration