我有这个sql,但我想要做的是让另一列有其他匹配列的总和这样......
declare @SchoolID int
declare @RoleID int
declare @match int
declare @firstname varchar
set @SchoolID=1
set @RoleID=1
SELECT
dbo.Teacher.Firstname,
dbo.School.SchoolName,
dbo.School.SchoolID,
dbo.RoleTitle.RoleTitle,
CASE WHEN dbo.School.SchoolID = @SchoolID then 1 else 0 end as [SchoolMatch],
CASE WHEN dbo.JobTitle.RoleTitleID = @RoleID then 1 else 0 end as [RoleMatch]
FROM
dbo.School INNER JOIN
dbo.Lesson ON dbo.School.SchoolID = dbo.Lesson.SchoolID INNER JOIN
dbo.RoleTitle ON dbo.Lesson.RoleTitleID = dbo.RoleTitle.RoleTitleID INNER JOIN
dbo.Teacher ON dbo.Lesson.TeacherID = dbo.Teacher.TeacherID
答案 0 :(得分:4)
您可以重复您的案例陈述并添加它们。
SELECT
.
.
CASE WHEN dbo.School.SchoolID = @SchoolID then 1 else 0 end +
CASE WHEN dbo.JobTitle.RoleTitleID = @RoleID then 1 else 0 end as [NewColumn]
答案 1 :(得分:2)
您可以这样做:
SELECT
dbo.Teacher.Firstname,
dbo.School.SchoolName,
dbo.School.SchoolID,
dbo.RoleTitle.RoleTitle,
CASE WHEN dbo.School.SchoolID = @SchoolID then 1 else 0 end as [SchoolMatch],
CASE WHEN dbo.JobTitle.RoleTitleID = @RoleID then 1 else 0 end as [RoleMatch],
(CASE WHEN dbo.School.SchoolID = @SchoolID then 1 else 0 end)+
(CASE WHEN dbo.JobTitle.RoleTitleID = @RoleID then 1 else 0 end) AS NewColumn
FROM
dbo.School INNER JOIN
dbo.Lesson ON dbo.School.SchoolID = dbo.Lesson.SchoolID INNER JOIN
dbo.RoleTitle ON dbo.Lesson.RoleTitleID = dbo.RoleTitle.RoleTitleID INNER JOIN
dbo.Teacher ON dbo.Lesson.TeacherID = dbo.Teacher.TeacherID