Solved

How to solve this complex query.

Posted on 2012-03-27
7
238 Views
Last Modified: 2012-03-28
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.
sample-data.xlsx
0
Comment
Question by:searchsanjaysharma
  • 4
  • 3
7 Comments
 
LVL 12

Expert Comment

by:patrikt
ID: 37775228
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.
0
 

Author Comment

by:searchsanjaysharma
ID: 37775378
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)
0
 
LVL 12

Accepted Solution

by:
patrikt earned 500 total points
ID: 37775591
I understand now. You need both aggregates in one query.

For this you should use "CASED" counts.
Something like this:
select state,district,source,
count(
       CASE 
         WHEN rfeeddate between '2012-3-1' and '2012-3-31' THEN 1 
         ELSE NULL 
       END) as cnt,
count(
       CASE 
         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

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 12

Expert Comment

by:patrikt
ID: 37775605
Note:
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.
0
 

Author Comment

by:searchsanjaysharma
ID: 37776184
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.
0
 
LVL 12

Expert Comment

by:patrikt
ID: 37776240
If you want partitioned results you should look on window agregate functions also:
COUNT() OVER (PARTITION BY <>)  see syntax in help.
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 37776543
Great
0

Featured Post

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

22 Experts available now in Live!

Get 1:1 Help Now