Using the conditional case statement. !

Hello Forum members.

I have a table with order of stores as well as the order dates and other information. I have a requirement to write a request for nominations for the roll of the order according to the below mentioned logic.

Name1 = Initial order date and next order within 7 days of the original order date then its considered R1

Name2 = next activity within 7 seven on the ord_id even of its R1 to date, this is going on as increases in order to ord_no simple or even dates

.

.

Final RN is Count (Rn)

RN %--Count(Rn) / Count Distinct (ord_no) * 100

Sample data output:

ORD_DATEORD_ID1_of_x2_of_x3_of_x4_of_x5_of_x6_of_xFinal RnXRN %
10/07/201310095V1100000
03/08/201310095V1100000
08/08/201310095V1010000
12/08/201310095V1001000
06/09/201310095V1100000

DDL:

create the table xx_ord_noms

(

ord_no varchar (30),

ord_date date,

ord_id varchar2 (30)

);


Sample data:

INSERT INTO xx_ord_noms (ord_no, ord_date ord_id) VALUES ('10091', TO_DATE('07/10/2013','MM/DD/YYYY'), ' 10091');

INSERT INTO xx_ord_noms (ord_no, ord_date ord_id) VALUES ('10091', TO_DATE('08/03/2013','MM/DD/YYYY'), ' 10091');

INSERT INTO xx_ord_noms (ord_no, ord_date ord_id) VALUES ('10091', TO_DATE('08/08/2013','MM/DD/YYYY'), ' 10091');

INSERT INTO xx_ord_noms (ord_no, ord_date ord_id) VALUES ('10091', TO_DATE('08/12/2013','MM/DD/YYYY'), ' 10091');

INSERT INTO xx_ord_noms (ord_no, ord_date ord_id) VALUES ('10091', TO_DATE('09/06/2013','MM/DD/YYYY'), ' 10091');

commit;

I was intending to do something like that, but I don't get the logic for R1... R7

Select ord_no,

ord_date,

ord_id,

prev_dt,

(to_date (ord_date) - to_date (prev_dt)) data_diff,.

case

When prev_dt is null or

(to_date (ord_date) - to_date (prev_dt)) < 7 then

'R1 '.

When (to_date (ord_date) - to_date (prev_dt)) > 7 THEN

"R2".

ON THE OTHER

nULL value

END

FROM (select a.*,

(SELECT MAX (tmp2.ord_date)

OF xx_ord_noms tmp2

WHERE a.ord_id = tmp2.ord_id

AND tmp2.ord_date < a.ord_date) prev_dt

of the xx_ord_noms one).

Could someone give advise me or suggestions on how we can achieve the desired results.

Thanks in advance.

LAG and LEAD are good when you want to look forward or back up a fixed number of lines, but in this case you do not know how far behind, you will need to go.

I have a solution. It calculates the Royal Navy in number, not a position of the column, but you can turn it with instructions BOX if you want to. I did it in two steps:

First of all, I calculated the Royal Navy by comparing the date of the current and previous line. To do this, I have generated a line number using row_number() Analytics:

Select ord_no,

ord_date,

ord_id,

numrow

Rn

of xx_ord_noms

model

partition (ord_no)

dimension (row_number() over (partition by order of ord_no of ord_date) numrow)

measures (ord_date 0 Rn, ord_id)

update rules

(

RN [1] = 1,

RN [ANY] BOX when trunc (ord_date [cv ()]) = - trunc (ord_date [cv () - 1])< 7="" then="" rn[cv()-1]+1="" else="" 1="">

)

ORD_DATE ORD_ID NUMROW RN ORD_NO

---------- -------------------- ---------- ---------- ----------

10091 10 July 2013 00:00:00 10091 1 1

10091 3 August 2013 00:00:00 10091 2 1

10091 August 8, 2013 00:00:00 10091 3 2

10091 12 August 2013 00:00:00 10091 4 3

10091 2013 - Sep - 06 00:00:00 10091 5 1

Then I needed to identify all the lines of each series. I defined firstDt as the date of the first item in the series:

