Solved

How to solve this complex query.

Posted on 2012-03-27
7
239 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql calculate reminders 11 73
format nvarchar field as mm/dd/yyyy 4 68
SQL Trigger selecting another database 4 37
Upgrading SQL 2005 Express to 2008 R2 Express 31 89
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 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