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 > Group by >2 columns

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-12-11, 04:03
gixig gixig is offline
Registered User
 
Join Date: Jan 2006
Posts: 5
Question Group by >2 columns

Hi all,

I'm really stuck with this table:

MAP
--------------------------
ID - CLASS - COUNTRY
1 - B - US
2 - B - CA
3 - A - CA
1 - B - PT
1 - B - CA
1 - C - US
2 - A - CA
2 - A - US
3 - B - CA

I have to query id's with same class and >2 different countries. I've tried :

select id,class,country from map group by id,class,country having count(country)>2;

But doesn't work . Any advice or clue?

Thanks!
Reply With Quote
  #2 (permalink)  
Old 05-12-11, 05:01
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Code:
SELECT id,
       class,
       country
FROM (
   SELECT id, 
          class,
          country,
          count(country) over (partition by class) as cntr_count
   FROM your_table
) t
WHERE cntr_count = 2
Reply With Quote
  #3 (permalink)  
Old 05-12-11, 05:59
gixig gixig is offline
Registered User
 
Join Date: Jan 2006
Posts: 5
Thumbs up

Thanks for your answer!!!!
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