Tuesday 15 February 2011

postgresql - Running complicated sql query within CASE -


"itemprop =" text ">

I created a new report in postgres - it should run only if there are 3 completed cases, and then an end result Determination (pass / fail) on the basis of personal matters. Depending on whether it passes or fails, different information is required to be displayed.

I have three main questions that I need, but now I can combine them in one.

The main query is that there is a conditional argument for a case here:

 Select  is the case when the "alloc" (select from COUNT) (*) Where "CodeID" = 1 and "StatusCodeID" (= 2,6)) = 3 Then --Determine Case 3 Different Assessment is the case when (depending on the COUNT passes through (*), Answer "ans" ans ans "AnswerOption" ansop on ansop. "AnswerOptionID" = ans. "AnswerOptionID" include "QTxti" QuestionTextInstance on qtxti. Qtxti. "QuestionTextInstanceID" = ans. "QuestionTextInstanceID" combination based on left aloc " Alloc "aloc." AllocID "= qtxti." A LlocID "Join Left" QuestionText "qtxt at qtxt." "QuestionTextID" = qtxti. Combination "code" boded based on the "QuestionTextID" left aloc. "CodeID" = bcode. "CodeID" Where bcode. "CodeID" = 1 more Qtxt. "QuestionTextID" = 11 and ANS. "Value" = 0) & gt; = 1 - At least 2 'yes' will have to be answered 'passed' --Execute 'Pass.sql' did not succeed in the remaining '--Execute' NotPass.sql end all other 'report did not run' end < / Code>  

It runs correctly and gives the correct result based on the conditions, however, within THEN and ELSE internal For intercepting the statement, I need to display different information that includes many columns and many (currently pass / .sql files instead of ode> not passed , but I can not find any way to apply this because it looks like THEN Or any question within ELSE blocks can only return one value.

How can I do this?

It can be done in various ways in plain SQL . A well-known composite / line is with types:

 Select   ( x ) . * From the (case of selection) cond_a_here there then (from SELECT t to ou = x) Bracket notes in code> (x). * . They are  Simple  to understand with  

PL / pgSQL , but you understand how to control composite types is . I have posted several related answers ...

  Create or resume the function () before return () returns $ func $ as SETOF T BEGIN if cond_a_ then returns QUERY Select * T to WHERE x = 1; Select ELSE return ticket * from where T = 2; end if; End $ $ func $ LANGUAGE plpgsql;    

No comments:

Post a Comment