?
Solved

Count Help

Posted on 2006-06-13
19
Medium Priority
?
297 Views
Last Modified: 2008-03-06
I want to display rows eg - displays number of properties for region

Auckland (12)
Wellington (2)
Christchurch (10)

1.
select distinct state from pcode

2.
SELECT     COUNT(PROPno) AS Expr1
FROM         Properties
WHERE     (state = @city)

@city = select statement 1


Combine 1 and 2 to get selected result.

0
Comment
Question by:skiltz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
19 Comments
 
LVL 19

Expert Comment

by:NickUpson
ID: 16898040
select state, count(*) from properties group by state
0
 
LVL 7

Author Comment

by:skiltz
ID: 16898177
But state might not exsist in properties
0
 
LVL 25

Expert Comment

by:jrb1
ID: 16898258
SELECT     state, COUNT(PROPno) AS Expr1
FROM         Properties
WHERE     state in (select distinct state from pcode)
group by state
0
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 
LVL 7

Author Comment

by:skiltz
ID: 16898412
same as above there are 30 states but presently there are only 8 states which have properties...

idea I would like to return all 30 states

Auckland (0)
Christchurch (3)
bl barh (0)
0
 
LVL 25

Expert Comment

by:jrb1
ID: 16898538
select pcode.state, sum(case when properties.state is null then 0 else 1) count
from pcode
left outer join properties
on pcode.state = properties.state
group by pcode.state
0
 
LVL 25

Expert Comment

by:jrb1
ID: 16898545
sorry...left out the end

select pcode.state
      , sum(case when properties.state is null then 0 else 1 end) as count
from pcode
left outer join properties
on pcode.state = properties.state
group by pcode.state
0
 
LVL 7

Author Comment

by:skiltz
ID: 16898677
Ok cool almost there... so this returns all the states but the count numbers are incorrect..

if I do

 SELECT     COUNT(state) AS Expr1
FROM         Properties
WHERE     (state = 'ashburton')

there I get 3 records....but your state above shows 75 :(

0
 
LVL 25

Expert Comment

by:jrb1
ID: 16898798
What counts do these give?

select count(*) from Properties where state = 'ashburton'

select count(*) from pcode where state = 'ashburton'

select count(PROPno) from Properties where state = 'ashburton'
0
 
LVL 7

Author Comment

by:skiltz
ID: 16899012
3
25
3

Right because in table pcode 'ashburton' is there 25 times because 'Ashburton' has many suburbs eg.. so we need a a DISTINCT?
0
 
LVL 25

Expert Comment

by:jrb1
ID: 16899725
select a.state, sum(case when properties.state is null then 0 else 1) count
from (select distinct state from pcode) a
left outer join properties
on a.state = properties.state
group by a.state
0
 
LVL 7

Author Comment

by:skiltz
ID: 16899805

Error in list of function arguments: ')' not recognized.
Unable to parse query text.

All should it be 1) as count?

0
 

Expert Comment

by:paulo111
ID: 16901447
Yeah and I beleive you may also have to wrap it in brackets [count].

Could be wrong.

sum(case when properties.state is null then 0 else 1) as [YourCount]
0
 
LVL 3

Assisted Solution

by:RickBeebe
RickBeebe earned 80 total points
ID: 16902152
Need to "END" your case statement

select a.state, sum(case when properties.state is null then 0 else 1 END) [count]
from (select distinct state from pcode) a
left outer join properties
on a.state = properties.state
group by a.state
0
 
LVL 25

Accepted Solution

by:
jrb1 earned 240 total points
ID: 16902409
Yes, RickBeebe is correct.  I was missing an end, but it has to be specified.  No brackets on the column alias.

select a.state, sum(case when properties.state is null then 0 else 1 end case) as count
from (select distinct state from pcode) a
left outer join properties
on a.state = properties.state
group by a.state
0
 
LVL 32

Expert Comment

by:awking00
ID: 16904373
I don't think you can use the word "count" as an alias since it is a reserved keyword.
0
 
LVL 25

Expert Comment

by:jrb1
ID: 16905252
ah, you could be right.  probably with the brackets it would be ok. but CNT would be just as good.
0
 
LVL 7

Author Comment

by:skiltz
ID: 16905910
Thanks heaps for your help... this worked in the  end

SELECT     a.state, SUM(CASE WHEN properties.state IS NULL THEN 0 ELSE 1 END) AS count
FROM         (SELECT DISTINCT state
                       FROM          pcode) AS a LEFT OUTER JOIN
                      Properties ON a.state = Properties.state
GROUP BY a.state
0
 
LVL 7

Author Comment

by:skiltz
ID: 16907616
how do I then combine the fields?

a.state + count as combined?

0
 
LVL 25

Expert Comment

by:jrb1
ID: 16909077
just concatenate:

SELECT     a.state + ' (' + SUM(CASE WHEN properties.state IS NULL THEN 0 ELSE 1 END) + ')'
FROM         (SELECT DISTINCT state
                       FROM          pcode) AS a LEFT OUTER JOIN
                      Properties ON a.state = Properties.state
GROUP BY a.state
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question