How to solve this complex query.

I have a table, in which dataentry is done from 48 locations field is district which are inside some states and those further belong to field called source.
The hierarchy is State,District,Source
In short,
There are 7 states, 48 districts and 3 source
I want to pick up the count  of dataentry done on date range basis and cumulative till to date.
The problem is how to pick those records from district where 0 data entry was done.
Output i need is as follows
State   District  Source   Date Entry Done Cumulative

I am using the following to get the date range data.
select state,district,source,count(*)
from mstchvs where flagr=1 and rfeeddate between '2012-3-1' and '2012-3-31'
group by state,district,source
order by 1,2,3

select state,district,source,count(*)
from mstchvs where flagr=1 and rfeeddate<='2012-3-31'
group by state,district,source
order by 1,2,3

The sample data is attached.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If I understand you need to report all State,District combinations even if there is no record in selected range (it is report 0 for it).

You have to join your table to "list" table of districts. Something as this:

select DistricstTable.state,DistricstTable.district,source,count(*)
from DistricstTable
left join mstchvs 
ON DistricstTable.state=mstchvs.state and DistricstTable.district=mstchvs.district
where flagr=1 and rfeeddate<='2012-3-31'
group by DistricstTable.state,DistricstTable.district,source
order by 1,2,3

Open in new window

You can use physical DistrictsTable or construct it from your data if it is possible.
searchsanjaysharmaAuthor Commented:
Thats done, but how to get the data within range.
Example if to a query 2 dates are there fromdate and todate
The output shoud be
State District Source Total (Within between dates) Cumulativetotal(Till todate)
I understand now. You need both aggregates in one query.

For this you should use "CASED" counts.
Something like this:
select state,district,source,
         WHEN rfeeddate between '2012-3-1' and '2012-3-31' THEN 1 
         ELSE NULL 
       END) as cnt,
         WHEN rfeeddate<='2012-3-31' THEN 1 
         ELSE NULL 
       END) as cntTill
from mstchvs where flagr=1 and 
group by state,district,source
order by 1,2,3

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Do not use date literals in form 'YYYY-MM-DD' or other locale dependent form. Parsing of literals is dependent on language settings of connection. It can cause problems.

Only safe form is ISO date exact 'YYYYMMDD'. This is language neutral.
searchsanjaysharmaAuthor Commented:
Great, now is it further possible to append the further data for same condition of dates.
and same base data, i.e state,district,source
from other tables like t1 and t2.

The idea is i want to generate the consolidated report from all the tables in horizontal output.
If you want partitioned results you should look on window agregate functions also:
COUNT() OVER (PARTITION BY <>)  see syntax in help.
searchsanjaysharmaAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.