如何合并两个选择查询?

时间:2012-09-24 06:23:30

标签: c# sql-server

我解决了这个问题...但现在我必须合并这两个查询:

(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

1 个答案:

答案 0 :(得分:2)

怎么样:

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

我更改了你的JOIN语法,使用正确的ANSI / ISO标准JOIN - 一个显式的INNER JOIN,其中JOIN条件就在那里(参见Bad habits to kick : using old-style JOINs来了解为什么“旧式”加入是一个非常糟糕的主意,应该避免。)

我还将日期字符串文字更改为语言和区域设置 - 安全 - 格式为YYYYMMDD无破折号!)