views:

41

answers:

4

I am trying to count the number of rows in a table that have an area, how can I achieve this, I currently have this query written,

SELECT DISTINCT area 
  FROM cv 
 WHERE status = 'SHOW' 
   AND is_complete = 'TRUE' 
ORDER BY area ASC

This query currently returns,

area
------------------
West Yorkshire  
Lanchashire  

What I am wanting is something like,

area             Number
------------------------
West Yorkshire   19000  
Lancashire       7000
+4  A: 
select area, count(*)
from cv
where status = 'SHOW' 
    and is_complete = 'TRUE' 
group by area
RedFilter
+1  A: 
SELECT area, COUNT(area)
  FROM cv
WHERE status = 'SHOW'
  AND is_complete = 'TRUE'
  AND area IS NOT NULL
GROUP BY area
gkrogers
+3  A: 
SELECT area, COUNT(area)
  FROM cv
WHERE status = 'SHOW'
  AND is_complete = 'TRUE'
GROUP BY area

Count of area will count only non null values

Chinjoo
Good point, +1 - I'd forgotten that.
gkrogers
OTOH, when you use GROUP BY you'll get a "NULL, 0" row, which you might not want. (At least in SQL Server, MySQL may exhibit different behaviour.
gkrogers
A: 

The sql distinct will list only the diffent values in a table, you original query will need to have count() and group by as well:

SELECT DISTINCT area, count(*) 
  FROM cv
.....

group by area

alternatively, Red Filer and gkrogers answer is good as well.

VoodooChild
Don't need DISTINCT on a column you're grouping by
OMG Ponies
+1 thanks, I was trying to add on to his original attemp
VoodooChild