CourseList表在SQL中的总计数

时间:2018-11-19 17:52:01

标签: sql

在计算我从表中学习的代数时遇到问题。

我想显示SchoolID Alg1列并保留Section_Number,即:

110      14

这是我的代码,并且已附加输出的图像:

SELECT
  DISTINCT
  cc.SchoolID
 ,e.alg1
 ,cc.Section_Number
FROM
  dbo.CRDC_2017_Course_Enrollments AS e
LEFT JOIN
  PSExtracts.PS_SCS_CC_Export AS cc
    ON
    e.StudentID = cc.StudentID
WHERE
  e.alg1 NOT LIKE 'NULL'
  AND (CONVERT(DATE, cc.DateEnrolled, 120) <= '2017-10-01')
  AND (CONVERT(DATE, cc.DateLeft, 120) >= '2017-10-01')
GROUP BY
  cc.SchoolID
 ,e.alg1
 ,cc.Section_Number;

CourseList

1 个答案:

答案 0 :(得分:0)

首先,您排除空值的条件:

e.alg1 not like 'NULL'

应该是:

e.alg1 is not null

因为null值不等于字符串'null'


您的其余问题尚不完全清楚,但也许以下是您要实现的目标:

select cc.SchoolID, Count(e.alg1)
from 
    dbo.CRDC_2017_Course_Enrollments e left join PSExtracts.PS_SCS_CC_Export cc
    on e.StudentID = cc.StudentID 
where
    e.alg1 is not null
    and 
    (CONVERT(date, cc.DateEnrolled, 120) <= '2017-10-01') AND 
    (CONVERT(date, cc.DateLeft, 120) >= '2017-10-01') 
group by    
    cc.SchoolID