2014-01-08 3 views
0

Я пытаюсь написать оператор CASE, который даст мне отличное значение для каждого актива. Следующий код выполняется без ошибок, но если актив проходит и завершается, каждый результат записывается. Мне нужен только отдельный результат для каждого актива, и он основан на инструкции CASE.TSQL Вложенный CASE с условием

Ниже приведен пример результата из запроса в его нынешнем виде

ServerID IP HOSTNAME CARS_SubBusiness STATUS Technology FailureReason 
20979 1.1.1.1 myhost1  Business1   PASSED Windows  - 
20979 1.1.1.1 myhost1  Business1   FAILED UNIX   Unable to complete Unix login 

В этом случае мне нужно только вернуть PASSED результат.

Exclusion + * = Exclusion 
Pass + * = Pass 
FAIL + Not Attempted/No Results = FAIL 
Not Attempted = Not Attempted 
No Results = No Results 

Если она проходит и не мне нужно только проходящую информацию, если она не входит в стоимости, но имеет другой статус, мне нужно только отчуждение как статус и так далее ..

SELECT auth.ServerID, auth.IP, auth.HOSTNAME, auth.CARS_SubBusiness, 

    CASE MAX (CASE 
       (CASE 
        (CASE WHEN [Status] LIKE 'PASSED' AND ExclusionType IS NOT NULL 
         THEN 6 WHEN [Status] LIKE 'FAILED' AND ExclusionType IS NOT NULL THEN 6 WHEN [STATUS] LIKE 'Not Attempted' AND ExclusionType IS NOT NULL 
         THEN 6 WHEN [Status] LIKE 'PASSED' AND ExclusionType IS NULL THEN 5 WHEN [Status] LIKE 'FAILED' AND ExclusionType IS NULL 
         THEN 4 WHEN [STATUS] LIKE 'Not Attempted' AND ExclusionType IS NULL THEN 3 ELSE 0 END) 
       WHEN 6 THEN 'Excluded' WHEN 5 THEN 'PASSED' WHEN 4 THEN 'FAILED' WHEN 3 THEN 'Not Attempted' ELSE 'No Results' END) 
      WHEN 'Excluded' THEN 5 
      WHEN 'Not Attempted' THEN 4 
      WHEN 'No Results' THEN 3 
      WHEN 'PASSED' then 2 
      WHEN 'FAILED' then 1 END) 
         WHEN 5 THEN 'EXCLUDED' 
         WHEN 4 THEN 'Not Attempted' 
         WHEN 3 THEN 'No Results' 
         WHEN 2 THEN 'PASSED' 
         WHEN 1 THEN 'FAILED' 
         END AS [STATUS], auth.Technology, 
         auth.FailureReason 
    FROM  _CombinedAuthentication AS auth RIGHT OUTER JOIN 
         BusinessTranslations AS bt ON auth.BusinessID = bt.BusinessID 
    WHERE (auth.ActiveFlag IS NOT NULL) 
    GROUP BY auth.ServerID, auth.IP,auth.CARS_SubBusiness, auth.HOSTNAME, auth.Technology, auth.FailureReason 
+0

И ваша проблема ...? – JayC

+0

@JayC Мне нужен только отдельный результат для каждого актива, и он основан на инструкции CASE. Исключение + * = Исключение Pass + * = Pass СБОЙ + Не Покушение/Нет Результаты = FAIL Не Покушение = Не Покушение Нет результатов = Нет результатов –

+0

«Мне нужен только отличный результат за актив» - все в порядке, так если есть * множественные * возможные результаты, какие правила мы (и в конечном итоге SQL Server) должны использовать для выбора одного результата? Эти правила должны основываться на доступных нам данных (например, результат с наибольшим значением 'sproog', где' sproog' является столбцом в одной из ваших таблиц) –

ответ

0

вы можете рассмотреть возможность использования комплектов, что-то вроде этого:

select auth.ServerID, auth.IP,auth.CARS_SubBusiness, auth.HOSTNAME, auth.Technology, auth.FailureReason,status 
from bla where status='exclusion' 
union 
select auth.ServerID, auth.IP,auth.CARS_SubBusiness, auth.HOSTNAME, auth.Technology, auth.FailureReason,status 
from bla where status='pass' and not exists (select 1 from bla where status='exclusion' and...) 
union 
select auth.ServerID, auth.IP,auth.CARS_SubBusiness, auth.HOSTNAME, auth.Technology, auth.FailureReason,status 
from bla where status='fail' and not exists (select 1 from bla where status='exclusion' and ...) 
          and not exists (select 1 from bla where status='pass' and ...) 
union 
select auth.ServerID, auth.IP,auth.CARS_SubBusiness, auth.HOSTNAME, auth.Technology, auth.FailureReason,status 
from bla where status='Not Attempted' and not exists (select 1 from bla where status='exclusion' and ...) 
             and not exists (select 1 from bla where status='pass' and ...) 
             and not exists (select 1 from bla where status='fail' and ..) 
union 
select auth.ServerID, auth.IP,auth.CARS_SubBusiness, auth.HOSTNAME, auth.Technology, auth.FailureReason,status 
from bla where status='No Result' and not exists (select 1 from bla where status='exclusion' and ...) 
            and not exists (select 1 from bla where status='pass' and ...) 
            and not exists (select 1 from bla where status='fail' and ..) 
            and not exists (select 1 from bla where status='Not Attempted' and ..) 

это решение, вероятно, может быть улучшена при использовании КТР, а

Смежные вопросы