基于案例陈述加入和选择

时间:2018-04-03 14:25:16

标签: sql-server

我有一个'select'代码段,它将多个表连接在一起。作为一个连接条件的prt,我想基于case语句连接表。我的伪如下:

case when @sample = 'type A'
then 
...
LEFT JOIN lookon lo ON lo.lookid = g.text2 AND lo.lookcat = 'Transaction'
inner join logchange logc on mc.bvin=lobg.bvin
join (
select ord=1, mcdord=1, bvin, cvbvin from @temp t where rowtype = 'ADD' union all
select ord=4, mcdord=1, bvin, cvbvin from @temp t where rowtype like 'EDIT' union all
select ord=2, mcdord=1, bvin, cvbvin from @temp t where rowtype like 'DEL' and rowtype not like 'EDIT' union all
select ord=9, mcdord=1, bvin, cvbvin from @temp t where rowtype like 'CLO'
    ) A1 on A1.bvin=mc.bvin and isnull(A1.cvbvin,0) = isnull(mc.cvbvin,0)

When @sample = 'type B'
then 
...
LEFT JOIN lookon lo ON lo.lookid = g.text2 AND lo.lookcat = 'Transaction'
join ( 
    select ord=1, bvin, cvbvin from @temp t where rowtype = 'ADD' union all
    select ord=4, bvin, cvbvin from @temp t where rowtype like 'EDIT' union all
    select ord=2, bvin, cvbvin from @temp t where rowtype like 'DEL' and rowtype not like 'EDIT' 
    ) A2 on A2.bvin=mc.bvin 
END

以下是我的代码。我没有收到任何错误,但我没有收到@sample ='type B'的任何记录。

  LEFT JOIN lookon lo ON lo.lookid = g.text2 AND lo.lookcat = 'Transaction'
join (
select ord=1, mcdord=1, bvin, cvbvin from @temp t where rowtype = 'ADD' union all
select ord=4, mcdord=1, bvin, cvbvin from @temp t where rowtype like 'EDIT' union all
select ord=2, mcdord=1, bvin, cvbvin from @temp t where rowtype like 'DEL' and rowtype not like 'EDIT' union all
select ord=9, mcdord=1, bvin, cvbvin from @temp t where rowtype like 'CLO'
    ) A1 on A1.bvin=mc.bvin and isnull(A1.cvbvin,0) = isnull(mc.cvbvin,0) and @sample = 'type A'
join ( 
    select ord=1, bvin, cvbvin from @temp t where rowtype = 'ADD' union all
    select ord=4, bvin, cvbvin from @temp t where rowtype like 'EDIT' union all
    select ord=2, bvin, cvbvin from @temp t where rowtype like 'DEL' and rowtype not like 'EDIT' 
    ) A2 on A2.bvin=mc.bvin and @sample = 'type B'
where exists(select 1 from logchange logc where mc.bvin = logc.bvin )

有任何帮助吗?谢谢!

2 个答案:

答案 0 :(得分:1)

尝试使用LEFT JOIN更改简单的JOIN子句。

  LEFT JOIN lookon lo ON lo.lookid = g.text2 AND lo.lookcat = 'Transaction'
LEFT JOIN (
select ord=1, mcdord=1, bvin, cvbvin from @temp t where rowtype = 'ADD' union all
select ord=4, mcdord=1, bvin, cvbvin from @temp t where rowtype like 'EDIT' union all
select ord=2, mcdord=1, bvin, cvbvin from @temp t where rowtype like 'DEL' and rowtype not like 'EDIT' union all
select ord=9, mcdord=1, bvin, cvbvin from @temp t where rowtype like 'CLO'
    ) A1 on A1.bvin=mc.bvin and isnull(A1.cvbvin,0) = isnull(mc.cvbvin,0) and @sample = 'type A'
LEFT JOIN ( 
    select ord=1, bvin, cvbvin from @temp t where rowtype = 'ADD' union all
    select ord=4, bvin, cvbvin from @temp t where rowtype like 'EDIT' union all
    select ord=2, bvin, cvbvin from @temp t where rowtype like 'DEL' and rowtype not like 'EDIT' 
    ) A2 on A2.bvin=mc.bvin and @sample = 'type B'
where exists(select 1 from logchange logc where mc.bvin = logc.bvin )

答案 1 :(得分:0)

我修改了我的代码如下,它帮助了我。所以第二组连接与第一组连接几乎相同。因此我删除了它。 Alsi我编辑了where条件以适应两个样本值。以下是我的代码:

LEFT JOIN lookon lo ON lo.lookid = g.text2 AND lo.lookcat = 'Transaction'
    LEFT JOIN (
    select ord=1, mcdord=1, bvin, cvbvin from @temp t where rowtype = 'ADD' union all
    select ord=4, mcdord=1, bvin, cvbvin from @temp t where rowtype like 'EDIT' union all
    select ord=2, mcdord=1, bvin, cvbvin from @temp t where rowtype like 'DEL' and rowtype not like 'EDIT' union all
    select ord=9, mcdord=1, bvin, cvbvin from @temp t where rowtype like 'CLO'
        )A1 on A1.bvin=mc.bvin and isnull(A1.cvbvin,0) = isnull(mc.cvbvin,0)
where (exists(select 1 from logchange logc where mc.bvin = logc.bvin )and @sample = 'typeA') or @sample = 'typeB'