azz845
azz845

Reputation: 33

how to merge two select queries?

I solved that query... but now I have to merge these two queries:

(SELECT Zila,
COUNT(AbhidayMaster.OfficeRegId) As AbhidayPaidSansthan,
SUM(AbhidayMaster.TotalEmp) As TotalWorkerPaid
FROM PanjikaranMaster, AbhidayMaster
WHERE PanjikaranMaster.OfficeRegId=AbhidayMaster.OfficeRegId
AND AbhidayMaster.DipositDate Between ('2012-06-22') AND ('2012-09-19') GROUP BY Zila)



Select 
((SELECT count(distinct OfficeRegId) FROM PanjikaranMaster)
 -
(SELECT count(distinct OfficeRegId) FROM AbhidayMaster)) As AbhidayNotPaidSansthan

Upvotes: 1

Views: 181

Answers (1)

marc_s
marc_s

Reputation: 754408

How about:

SELECT 
   Zila, 
   COUNT(AbhidayMaster.OfficeRegId) As AbhidayPaidSansthan ,
   SUM(AbhidayMaster.TotalEmp) As TotalWorkerPaid
FROM 
   PanjikaranMaster
INNER JOIN
   AbhidayMaster ON PanjikaranMaster.OfficeRegId = AbhidayMaster.OfficeRegId
WHERE
   AbhidayMaster.DipositDate BETWEEN '20120622' AND '20120919'
GROUP BY 
   Zila

I changed your JOIN syntax to use the proper ANSI/ISO standard JOINs - an explicit INNER JOIN with the JOIN condition right there where it belongs (see Bad habits to kick : using old-style JOINs to learn why the "old-style" JOIN is a really bad idea and should be avoided).

I also changed your date string literals to be language- and regional settings-safe - the format is YYYYMMDD (no dashes!)

Upvotes: 2

Related Questions