0

所以我试图得到一个有条件的计数的总和(与查询的其余部分不同)

这是我到目前为止所拥有的:

SELECT studemo.ssid                                 AS [SSID], 
       stustat.graden                               AS Grade, 
       studemo.lastname                             AS [Last Name], 
       studemo.firstname                            AS [First Name], 
       (SELECT Count(*) 
        WHERE  attend.attendc IN ( 'E', 'G', 'H', 'I', 
                                   'J', 'L', 'M', 'P', 
                                   'Q', 'V', 'X' )) AS [Days Absent], 
       attend.attendc                               AS [Attendance Code], 
       attend.ddate                                 AS [Date] 
FROM   track, 
       stustat, 
       stusched, 
       studemo, 
       attend 
WHERE  studemo.suniq = stustat.suniq 
       AND attend.scduniq = stusched.scduniq 
       AND studemo.suniq = stusched.suniq 
       AND stustat.trkuniq = track.trkuniq 
       AND track.schoolc = '408' 
       AND track.schyear = '2013' 
       AND stustat.edate >= '08/21/2012' 
       AND stustat.xdate IS NULL 
ORDER  BY [last name], 
          [first name], 
          attend.ddate 

这将为我提供正确的信息,而不是以我试图获取它的方式。每个出勤代码都会给我一行。我希望做的只是将我从 count(*) 子查询中获得某些东西的次数加起来。我以大约 100 种不同的方式尝试了 sum(),但我无法得到它。有什么建议么?

4

1 回答 1

3

尝试更换线路:

(select count(*) where attend.attendc IN ('E','G','H','I','J','L','M','P','Q','V','X')) AS [Days Absent],

使用窗口功能:

sum(case when attend.attendc IN ('E','G','H','I','J','L','M','P','Q','V','X') then 1 else 0 end) over () as [Days Absent]

那是缺席的总天数。我猜你想要这个SSID。在这种情况下,使用partition by

sum(case when attend.attendc IN ('E','G','H','I','J','L','M','P','Q','V','X') then 1 else 0 end)
          over (partition by SSID) as [Days Absent]
于 2013-05-10T18:09:33.213 回答