将所有列与0替换为1

时间:2018-02-12 14:57:19

标签: sql sql-server

我每月都会有学生上学。在最后一栏中,我将以 -

的格式显示Total类

(参加的课程总数)/(持有的课程总数)

所以,sum()会给我参加的课程总数,但是我如何获得总课程,因为出勤率可以是0到4之间的任何整数?

出勤表的样本数据: -

Total size:    734930879995888 B (Total open files size: 537967073 B)
 Total dirs:    1501316
 Total files:   113743394
 Total symlinks:                0 (Files currently being written: 137)
 Total blocks (validated):      109063040 (avg. block size 6738587 B) (Total open file blocks (not validated): 133)
 Minimally replicated blocks:   109063040 (100.00001 %)
 Over-replicated blocks:        0 (0.0 %)
 Under-replicated blocks:       0 (0.0 %)
 Mis-replicated blocks:         0 (0.0 %)
 Default replication factor:    3
 Average block replication:     3.001944
 Corrupt blocks:                0
 Missing replicas:              0 (0.0 %)
 Number of data-nodes:          103
 Number of racks:               1
FSCK ended at Mon Feb 12 10:09:10 CET 2018 in 1608344 milliseconds

学生表的样本数据: -

+--------------+-------------+----------+------------+------------+-----------+
| EnrollmentNo | SubjectCode |   Date   | Attendance | CourseCode | FacultyId |
+--------------+-------------+----------+------------+------------+-----------+
| DDU-320/12   | CSHT101     | 2/1/2018 |          0 | BSCCS      | Fac101    |
| DDU-320/12   | CSHT102     | 2/1/2018 |          1 | BSCCS      | Fac101    |
| DDU-320/12   | CSHT203     | 2/1/2018 |          1 | BSCCS      | Fac101    |
| DDU-320/12   | CSHT101     | 2/2/2018 |          1 | BSCCS      | Fac101    |
| DDU-320/12   | CSHT102     | 2/2/2018 |          1 | BSCCS      | Fac101    |
| DDU-320/12   | CSHT203     | 2/2/2018 |          1 | BSCCS      | Fac101    |
| DDU-320/12   | CSHT101     | 2/3/2018 |          2 | BSCCS      | Fac101    |
| DDU-320/12   | CSHT102     | 2/3/2018 |          1 | BSCCS      | Fac101    |
| DDU-320/12   | CSHT203     | 2/3/2018 |          1 | BSCCS      | Fac101    |
| DDU-320/12   | CSHT101     | 2/5/2018 |          1 | BSCCS      | Fac101    |
| DDU-320/12   | CSHT102     | 2/5/2018 |          0 | BSCCS      | Fac101    |
| DDU-320/12   | CSHT203     | 2/5/2018 |          1 | BSCCS      | Fac101    |
+--------------+-------------+----------+------------+------------+-----------+

UserDetails表的示例数据: -

+--------------+--------+-------------+------------+------+
| EnrollmentNo | RollNo | CollegeCode | CourseCode | Year |
+--------------+--------+-------------+------------+------+
| DDU-320/12   |     38 | DDUC        | BSCCS      | 2012 |
+--------------+--------+-------------+------------+------+

输出: -

Output

在输出中,总计列应包含 4/5 而不是4/4。

以下是我每月出席的代码。

+--------------+-----------+----------+--------+---------+---------+
| EnrollmentNo | FirstName | LastName | Gender | Address |  Phone  |
+--------------+-----------+----------+--------+---------+---------+
| DDU-320/12   | Suyash    | Gupta    | Male   | Lucknow | 9817271 |
+--------------+-----------+----------+--------+---------+---------+

DDL和消耗品样本数据(由Larnu提供)

CREATE PROCEDURE GET_ATTENDANCE_REPORT_FOR_FACULTY
@startdate DATE,  
@enddate DATE,
@collegecode nvarchar(10),
@coursecode nvarchar(10),
@subjectcode nvarchar(10)

AS BEGIN

DECLARE @query as varchar(MAX);

with cte (startdate) as 
(
    select @startdate startdate
    union all 
    select dateadd(DD, 1, startdate) 
    from cte
    where startdate < @enddate
)

select @query = coalesce(@query, '') + 
              N',coalesce(MAX(CASE when A.[Date] = ''' + 
              cast(cte.startdate as nvarchar(20)) + 
              N''' THEN Convert(varchar(10),A.[Attendance]) end), ''-'') ' + 
              quotename(convert(char(2), cte.startdate,104))
from cte
where datename(weekday, cte.startdate) <> 'Sunday';

set @query = 'Select S.RollNo AS [Roll No],Concat(FirstName,'' '',LastName) Name' + @query + ',Concat(sum(Attendance),''/'',count(Attendance)) Total
              from Attendance A, Student S, UserDetails U
              where A.EnrollmentNo=S.EnrollmentNo and S.EnrollmentNo=U.userID and S.CollegeCode=''' + @collegecode + ''' and A.CourseCode=''' + @coursecode + ''' and A.SubjectCode =''' + @subjectcode +''' 
              and A.Date between ''' + Convert(nvarchar,@startdate) + ''' and ''' + Convert(nvarchar,@enddate) + '''
              Group By S.RollNo,U.FirstName,U.LastName';


Execute (@query)
END

1 个答案:

答案 0 :(得分:0)

这是一个简单的解决方案(礼貌Larnu)。

SUM(CASE Attendance WHEN 0 THEN 1 ELSE Attendance END)