Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-01-2012, 11:26 AM   PM User | #1
Radeom
New Coder

 
Join Date: May 2012
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Radeom is an unknown quantity at this point
Country count

I have database names and there are column country, name, id
like this:

Sweden, John, 1
Sweden, Carl, 1
Sweden, Bart, 2
Sweden, Bart, 3
Sweden, Benny, 4

Denmark, Adam, 5
Denmark, Aden, 5
Denmark, Paula, 6
Denmark, Paula, 7

Germany, Rahel, 8
Germany, Rahel, 8

how get:
Code:
Sweden 3
Denmark 2
Germany 1
PHP Code:
SELECT countrycount(*) AS theCount FROM names GROUP BY country ORDER BY theCount DESC 
Radeom is offline   Reply With Quote
Old 12-01-2012, 12:26 PM   PM User | #2
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
Code:
select t.state,count(distinct t.name) from temp_table t
group by t.state
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search
BubikolRamios is offline   Reply With Quote
Old 12-01-2012, 12:55 PM   PM User | #3
Radeom
New Coder

 
Join Date: May 2012
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Radeom is an unknown quantity at this point
PHP Code:
SELECT countrycount(distinct name) AS theCount FROM names GROUP BY country ORDER BY theCount DESC 
result is:

Code:
Sweden 4
Denmark 3
Germany 1
because no id
it does not work:
PHP Code:
SELECT countrycount(distinct name and id) AS theCount FROM names GROUP BY country ORDER BY theCount DESC 
Radeom is offline   Reply With Quote
Old 12-01-2012, 04:48 PM   PM User | #4
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
I looked to quick.
Explain what 3, in expected result, for sweden means ?
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search
BubikolRamios is offline   Reply With Quote
Old 12-01-2012, 10:03 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
*MAYBE* you are after this?
Code:
SELECT S.country, COUNT(  DISTINCT S.minid ) AS theCount
FROM (
    SELECT country, name, MIN(id) AS minid
    FROM names
    GROUP BY country, name 
    ) AS S
GROUP BY S.country
ORDER BY S.country
???
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:16 AM.


Advertisement
Log in to turn off these ads.