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 > PostgreSQL > SQL Query for finding items of 2 types

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-10-12, 15:10
learn4life learn4life is offline
Registered User
 
Join Date: May 2012
Posts: 7
SQL Query for finding items of 2 types

Have table of form:

ID name type
--- ------- ------
1 ad v
2 ba c
2 ba v
1 ad v
3 ca c
4 da v
1 ad c

Suppose I'd like to find the records in which an id has 2 or more type, which query could I use.

For example, id can be of type c or v. Which query would return the ids which had more than 1 type?
Reply With Quote
  #2 (permalink)  
Old 05-10-12, 15:31
imex imex is offline
Registered User
 
Join Date: Apr 2012
Posts: 178
Try:

Code:
select ID from MyTable
group by ID
having COUNT(*) > 1
Hope this helps.
Reply With Quote
  #3 (permalink)  
Old 05-10-12, 16:32
learn4life learn4life is offline
Registered User
 
Join Date: May 2012
Posts: 7
Question

Thanks for the response. However, would the results include all records with duplicate types?

For ex,

ID name type
--- ------- ------
1 ad v
2 ba c
2 ba c
1 ad v
3 ca c
4 da v
1 ad c

I'd only want to return the ids that had more than one type of type. So in this case, ID 1 is the only ID associated with types c and v, so it should be the only result returned.
Reply With Quote
  #4 (permalink)  
Old 05-10-12, 16:49
imex imex is offline
Registered User
 
Join Date: Apr 2012
Posts: 178
Try:

Code:
select ID
from (select distinct ID, typecol from MyTable) as t
group by ID
having COUNT(*) > 1
Hope this helps.
Reply With Quote
  #5 (permalink)  
Old 05-11-12, 02:46
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,758
Code:
SELECT id
 FROM  MyTable
 GROUP BY
       id
 HAVING
       MIN(type) < MAX(type)
;
Reply With Quote
  #6 (permalink)  
Old 05-11-12, 19:11
learn4life learn4life is offline
Registered User
 
Join Date: May 2012
Posts: 7
Additional

Now, what if I wanted to order the results, by ids that fell into, 1, or both of the categories and list these categories?
Reply With Quote
  #7 (permalink)  
Old 05-12-12, 00:04
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,758
Now, completely different requirements.
Is it homework?

Try something by yourself, even if you couldn't get final results,
and show your trial queries, results of the queries and required results.
Then I would help you.
Reply With Quote
  #8 (permalink)  
Old 05-12-12, 00:08
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,758
Are possible types c or v only?
Reply With Quote
Reply

Tags
duplicate, postgres, query, records, sql

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