Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 please help me with this query

Author  Topic 

jprogrammer
Starting Member

2 Posts

Posted - 2008-12-12 : 11:02:24
Please check below tables


I am trying to get number of states and districts using this query
Select
c.chcountrycode,
count(s.chstatecode) as states,
count(d.chdistrictcode) as districts
From
Country C
inner join Countrystatelist CS on c.chCountrycode = c.chCountrycode
inner join table_statelist sl on cs.statelistid = sl.statelistid
inner join table_state s on s.chstatecode = sl.chstatecode
inner join statedistrictlist sdl on sdl.chstatecode = s.chstatecode
inner join table_districtlist dl on dl.districtlistid = sdl.districtlistid
inner join table_district d on d.chdistrictcode = dl.chdistrictcode
Group by c.chcountrycode

But i am getting the result set like
this

countrycode-states - districts
US 3 3

Actually i got 3 districts and 2 states in the database.
Please help me to write this query.

Thanks in advance.

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-12-12 : 11:29:16
quote:
Originally posted by jprogrammer

Please check below tables


I am trying to get number of states and districts using this query
Select
c.chcountrycode,
count(distinct s.chstatecode) as states,
count(d.chdistrictcode) as districts
From
Country C
inner join Countrystatelist CS on c.chCountrycode = c.chCountrycode
inner join table_statelist sl on cs.statelistid = sl.statelistid
inner join table_state s on s.chstatecode = sl.chstatecode
inner join statedistrictlist sdl on sdl.chstatecode = s.chstatecode
inner join table_districtlist dl on dl.districtlistid = sdl.districtlistid
inner join table_district d on d.chdistrictcode = dl.chdistrictcode
Group by c.chcountrycode

But i am getting the result set like
this

countrycode-states - districts
US 3 3

Actually i got 3 districts and 2 states in the database.
Please help me to write this query.

Thanks in advance.

Go to Top of Page

jprogrammer
Starting Member

2 Posts

Posted - 2008-12-12 : 17:39:00
Hi sodeep,

Thanks alot. It is working now.
Thanks for your great help.

Regards,
JP
Go to Top of Page
   

- Advertisement -