使用CASE的SQL连接条件

时间:2017-03-01 03:17:24

标签: sql sql-server sql-server-2008

我有一个查询,我用来连接多个列到一个列但是我得到'学年附近的'语法不正确'。任何想法 - 我不太确定如何在连接中结束案例

select distinct 
    bb.pk1 as 'LearnSourceKey',
    sc.pk1 as 'SISSourceKey',
    row_number () over (partition by bb.pk1 order by sc.SISCourseKey) as 'RowRank'
from
    Source.SISCourse sc
    left join source.SISTerm st on sc.TermSourceKey=st.SISTermKey
    inner join Source.COURSE_MAIN bb
        on bb.batch_uid = substring(CatalogNumber, 1, (len(CatalogNumber) - 1)) + 
            case 
                when st.TermDescription='Semester 1' 
                then 'SEM-1'  + '_' + st.AcademicYear   

2 个答案:

答案 0 :(得分:1)

这是您的查询,基本上是:

case

您的else没有when条款。因此,任何不符合NULL条件的内容都是select distinct bb.pk1 as LearnSourceKey, sc.pk1 as SISSourceKey, row_number () over (partition by bb.pk1 order by sc.SISCourseKey) as RowRank from Source.SISCourse sc left join source.SISTerm st on sc.TermSourceKey = st.SISTermKey inner join Source.COURSE_MAIN bb on st.TermDescription = 'Semester 1' and bb.batch_uid = (left(CatalogNumber, len(CatalogNumber) - 1) + st.AcademicYear ); 。这意味着您可以将其简化为:

st.AcademicYear

这(与原始查询一样)假设filmList是一个字符串。

答案 1 :(得分:0)

案例陈述应该有结束表达

select distinct 
    bb.pk1 as 'LearnSourceKey',
    sc.pk1 as 'SISSourceKey',
    row_number () over (partition by bb.pk1 order by sc.SISCourseKey) as 'RowRank'
from
Source.SISCourse sc
left join source.SISTerm st on sc.TermSourceKey=st.SISTermKey
inner join Source.COURSE_MAIN bb 
      on bb.batch_uid = substring(CatalogNumber,  1, (len(CatalogNumber) - 1)) + 
         case 
            when st.TermDescription='Semester 1' 
            then  'SEM-1'  + '_' + st.AcademicYear 
         end