Solved

Count Help

Posted on 2006-06-13
19
295 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift 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 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Table where row act as column 11 82
Access control a form field in Lotus Notes 3 41
Append Query Access 2010 4 61
Wordpress -  Updating All Posts on Website 3 85
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
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…

737 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