Solved

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

Posted on 2012-04-11
5
266 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

Suggested Solutions

Title # Comments Views Activity
Pivot Query Problem 9 47
SQL 2005 - Memory Table Column Names 11 75
Need help with a query 6 76
display data in text field from data base for updating 6 65
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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