2012-09-26 3 views
1

2 것들에 대한 T-SQL에 case 문에또는 내부 케이스 문

나는 기본적으로 확인 해요을에 OR을 통합하는 방법을 알아 내려고 노력하고, 어느 하나에 해당하는 경우, 단지 0로보고 카운트.

COUNT(Case When (car[Weight] IS null) then 0 else car.CarKey 
      OR When (car.BinNumber is null) then 0 else car.CarKey 
     End) as Carkey 

또한이 시도했지만 구문은

COUNT(Case When (car[Weight] IS null) then 0 
     else When (car.BinNumber is null) then 0 
     else car.CarKey 
     End) as Carkey 
+0

을 향후 참고로, 경우 문은 그런 식으로 분할하지 않습니다. 경우 (...) 그때 ... (...) 그때 ... 끝 ... 끝; – jtimperley

답변

2

당신은 OR이 방법으로 사용할 수 있습니다 잘못 : 그냥 제거,

COUNT(Case 
     When (car.[Weight] IS null) or (car.BinNumber is null) 
     then 0 
     else car.CarKey End) as Carkey 
+0

그러나 이것은 0을 계산합니다. 그것은 OP가 원하는 것입니까? 그는 아마도 SUM (CASE ... THEN 0 ELSE 1 END) AS를 CarKey – GilM

+0

@ GilM으로 원하기 때문에 그들이 원하는 것입니다. 그것은 OP를위한 질문 일 것입니다. – Taryn

1

당신은 당신의 두 번째 시도에 가까웠다을 하나 else

COUNT(Case When (car[Weight] IS null) then 0 
    When (car.BinNumber is null) then 0 
    else car.CarKey 
    End) as Carkey 

참고 : Null 값없이 항목을 계산하려면 해당 항목이 포함되지 않습니다. 0 값은 여전히 ​​값이므로 카운트됩니다.

COUNT(Case When (car[Weight] IS null) then null 
    When (car.BinNumber is null) then null 
    else car.CarKey 
    End) as Carkey 

또는 대신 sum를 사용 : 당신이 계산하고 싶지 않은 항목에 대한 null를 사용

SUM(Case When (car[Weight] IS null) then 0 
    When (car.BinNumber is null) then 0 
    else 1 
    End) as Carkey