我有一个存储过程,我有一个临时表,我需要更新“day_name”列的值。循环throug临时表并更新值
这是数据的示例(有多个学生姓名):
student StudentID courseID course_name period day_name
Bray, Clifford 14088 10064 Physical Education 9GYM 1 A
Bray, Clifford 14088 10064 Physical Education 9GYM 1 B
Bray, Clifford 14088 10064 Physical Education 9GYM 1 C
Bray, Clifford 14088 10064 Physical Education 9GYM 1 D
Bray, Clifford 14088 10064 Physical Education 9GYM 2 A
Bray, Clifford 14088 10064 Physical Education 9GYM 2 B
Bray, Clifford 14088 10064 Physical Education 9GYM 2 C
Bray, Clifford 14088 10064 Physical Education 9GYM 2 D
Bray, Clifford 14088 10065 Physical Education 11GYM 3 B
Bray, Clifford 14088 10065 Physical Education 11GYM 3 D
Bray, Clifford 14088 10065 Physical Education 11GYM 5 A
Bray, Clifford 14088 10065 Physical Education 11GYM 5 C
Bray, Clifford 14088 10065 Physical Education 11GYM 6 A
Bray, Clifford 14088 10065 Physical Education 11GYM 6 B
Bray, Clifford 14088 10065 Physical Education 11GYM 6 C
Bray, Clifford 14088 10065 Physical Education 11GYM 6 D
Bray, Clifford 14088 10059 Health 9P373 7 A
Bray, Clifford 14088 10059 Health 9P373 7 B
Bray, Clifford 14088 10059 Health 9P373 7 C
我希望数据是这样的:
student studentID courseID course_name period day_name
Bray, Clifford 14088 10064 Physical Education 9GYM 1 ABCD
Bray, Clifford 14088 10064 Physical Education 9GYM 2 ABCD
Bray, Clifford 14088 10065 Physical Education 11GYM 3 BD
Bray, Clifford 14088 10065 Physical Education 11GYM 5 AC
Bray, Clifford 14088 10065 Physical Education 11GYM 6 ABCD
Bray, Clifford 14088 10059 Health 9P373 7 ABC
非常感谢提前:) 请帮忙......
答案 0 :(得分:1)
鉴于您拥有有限的日期名称列表,最简单的方法可能是明确的聚合:
select student, StudentID, courseID, course_name, period,
(max(case when day_name = 'A' then day_name else '' end) +
max(case when day_name = 'B' then day_name else '' end) +
max(case when day_name = 'C' then day_name else '' end) +
max(case when day_name = 'D' then day_name else '' end)
) as days
from table t
group by student, StudentID, courseID, course_name, period;
如果您希望表格看起来像这样,我建议您创建一个新表格。修改原始版本并不合理 - 您必须删除行以及修改或插入值。
答案 1 :(得分:1)
以此为输入:
DECLARE @STUDENTS TABLE (STUDENT VARCHAR(20), STUDENTID INT, COURSEID INT, COURSE_NAME VARCHAR(MAX), PERIOD INT, DAY_NAME CHAR(1))
INSERT @STUDENTS VALUES
('Bray, Clifford', 14088, 10064, 'Physical Education 9GYM', 1, 'A'),
('Bray, Clifford', 14088, 10064, 'Physical Education 9GYM', 1, 'B'),
('Bray, Clifford', 14088, 10064, 'Physical Education 9GYM', 1, 'C'),
('Bray, Clifford', 14088, 10064, 'Physical Education 9GYM', 1, 'D'),
('Bray, Clifford', 14088, 10064, 'Physical Education 9GYM', 2, 'A'),
('Bray, Clifford', 14088, 10064, 'Physical Education 9GYM', 2, 'B'),
('Bray, Clifford', 14088, 10064, 'Physical Education 9GYM', 2, 'C'),
('Bray, Clifford', 14088, 10064, 'Physical Education 9GYM', 2, 'D'),
('Bray, Clifford', 14088, 10065, 'Physical Education 11GYM', 3, 'B'),
('Bray, Clifford', 14088, 10065, 'Physical Education 11GYM', 3, 'D'),
('Bray, Clifford', 14088, 10065, 'Physical Education 11GYM', 5, 'A'),
('Bray, Clifford', 14088, 10065, 'Physical Education 11GYM', 5, 'C')
此查询:
SELECT STUDENT, STUDENTID, COURSEID, COURSE_NAME, PERIOD,
(SELECT DISTINCT '' + DAY_NAME
FROM @STUDENTS
WHERE S.STUDENTID = STUDENTID AND S.COURSEID = COURSEID AND S.PERIOD = PERIOD
FOR XML PATH ('')) AS DAYS
FROM @STUDENTS AS S
GROUP BY STUDENT, STUDENTID, COURSEID, COURSE_NAME, PERIOD
产生以下输出:
STUDENT STUDENTID COURSEID COURSE_NAME PERIOD DAYS
------------------------------------------------------------------------------
Bray, Clifford 14088 10064 Physical Education 9GYM 1 ABCD
Bray, Clifford 14088 10064 Physical Education 9GYM 2 ABCD
Bray, Clifford 14088 10065 Physical Education 11GYM 3 BD
Bray, Clifford 14088 10065 Physical Education 11GYM 5 AC