Solved

Count Help

Posted on 2006-06-13
19
291 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
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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 20 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 60 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 31

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now