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 > Database Server Software > MySQL > How to query about all entries which occur less than n times

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-01-09, 00:38
cy163 cy163 is offline
Registered User
 
Join Date: Apr 2007
Posts: 127
How to query about all entries which occur less than n times

Hello ALL,

I have a table as follows

Code:
ID   Cate.  		Word
1    Sports 		basketball
2    Sports  		football
3    SPorts  		volleyball

4    Economy		share
5    Economy		bank
6    Economy		credit
7    Economy		basketball

8    Society   		credit
9    Society		democracy
10   Society 		people
11   Society 		basketball
I would like to query about those words which occur in less than 3 (<3) categories.
In the above example, the query result should be all entries except for entries corresponding to 'basketball', i.e., all excluding entries 1, 7, 11.


I used the following statement

Code:
Select *
 From TableName
Group By
      Word
Having Count(*)<3
Order by
    Cate
However, I got less entries than expected by this statement.
For a word, only one entry is returned. e.g. for 'credit'

I have

Code:
6    Economy		credit
while I expect

Code:
6    Economy		credit
8    Society   		credit
it seems that the clause 'group by' lead to this result. How can I do to get the right answer?
Please help
Reply With Quote
  #2 (permalink)  
Old 06-01-09, 03:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT *
  FROM TableName
 WHERE word IN
       ( SELECT word
           FROM TableName
         GROUP 
             BY Word
         HAVING COUNT(*) < 3 )
ORDER 
    BY Cate
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-01-09, 19:31
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
My preference is to join instead of using the IN operator as a join will scale better
Code:
SELECT a.*
FROM   table_name As a
 INNER
  JOIN (
        SELECT word
        FROM   table_name
        GROUP
            BY word
        HAVING Count(*) < 3
       ) As b
    ON a.word = b.word
ORDER
    BY a.cate
__________________
George
Twitter | Blog

Last edited by gvee; 06-02-09 at 08:09.
Reply With Quote
  #4 (permalink)  
Old 06-01-09, 20:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
good one george

pssst, fix your syntax error
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 06-02-09, 08:11
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Fixed-d-d-d
__________________
George
Twitter | Blog
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