题目地址
https://leetcode.cn/problems/class-performance/description/
题目大意
编写一个查询,计算学生获得的 最高分 和 最低分 之间的 总分差(3
次作业的总和)。
代码
/* Write your T-SQL query statement below */
DECLARE @sql_dynamic NVARCHAR(MAX);
SELECT @sql_dynamic = 'SELECT MAX(total_score) - MIN(total_score) AS difference_in_score FROM (SELECT ' + STRING_AGG(COLUMN_NAME, ' + ') + ' AS total_score FROM Scores) as t1'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Scores'
AND COLUMN_NAME LIKE 'assignment%';
-- SELECT @sql_dynamic;
EXEC sp_executesql @sql_dynamic;