使用WHERE CASE选择某些行所需的SQL帮助

时间:2015-02-17 19:05:27

标签: sql sql-server-2005

  

我的第一篇文章,请耐心等待。

     

我的数据集

 t1.c1   t1.c2   t2.c1   t2.c2   t2.c3  t3.c1   t3.c2   t1.C3
 ------------------------------------------------------------
2551770  87772   87772   82272   TEST    P       922     922
2551770  87772   87772   K0328   TEST    P       922     922
2551770  99396   99396   99396   PREV    P       922     922
2809700  93000   93000   93000   ELEC    M       310     310
2809700  99397   99397   99397   PREV    M       310     310
2809700  99397   99397   G0439   PREV    M       310     310
  

期望输出

t1.C1     t1.C2  t2.C1  t2.C2  t2.C3  t3.C1  t1.C3
---------------------------------------------------     
2551770   87772  82272  82272  TEST    P     82272 (from t1.C2)
2551770   99396  99396  99396  PREV    P     99396 (from t1.C2)
2809700   93000  93000  93000  ELEC    M     93000 (from t1.C2)
2809700   99397  99397  G0439  PREV    M     G0439 (from t2.C2)
  

我知道必须在Where子句中使用某种Case,但我是   在语法上遇到困难。我需要一些帮助来获得   期望的输出!我列出了下面的逻辑。

     

逻辑如下:

1. If t3.C1 != 'M'
a. If t1.C2 = t2.C2 then select t1.C2
b. If t1.C2 != t2.C2 then do not retrieve this row
2. If t3.C1 = 'M'
a. If t1.C2 = t2.C2 And t2.C3 is not like 'PREV' then select t1.C2
b. If t1.C2 = t2.C2 And t2.C3 is like 'PREV' then do not retrieve this row
c. If t1.C2 != t2.C2 And t2.C3 is like 'PREV' then select t2.C2

>

  

而且,我正在考虑像这样的代码,但我对我的案例感到迷茫:

Select Distinct*
From t1 Inner Join t2 On t1.C2 = t2.C1 
Inner Join t3 On t1.C3 = t3.C2
Where  
  Case When t3.C1 != 'M' Then 
         Case When t1.C2 = t2.C2 Then t1.C2
              When t1.C2 != t2.C2 then /* do not retreive this row */
  Case When t3.C1 = 'M' Then
       Case When t1.C2 = t2.C2 And t2.C3 is not like 'PREV' Then t1.C2
        When t1.C2 = t2.C2 And t2.C3 is like 'PREV' then /* do not retreive this row */
        When t1.C2 != t2.C2 And t2.C3 is like 'PREV' Then t2.C2
End     

2 个答案:

答案 0 :(得分:1)

您的select子句应使用case语句进行过滤:

SELECT Case When t3.c1 != 'M' or t1.C2 = t2.C2 
       Then t1.C2 ELSE t2.C2 END

然后在你的where语句中你有这样的东西:

WHERE (t3.c1 != 'M' and t1.C2 = t2.C2) or 
      (t3.c1 = 'M' and 
      ((t1.C2 = t2.C2 And t2.C3 != 'PREV') or 
       (t1.C2 != t2.C2 And t2.C3 = 'PREV'))

如果您没有使用通配符,那么就没有太多理由......

答案 1 :(得分:0)

Select Distinct /* Not sure that you need this */
    *,
    Case /* I removed the Null cases since they aren't for output */
        When t3.C1 <> 'M' Then 
            Case
                When t1.C2  = t2.C2 Then t1.C2
            End
        When t3.C1  = 'M' Then
            Case
                When t1.C2  = t2.C2 And t2.C3 Not Like 'PREV' Then t1.C2
                When t1.C2 <> t2.C2 And t2.C3     Like 'PREV' Then t2.C2
            End
    End as LongCaseExpression
From
    t1
    Inner Join t2 On t1.C2 = t2.C1
    Inner Join t3 On t1.C3 = t3.C2
Where  
    Case
        When t3.C1 <> 'M' Then 
            Case
                When t1.C2  = t2.C2 Then t1.C2
                When t1.C2 <> t2.C2 Then Null
            End
        When t3.C1  = 'M' Then
            Case
                When t1.C2  = t2.C2 And t2.C3 Not Like 'PREV' Then t1.C2
                When t1.C2  = t2.C2 And t2.C3     Like 'PREV' Then Null
                When t1.C2 <> t2.C2 And t2.C3     Like 'PREV' Then t2.C2
                Else Null /* I presume?? Not equal and not like 'PREV' */
            End
    End Is Not Null /* I assume that the C2 is never null */

也可以编写where子句,尽管将它与选择列表中的输出列并行是有意义的:

Where  
         (t3.C1 <> 'M' And t1.C2  = t2.C2)
     Or  (t3.C1  = 'M' And
               (t1.C2  = t2.C2 And t2.C3 Not Like 'PREV')
           Or  (t1.C2 <> t2.C2 And t2.C3     Like 'PREV')
         )