SQL在选择列表中无效

时间:2016-08-23 05:32:07

标签: sql sql-server group-by

此SQL查询出现以下错误

  

专栏' StudentSIP.StartDate'因为它在选择列表中无效   不包含在聚合函数或GROUP BY中   子句。

select concat(sc.StartDate, sc.EndDate) SIPDate,
       count (sj.LOComment) WeekReviewed,
       count(sjd.WeekNo) TotalWeek,
       sum(sjj.TotalDaysRecord) TotalDaysRecord,
       count(case sjd.JournalStatusCode when 'D' then 1 else null end) PendingComplete
from StudentJournalDate sjd
left join StudentJournal sj
    on sjd.WeekNo = sj.WeekNo
left outer join
(
    select sj.WeekNo, 
           case when RTRIM(sj.Day1Journal) = '' or sj.Day1Journal is null then 0 else 1 end +
           case when RTRIM(sj.Day2Journal) = '' or sj.Day2Journal is null then 0 else 1 end +
           case when RTRIM(sj.Day3Journal) = '' or sj.Day3Journal is null then 0 else 1 end +
           case when RTRIM(sj.Day4Journal) = '' or sj.Day4Journal is null then 0 else 1 end +
           case when RTRIM(sj.Day5Journal) = '' or sj.Day5Journal is null then 0 else 1 end +
           case when RTRIM(sj.Day6Journal) = '' or sj.Day6Journal is null then 0 else 1 end +
           case when RTRIM(sj.Day7Journal) = '' or sj.Day7Journal is null then 0 else 1 end as TotalDaysRecord 
    from StudentJournal sj
) as sjj
    on sjj.WeekNo = sj.WeekNo
left outer join StudentSIP sc
    on sc.AdminNo = sjd.AdminNo

2 个答案:

答案 0 :(得分:2)

您从未告诉我们您使用的是哪个版本的SQL,但我怀疑您的 android:configChanges="orientation|keyboardHidden|screenSize" (例如SELECTCOUNT)中聚合函数的存在是数据库进入分组模式。在此模式下,您只能使用SUM中列的聚合函数或SELECT子句中出现的列。由于GROUP BY既不是聚合,也没有sc.StartDate子句,因此会出现此错误。

使此错误消失的一个快速解决方法是GROUP BY连接的开始和结束日期,即将GROUP BY添加到当前查询的末尾:

GROUP BY CONCAT(sc.StartDate, sc.ENDDate)

答案 1 :(得分:-1)

在没有group by子句的情况下使用A ggregate函数时,sql不起作用;

ex select key1,key2,count(*),sum(*) from tableA group by key1,key2