firstDt [1] = ord_date [cv ()]

firstDt [ANY] = CASE when rn [cv ()] = 1 then ord_date [cv ()] to another firstDt [cv () - 1] end

With that, the FinalRn is just a max (rn) Analytics (firstDt score)

I'm not clear on what R % is. Your descriptions are not clear and I think they are incompatible.

Select ord_no, ord_date ord_id

, Rn, finalRn, 100 * finalRn/totCnt PCT1, 100 * totRn/totCnt PCT2

totRn

of xx_ord_noms

model

partition (ord_no)

dimension (row_number() over (partition by order of ord_no of ord_date) numrow)

measures (ord_date, 0 Rn, ord_id, to_date (null) firstDt, 0 finalRn, 0 totRn, 0 totCnt)

upsert rules all the

(

RN [1] = 1

RN [ANY] BOX when trunc (ord_date [cv ()]) = - trunc (ord_date [cv () - 1])< 7="" then="" rn[cv()-1]+1="" else="" 1="">

firstDt [1] = ord_date [cv ()]

firstDt [ANY] = CASE when rn [cv ()] = 1 then ord_date [cv ()] to another firstDt [cv () - 1] end

[ANY] finalRn = max (rn) on (firstDt score)

totCnt [ANY] = count (*) over()

, totRn [ANY] = count (case when rn = 1 then 1 other trailing null))

)

ORD_DATE ORD_ID RN PCT1 PCT2 TOTRN FINALRN ORD_NO

---------- -------------------- ---------- ---------- ---------- ---------- ---------- ----------

10091 10 July 2013 00:00:00 10091 1 1 20 60 3

10091 3 August 2013 00:00:00 10091 1 3 60 60 3

10091 August 8, 2013 00:00:00 10091 2 3 60 60 3

10091 12 August 2013 00:00:00 10091 3 3 60 60 3

10091 2013 - Sep - 06 00:00:00 10091 1 1 20 60 3

Kind regards

David

Tags: Database

