Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Dear Friends,

I have three tables like tblStates(StateId, StateName) , tblCities (StateName, Id, CityName) & tblPost(Id, StateName, CityName, AdTitle, description). Now I would like to count the no.of ads posted in individual States & Cities using any bound Control.

Ex: Expected output is

Andhra Pradesh (20)
---------------
Vijayawada (5)
Guntur (5)
Visakapatnam (3)
Tirupati (5)
Kadapa (2)

Karnataka (20)
-----------
Bangalore (5)
Mangalore (5)
Mysore (5)
Belgum (5)

Tamilnadu (20)
---------
Chennai (10)
Coimbator (5)
Selum (3)
Hosur (2)

etc..

Note: StateName & CityName we can bind from tblStates & tblCities only and the count should be bind from tblPost table.

Thanks in advance.
Posted

1 solution

Why not do the counting in the database when you fetch the data.

As an example if you list states and want to have count of cities in the state as another column you can of course use grouping (GROUP BY clause) but also one easy way is to use a correlated scalar query. For example:
SQL
SELECT s.StateName,
       (   SELECT COUNT(*) 
           FROM   tblCities c
           WHERE  c.StateName = s.StateName)
FROM   tblStates s

Using the same idea I believe it would be easy to count the ads.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900