选择查询中的Group By子句错误

时间:2014-09-27 06:10:15

标签: sql postgresql

以下是我在PostgreSQL中的select查询

SELECT 
        gtab12.AcGrCode,
        gtab12.AcId,
        gtab12.AcName,
        gtab11.AcgrName,
        gtab16.VrDate,
        gtab16.PDC,
        cast(gtab16.VrDate as char(12)) as vdate,
        gtab16.VrNo,
        gtab16.Refno,
        gtab02.VrName,
        gtab17.Narr,
        CASE WHEN gtab16.VrId = 6 THEN 0::decimal ELSE gtab17.Dr::decimal END AS Dr,
        CASE WHEN gtab16.VrId = 6 THEN 0::decimal ELSE  gtab17.Cr::decimal  END AS Cr,
        gtab16.AcyrId, gtab16.VrId,  
        coalesce(SUM(gtab17.Dr), 0::money) AS OpDr,
        coalesce(SUM(gtab17.Cr), 0::money) AS OpCr ,
        coalesce(SUM(gtab17.Dr), 0::money)AS OpperDr,
        coalesce(SUM(gtab17.Cr), 0::money)AS OpperCr, 
        gtab47.AreaName,
        gtab16.JrmId  
     FROM 
        gtab16
        INNER JOIN  gtab02 ON gtab16.VrId = gtab02.VrId 
        INNER JOIN gtab17  ON gtab16.JrMId = gtab17.JrmId 
        INNER JOIN gtab12 ON gtab17.AcId = gtab12.AcId 
        INNER JOIN gtab11 ON gtab12.AcGrCode = gtab11.AcgrCode 
        INNER JOIN gtab01 ON gtab16.AcyrId = gtab01.AcYrId 
        LEFT OUTER JOIN  gtab22 ON gtab16.RepId = gtab22.RepId 
        LEFT OUTER JOIN gtab47 ON gtab12.AreaId = gtab47.AreaId  
     WHERE   
       (gtab17.AcId = gtab12.AcId) and gtab16.BranchID = 1 And vrdate Between  
       Cast('2014-04-01' AS timestamp) AND Cast('2014-09-27' AS timestamp) AND  
       (gtab16.AcYrId = 7) AND gtab16.VrId <> 6 And gtab12.acid <> 0

执行时遇到此错误

  

错误:列&#34; gtab12.acgrcode&#34;必须出现在GROUP BY子句中   用于聚合函数


注意:在修改此查询之前,aggreagte函数正在从子选择中收集,请参阅here

1 个答案:

答案 0 :(得分:0)

您在某些列中使用聚合函数(SUM),因此您需要在其他列上使用聚合函数或按列分组。

再见,大卫。