Sql 在select子句中添加case条件会//产生聚合函数或GROUP BY子句//错误

Sql 在select子句中添加case条件会//产生聚合函数或GROUP BY子句//错误,sql,sql-server,Sql,Sql Server,我已经定义了我的select查询,比如 SELECT Day(Date) as Day, ... Case when (SUM(GallonsPumped)*0.01 +130 <ABS(Sum(DailyVar))) Then 'Fail' Else 'pass' End as result, .... FROM [dbo].[zzz] WHERE date >=

我已经定义了我的select查询,比如

SELECT  Day(Date) as Day, 
        ... 
        Case
            when (SUM(GallonsPumped)*0.01 +130 <ABS(Sum(DailyVar))) Then 'Fail'
            Else 'pass'
        End as result,
        ....
FROM  [dbo].[zzz]
WHERE date >='2019-09-12' and date<='2019-10-09' and SiteCode='0209365' and CompanyId = 67   
order by  CAST(Date AS Date) asc
选择日期作为日期,
... 
案例

当(总和(加仑数)×0.01+130='2019-09-12'和日期时,对于聚合函数中未涉及的列,您需要一个分组依据

因此,假设您有Day、col1、col2、col3列未参与聚合,则需要按Day(Date)、col1、col2、col3分组

  SELECT  Day(Date) as Day, 
        col1, 
        col2,

        Case
            when (SUM(GallonsPumped)*0.01 +130 <ABS(Sum(DailyVar))) Then 'Fail'
            Else 'pass'
        End as result,
        col3
FROM  [dbo].[zzz]
WHERE date >='2019-09-12' and date<='2019-10-09' and SiteCode='0209365' and CompanyId = 67   
GROUP BY Day(Date), col1, col2, col3 
order by  CAST(Date AS Date) asc
选择日期作为日期,
col1,
col2,
案例

当(SUM(GallonsPumped)*0.01+130='2019-09-12'和日期)时,您需要发布完整的查询。错误可能与您的案例陈述之外的其他内容有关。请提供示例数据、所需结果,并解释您想要实现的逻辑。