Solved

How to merge these two queries where the reference table is same.

Posted on 2012-04-11
5
263 Views
Last Modified: 2012-06-22
I write two queries to get the 2 datasets equilvalent from two tables which are joined with geo table. How to get it as one set. so that we can get the one set.


Select X.State,X.District,X.Source,X.Res,Y.Cum_res From

(select geotable.state,geotable.district,geotable.source,sum(isnull(mstchvs.flagr,0)) as  res
from geotable
left join mstchvs
ON geotable.state=mstchvs.state and geotable.district=mstchvs.district and geotable.source=mstchvs.source
and flagr=1 and dateadd(dd,0,DATEDIFF(dd,0,rfeeddate)) >= CONVERT(DATETIME,@fd) and
dateadd(dd,0,DATEDIFF(dd,0,rfeeddate)) <= CONVERT(DATETIME, @td)
--rfeeddate>='2012-3-1' and rfeeddate<='2012-3-31'
group by geotable.state,geotable.district,geotable.source
) X
inner join
(
select geotable.state,geotable.district,geotable.source,sum(isnull(mstchvs.flagr,0)) as cum_res
from geotable
left join mstchvs
ON geotable.state=mstchvs.state and geotable.district=mstchvs.district and geotable.source=mstchvs.source
and flagr=1 and dateadd(dd,0,DATEDIFF(dd,0,rfeeddate)) <= CONVERT(DATETIME, @td)
group by geotable.state,geotable.district,geotable.source
) Y
on X.state = Y.state and X.district = Y.district and X.source = Y.source



Select X.State,X.District,X.Source,X.Res,Y.Cum_res From

(select geotable.state,geotable.district,geotable.source,sum(isnull(mstchvs.flagpre,0)) as  res
from geotable
left join mstchvs
ON geotable.state=mstchvs.state and geotable.district=mstchvs.district and geotable.source=mstchvs.source
and flagpre=1 and dateadd(dd,0,DATEDIFF(dd,0,prefeeddate)) >= CONVERT(DATETIME,@fd) and
dateadd(dd,0,DATEDIFF(dd,0,prefeeddate)) <= CONVERT(DATETIME, @td)
--prefeeddate>='2012-3-1' and prefeeddate<='2012-3-31'
group by geotable.state,geotable.district,geotable.source
) X
inner join
(
select geotable.state,geotable.district,geotable.source,sum(isnull(mstchvs.flagpre,0)) as cum_res
from geotable
left join mstchvs
ON geotable.state=mstchvs.state and geotable.district=mstchvs.district and geotable.source=mstchvs.source
and flagpre=1 and dateadd(dd,0,DATEDIFF(dd,0,prefeeddate)) <= CONVERT(DATETIME, @td)
group by geotable.state,geotable.district,geotable.source
) Y
on X.state = Y.state and X.district = Y.district and X.source = Y.source
0
Comment
Question by:searchsanjaysharma
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:armchair_scouse
ID: 37832065
UNION query perhaps?  If your output fields are the same in the two queries (and they are the same data types), you should be able to use a UNION.

http://www.techonthenet.com/sql/union.php
0
 

Author Comment

by:searchsanjaysharma
ID: 37832328
I dont have to fetch common rows.
I want to use CTE.
here geotable is the common table that contain 48 districts. For these districts values are entered in these two tables.
So the queries shows the result reparately with the base table as geo table.
I wanto merge that,
0
 
LVL 9

Accepted Solution

by:
armchair_scouse earned 500 total points
ID: 37832884
OK, wanting to use CTE wasn't clear from your original question, so UNION isn't for you!  I don't have any practical experience of using CTEs although I appreciate the concept as I have used table variables before.  If you want to use multiple CTEs (one for each query) in one select statement, then consider the following links:

http://www.4guysfromrolla.com/webtech/071906-1.shtml
http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/ (see response comment from Tejas Shah as well as main post)

Hope this helps or at least provides a pointer to where you wish to go!
0
 

Author Comment

by:searchsanjaysharma
ID: 37833170
Can you convert my query with cte
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 37837483
ok
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

775 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