0

我有以下查询,我想获取我的数据的总和计数

SELECT  
  TI.[text] as zone,
  YEAR (ER.Inserted) as [Year], 
  SUM(CONVERT(INT,DRT.RDRT)) as RDRT,
  SUM(CONVERT(INT,DRT.FACT)) as FACT ,
  SUM(CONVERT(INT,DRT.ERU))  as ERU,
  (
     SELECT COUNT(ER1.ReportID) 
     FROM dbo.EW_Reports ER1
     INNER JOIN  dbo.EW_Report_InformationManagement ERI ON ER1.ReportID = ERI.ReportID
     INNER JOIN EW_Report_Country ERC1 ON   ER1.ReportID = ERC1.ReportID 
     INNER JOIN ApplicationDB.dbo.Country C1 ON ERC1.CountryID = C1.countryId 
     INNER JOIN ApplicationDB.dbo.Region R1 ON C1.regionId = R1.regionId
     INNER JOIN ApplicationDB.dbo.Zone Z1  ON R1.zoneId = Z1.zoneId 
     WHERE ERI.EmergencyAppeal IS NOT NULL
      AND (YEAR ( ER1.Inserted) = YEAR ( ER.Inserted))
      AND Z1.zoneId =  Z.zoneId     
   ) as emergencyAppeals    
FROM     EW_Reports ER 
INNER JOIN EW_DisasterResponseTools DRT ON   ER.ReportID = DRT.ReportID 
INNER JOIN EW_Report_Country ERC ON   ER.ReportID = ERC.ReportID  
INNER JOIN ApplicationDB.dbo.Country C ON ERC.CountryID = c.countryId 
INNER JOIN ApplicationDB.dbo.Region R ON c.regionId = R.regionId
INNER JOIN ApplicationDB.dbo.Zone Z ON R.zoneId = Z.zoneId 
INNER JOIN ApplicationDB.dbo.Translation T ON Z.translationId = T.translationId
INNER JOIN ApplicationDB.dbo.TranslationItem TI ON T.translationId = TI.translationId
INNER JOIN EW_lofDisasterTypes D ON ER.DisasterTypeID = D.TranslationID AND D.LanguageID = 1 AND TI.languageID = 1      
WHERE (YEAR ( ER.Inserted) IN (2011,2012))
GROUP BY TI.[text], YEAR (ER.Inserted)  

但它给出以下错误

列“ApplicationDB.dbo.Zone.zoneId”在选择列表中无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。

请帮助我如何解决此错误。

4

2 回答 2

0

您的表中已经有太多 ApplicationDB.dbo.Zone.zoneId 记录

只需在组中添加 ApplicationDB.dbo.Zone.zoneId 即可解决问题

Select ....
.....
GROUP BY TI.[text], YEAR (ER.Inserted) ,ApplicationDB.dbo.Zone.zoneId

对于您的问题,为什么您需要在我的组中添加 ApplicationDB.dbo.Zone.zoneId ,因为我在您的子查询中使用它,这是因为您在子查询中执行外部条件

SELECT
----
(
SELECT
-----
INNER JOIN ApplicationDB.dbo.Zone Z1  ON R1.zoneId = Z1.zoneId 
WHERE
----
AND Z1.zoneId =  Z.zoneId 
)
-----
INNER JOIN ApplicationDB.dbo.Zone Z ON R.zoneId = Z.zoneId
WHERE (YEAR ( ER.Inserted) IN (2011,2012)) 
------

请注意,您在不同年份有一个条件

所以你的数据流可能是这样的

ZoneID    Years     Record
  1       2011        1000
  1       2012        1000

相同的区域 id 包含不同的年份,没有适当的分组,sql 无法对年份列进行分组

于 2013-10-01T09:39:55.930 回答
0

它显示错误,因为您在子查询 where 条件中使用了它

您需要在 group by 中添加 ApplicationDB.dbo.Zone.zoneId

于 2013-10-01T09:43:46.580 回答