Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Count Help

Posted on 2006-06-13
19
Medium Priority
?
298 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

636 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