Similar Questions

  • conditional case statement

    I wonder if it's possible to put conditional case statements in a subquery in a pl sql view?

    I have a sub request, and final statement is:
    AND EXISTS (SELECT NULL
                                 FROM   steps ps
                                 WHERE  ps.route = pr.id
                                 AND    ps.step_type= 1
    is it possible to do something like this conditionally includes the statement "AND"?
    WHEN id = 1(
             AND EXISTS (SELECT NULL
                                 FROM   steps ps
                                 WHERE  ps.route = pr.id
                                 AND    ps.step_type= 1)

    When there is more conditions, better put everything in extra parentheses (because of the condition of GOLD):

    ... 
    AND (id != 1
    OR (id = 1 AND EXISTS (SELECT NULL
                                 FROM   steps ps
                                 WHERE  ps.route = pr.id
                                 AND    ps.step_type= 1)))
    
  • Using the cmdlet Get-stats for the problems of performance of ESXi

    I need to detect the bottlenecks (if any) causing poor performance with a (free) 5.5 ESXi host.

    I see that if I use the cmdlet Get - stat PowerCLI I get dozens of meters, such as "mem.usage.average" or "'rescpu.maxlimited1.latest ', just to give two samples. "

    Can I identify a subset of the counters I should follow to identify possible bottlenecks in the fastest way possible?

    Concerning

    Marius

    Everything depends of course.

    There are several positions which list a number of key counters to watch.

    The Hosts of ESXi monitoring - a deeper on the what and the why look , you will find a good starting set.

  • How to use the conditional and made mandatory

    Hello

    I need to use the conditional and made mandatory something jspx page. Following line gives an error:

    rendered = "#{Bindings.type.inputValue! = 'abc' & & bindings.proofId.inputValue! ="} ' "

    Error: Name expected instead of &.

    What is the right way?



    Note: I use Jdev 10.1.3

    Boring, but you can use & #38; & #38; (remove the space after the &) for this.

    Timo

    Published by: Timo Hahn on 24.11.2009 08:46

    Published by: Timo Hahn on 24.11.2009 08:48

  • How to use the conditions where there are more than two options

    I have a blade that collects information from the user and then makes decisions based on the values entered. On a slide I use the widget drop-down list and list of"basic", "Intermediate" and "Advanced". I hoped to do is use the Coditional Structure as follows:

    1. If the Basic was selected slide goto BasicQuickQuiz
    2. If the intermediary has been selected goto slide IntermediateQuickQuiz
    3. If Advanced was selected slide goto AdvancedQuick Quiz

    I can make this work for Basic or intermediate, but I don't see how (or even if) I have a multi level if statement.
    My work is about:
    1. a button added logic:
    So basic, then got otherwise BasicQuickQuiz goto IntermediateQuickQuiz

    2. on the Enter slide on IntermediateQuickQuiz event:
    If the intermediary, then keep, otherwise goto slide AdvancedQuick Quiz

    It works, but it seems awkward - y at - it another way that this goal could be achieved?

    Tim

    What I was hoping to see was the ability to create several Else If statements within the only condition. I've changed it now to add two conditional statements, one after the other, but still find this unscrupulous feature.
    I will record a feature request to add a Select Case block / Switch style fuction to this group.
    However, since it is new I can live with the workflow (for this version of Cp anyway!)

    Tim

  • First HP: help using the command CAS.solve (STRING1, STRING2) within a program

    Hello and hope you can help me with my problem!

    I write a somewhat complicated program (operating mode no case) who has a subroutine called STSOLVE where his entry is always a string from the main program that contains a formula as "'I' = 'V' / 'R', 'R'" and its goal is to replace all the variables contained in "" to the compatible standard CASE generated automatically variable (teiXX where XX is a unique number) and then use the command solve to assess this formula (say final temporary formula "tei1 = tei2 / ') tei3, tei3"). Before returning, he replaces the temporary variables, generated automatically with the original on the result, so the original formula will be sent back solved in the main program.

    My problem:

    It returns the result {UNDEF} or simply {} even in expressions that absolutely work if used with the command solve manually, in the main screen of calculators CASES when the CAS.solve command is used either! Even though I know that the entry of this order is correct according to the tutorials I've read in the forums, I can't work properly.

    I tried all methods of writing the order and I only managed to get a result when I wrote this:

    RPART1: = cas.expr (RPART1);

    RESLT: = case. Solve (RPART1, "tei2");

    .. .or RPART1 contains, say, the part 'tei1 = tei2/tei3' (NOT AS A STRING!) and "tei2" SHOULD be written as shown here, a chain, entered manually just for test (can be 'tei1' or 'tei3' too for this particular case). However, if I put "tei2" in RPART2 of local variable in the subroutine as a string and then pass this variable to the solve command, say:

    RPART1: = cas.expr (RPART1);

    RESLT: = case. Solve (RPART1, RPART2);  RPART2 contains the string "tei2", "tei1" or "tei3.

    Here again, I get a {} as a result!

    I tried so already a lot of things that I don't know exactly what mention here, but I hope I described the main situation enough to start detailed and of course, I'll give you any other information you might need to give me more help. I tried to paste the entire subroutine, or even the entire program, but I don't have any windows pc to connect the first HP and copy the program. I am writing from an Android Tablet and I should maybe type the everything here is not very easy, I will of course if you ask.

    Perhaps, I should also note that the generated vars tei1, tei2 etc. do not exist as variables in the calculator, but if I copy channels same as the program tries to run in the command CAS.solve and manually write in case screen (not a string of course) then they get solved without problems.

    Hope to get help and learn from you and I wish I could found more help about it online, but it seems very difficult and also all the tutorials I found on the cas.solve command in the programs did not work in my case!

    Andreas

    It works, or is this not the problem?

    CAS.solve ("tei1 = tei2/tei3", "tei2");

    I get output: {tei1 * tei3} which is correct.

  • How can I use the same case of event of controls dynamically


    Hi Johannes,
    collect all your references control (for this, you can use the FP property) and create a user event. Connect this user event to your structure of the event. You now have a case of event for all of your controls. See the attached picture.

    Mike

  • How can I get the nested case statements to work with listagg?

    I have a statement below that does not work as I want it, here is my code:

    Select

    cases where o.personal_label is not null

    then

    listagg (cases where s.subcodevalue like '% MON %')

    then s.subcodevalue |' '|| o.personal_label

    of other s.subcodevalue

    end,': ')

    the Group (order of s.subcodevalue) as subcodevalue

    on the other

    listagg (s.subcodevalue,': ')

    the Group (order of s.subcodevalue) as subcodevalue

    end

    of mtm_styles_new s, order_mtm o

    where INSTR (o.extras, s.code) > 0

    and o.bodyfitting = s.bodyfitting

    and o.division = s.division

    and s.subcodevalue is not null

    and o.ORDER_MTMID = 'somevalue '.

    It should bring the following

    * MY TEST: RPP

    or the following if the first case is not completed:

    * MONDAY: RPP

    I get an error message saying 'missing keyword', but cannot make out where, can someone please help me here?

    The following works fine as long as it is not a nested case statement, but I need to nest the case statement:

    Select listagg (cases where s.subcodevalue like '% MON %')

    then s.subcodevalue |' '|| o.personal_label

    of other s.subcodevalue

    end,': ')

    the Group (order of s.subcodevalue) as subcodevalue

    of mtm_styles_new s, order_mtm o

    where INSTR (o.extras, s.code) > 0

    and o.bodyfitting = s.bodyfitting

    and o.division = s.division

    and s.subcodevalue is not null

    and o.ORDER_MTMID = 'somevalue ';

    Out alias 'subcodevalue as' the case at the end of the case body.

  • Using nested switch/case statements

    What is the best practice of two following switch/case statements?

    Is there an easier way (less code) to do this?

    Thank you.

    I think it's as clean as it gets:

  • Using the conditional to determine the Select statement

    I don't know if anyone knows if I can do that, but I try to use a factor based on a value on what query to run...

    Something like:
    DECODE('INBOUND', 
         'INBOUND', SELECT 'I' FROM DUAL, 
         'OUTBOUND', SELECT 'O' FROM DUAL, 
         'BOTH', SELECT 'B' FROM DUAL);
    OR
    SELECT * 
          FROM 
          ( 
               CASE 
                    WHEN '<~S~>' = 'INBOUND' THEN SELECT 'INBOUND' FROM DUAL 
                    WHEN '<~S~>' = 'OUTBOUND' THEN SELECT 'OUTBOUND' FROM DUAL 
                    ELSE SELECT 'BOTH' FROM DUAL 
               END 
           ) AllValues
    Is it still possible?

    Thank you
    Andy

    Published by: 847975 on September 28, 2011 11:02

    Scalar subqueries should appear in square brackets, for example

    select case dummy
              when 'X' then (select 'First example' from dual)
              when 'Y' then (select 'Second example' from dual)
           end as demo
    from dual;
    

    or if you prefer the old syntax DECODE,

    select decode(dummy,
                  'X', (select 'First example' from dual),
                  'Y', (select 'Second example' from dual)
           ) as demo
    from dual;
    
  • Work with the nested case statement

    Hi, I tried the SQL to the date below max but not a single runtime error

    Select the case sensitive option

    When f.rv = "BITTER" then

    (case

    When (m.SC. = 'A' and str_name = "ST_DT1")

    or (m.SC. = 'B' and str_name = "ST_DT2")

    or (m.SC. = 'C' and str_name = "ST_DT3") then

    Max (m.STDT)

    Where (mc.sc = 'C' and str_name = "ST_DT4")

    or (mc.sc = 'K' and str_name = "ST_DT5") then

    Max (MC. EDDT)

    on the other

    null

    end)

    on the other

    null

    end

    of memo_con mc

    baro_uy b

    end f

    ped pe

    where b.id = mc.id

    and pe.id = mc.mid

    and pe.id1 = 1234

    and b.cd = f.cd

    and f.id = 93;

    Could you please suggest me the correct way on it.

    Thank you

    You need a group of expression OR the only authorized linked should be aggregation function.

    Onw solution could be to put the aggregate function (MAX) education all about box.

    Like this

    SELECT max)

    case

    When f.rv = "BITTER" then

    case

    When (m.SC. = 'A' and str_name = "ST_DT1")

    or (m.SC. = 'B' and str_name = "ST_DT2")

    or (m.SC. = 'C' and str_name = "ST_DT3")

    then m.stdt

    Where (mc.sc = 'C' and str_name = "ST_DT4")

    or (mc.sc = 'K' and str_name = "ST_DT5")

    then mc.eddt

    end

    end) as maxdate

    of memo_con mc

    baro_uy b

    end f

    ped pe

    where b.id = mc.id

    and pe.id = mc.mid

    and pe.id1 = 1234

    and b.cd = f.cd

    and f.id = 93;

  • Use the condition in flex

    Hello

    I need to use itemrender or something like that to show a value of GridColumn in a color different corre its value

    < s:GridColumn width = "120" dataField = "email_active" headerText = "E-mail active" >

    < s:itemRenderer >

    < fx:Component >

    < s:GridItemRenderer >

    < s:Label backgroundColor = "{data.email_active == 1?" #00ff00 ":"#FF0000"}" / > "

    < / s:GridItemRenderer >

    < / fx:Component >

    < / s:itemRenderer >

    < / s:GridColumn >

    email_active: is a field of a table of my database.

    the database is downloaded in datagrid using a php and copy the following code:

    < s:AsyncListView id = list = "{getUserResult.lastResult}" / "UserAsyn" >

    I suggest using a separate GridItemRenderer and not an embeded in the GridColumn tag.

    First value label, is that the size of the length of the data of this element, thus affecting the background color will not be for the label and not the whole of the cell.

    Instead of changing the label, as a rectangle graphic element, then set it to the width and height of the cell before you can set the background color according to the data.

    Second reason is to avoid the question of trying to put a color value of string to a uint value.

    I created and example for something similar, here is the Source view .

  • Using the conditional output

    Thanks VetSrini, I know how to generate the first half of this production (the names by commas).

    Guidance on how I would go about generating the other half? IE: print a string called 'New' and add the names in a specific order, if the 'New' column Y as shown in the below desired output (formatting is messed up...) New: C2 must be displayed in the client and no column as it appears below).


    Customer country
    USA C1, C6
    New: C2

    Canada C3
    New: C4, C5


    XML:

    <? XML version = "1.0" encoding = "UTF-8" standalone = "yes"? >
    < root xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" >
    < row >
    < country > USA < / country >
    C1 of < customer > < / customer >
    < New > N < / New >
    < / row >
    < row >
    < country > USA < / country >
    C2 < customer > < / customer >
    Y < new > < / New >
    < / row >
    < row >
    < country > USA < / country >
    C6 < customer > < / customer >
    < New > N < / New >
    < / row >
    < row >
    < country > Canada < / country >
    C3 < customer > < / customer >
    < New > N < / New >
    < / row >
    < row >
    < country > Canada < / country >
    C4 < customer > < / customer >
    Y < new > < / New >
    < / row >
    < row >
    < country > Canada < / country >
    C5 < customer > < / customer >
    Y < new > < / New >
    < / row >
    < / root >

    I sent you again, with modified solution.

    ,

  • Case statement in the SQL Query prompt dashboard

    Hi Experts,

    I use the following case statement to generate default values in the prompt dashboard based on a condition (the other guest-generated values). Below are two of the query I've tried and the syntax error. I tried all the possibilities (like giving apostrophes, double quotes, don't quote not etc.), but could not understand the question. Don't know what exactly I'm missing here.

    Query 1:

    SELECT

    CASE

    WHEN @{PRType} = monthly THEN 'Participant pay '. "" Amount of PayRate "="15000"

    WHEN @{PRType} = daily THEN 'Participant pay '. "" Amount of PayRate "="650"

    WHEN @{PRType} = hourly THEN 'Participant pay '. "" Amount of PayRate "="1"

    ELSE ' 0'

    END

    OF 'PA-Participant pay. '

    Query 2:

    SELECT

    CASE

    WHEN @{PRType} = monthly THEN "15000"

    WHEN @{PRType} = daily THEN "650"

    @{PRType} = schedule THEN '1'

    ELSE ' 0'

    END

    OF 'PA-Participant pay. '

    This is the query that is generated in the log with error message file.

    1 log message:


    SELECT CASE WHEN Hourly = Monthly THEN "Participant Payroll"."Payrate Amount" = '15000' WHEN Hourly = Daily THEN "Participant Payroll"."Payrate Amount" = '650' WHEN Hourly = Hourly THEN "Participant Payroll"."Payrate Amount" = '1' ELSE '0' END FROM "PA-Participant Payroll"

    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error occurred. [nQSError: 43113] The message returned by OBIS. [nQSError: 27002] Near <>=: Syntax error [nQSError: 26012]. (HY000)



    Comment 2:

    ;CASE WHEN To_Char (Hourly) = 'Hourly' THEN 1 ELSE 0 END 

    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error occurred. [nQSError: 43113] The message returned by OBIS. [nQSError: 27002] Near <>(): syntax error [nQSError: 26012]. (HY000)

    Sorry, I wasn't clear... you must always put this CASE statement in a valid SQL SELECT statement.

    SELECT

    CASE

    When ' @{PRType}' = 'Monthly' THEN '15000'

    When ' @{PRType}' = 'Daily' THEN '650'

    ELSE '0' END

    OF 'PA-Participant pay. '

  • How can you change a menu drop-down menu in a case statement?

    Hello

    I'm trying a table find vi. This VI will be an existing xml file and once the user has selected access to its equipment, input, output and frequency it put all of these options in a string and search the XML for this exact table name and post it on one screen of output. The problem I have is my entry and exit selection must be a little different depending on the chosen equipment. I thought I could use an instruction box to change the options of selectable input and output, but I ran into snags a lot with it. For whenever I have to put a new drop of entry and exit in the drop-down menu in the case statement, he wants to put a new one in the front (I only want to enter and exit not only three I have two inputs and outputs hidden right now). I think I'm using the wrong case statement... Can someone help change my input and output, selectable options depending on the chosen equipment. Attached is my xml file, Array Find.vi (the original) and table Find_Test.vi (that's what I'm trying to change).

    Thank you

    dlovell

    Hi dlovell,

    A few changes in the attached VI:

    Requirements/possible Solutions

    • When the value of the equipment, do something
      • Add the cases to the value of equipment change event
    • Do something = decide what channels to write, to change the input and output strings
      • Read material value changed
      • Decision (case structure)
      • Channel (same method as the previous mod)

    Hope this helps

Maybe you are looking for

  • Video driver W541

    Hello I just got my laptop and the brightness keys don't work. Then I could see that, in the Device Manager, it seems that the video driver is not recognizxed. Do you know how I can fix this problem? Best regards Dalmo

  • Upgrade Windows Server 2012 Std to 2012 Std R2 with the same license

    I have an activation key for windows server 2012 Std, can I upgrade windows server 2012-2012 Std R2 with this key? Thank you

  • Windows update and does not update Security Essentials

    Minimum information to validate:Version of Windows and Service pack level. (Windows XP Home Edition, SP3; Vista Home Premium, SP1; Windows 7 Ultimate, etc...)Post also if you use 32-bit or the 64 bit version of Windows. 32-bit of WINDOWS XP Service p

  • downloadedd MSE, but can't findd on my pc

    XP family, downloaded, but can't find it on my pc. is there something else to do?

  • Cannot access control panel

    Hello I'm trying to access the control panel and whenever I try to access the control panel it comes up with an error that Windows Explorer has stopped working and said then that reset Windows Explorer.  I stopped and restarted and that did not help.