在SQL中将多个结果行合并为一个

时间:2014-05-15 21:04:30

标签: sql sql-server

我有这个SQL:

SELECT [Course Section], [Instructor Name], [Respondent Code], [1], [2], [3], [4], [5]
FROM [sirssoctonlineforms].[dbo].[Denormalized_V]
where term = 'ss14' and subject = 'iss' and course like '%330%'
order by subject, course, [course section], [respondent code]

结果是:

Respondent Code     1         2              3              4              5
1281172             Average   Above Average  Above Average  Above Average  NULL
1281172             NULL      NULL           NULL           NULL           Average

我想看到的是:

Respondent Code     1         2              3              4              5
1281172             Average   Above Average  Above Average  Above Average  Average

我有什么方法可以做到这一点吗?

1 个答案:

答案 0 :(得分:2)

SELECT [Course Section], [Instructor Name], [Respondent Code], 
       max([1]) as [1], 
       max([2]) as [2], 
       max([3]) as [3], 
       max([4]) as [4], 
       max([5]) as [5]
FROM [sirssoctonlineforms].[dbo].[Denormalized_V]
where term = 'ss14' and subject = 'iss' and course like '%330%'
group by [Course Section], [Instructor Name], [Respondent Code]
order by subject, course, [course section], [respondent code]