Update based on the max value of different tables.

{code}

create the table rule_table (number rule_id);

insert into rule_table values (10);

insert into rule_table values (20);

insert into rule_table values (30);

insert into rule_table values (40);

create the table auth_table (number of auth_id, rule_id number);

insert into auth_table values (1000, 10);

insert into auth_table values (2000, 10);

insert into auth_table values (3000, 10);

insert into auth_table values (4000, 20);

insert into auth_table values (5000, 20);

insert into auth_table values (6000, 30);

insert into auth_table values (7000, 30);

insert into auth_table values (8000, 40);

insert into auth_table values (9000, 40);

create the table pay_table (pay_id number, auth_id number, pay_indicator number);

insert into pay_table values (11111, 1000, 0);

insert into pay_table values (22222, 1000, 1);

insert into pay_table values (33333, 1000, 0);

insert into pay_table values (44444, 2000, 0);

insert into pay_table values (55555, 2000, 1).

insert into pay_table values (66666, 2000, 0);

insert into pay_table values (77777, 3000, 0);

insert into pay_table values (88888, 3000, 0);

insert into pay_table values (99999, 4000, 0);

insert into pay_table values (111111, 4000, 0);

insert into pay_table values (222222, 5000, 0);

insert into pay_table values (333333, 5000, 0);

insert into pay_table values (444444, 6000, 0);

insert into pay_table values (555555, 7000, 1);

insert into pay_table values (666666, 8000, 0);

insert into pay_table values (777777, 9000, 0);

insert into pay_table values (888888, 9000, 1);

create the table rule_pay (rule_id number, pay_max_indicator number);

insert into rule_pay values (10, 0);

insert into rule_pay (20, 0) values;

insert into rule_pay (30, 0) values;

insert into rule_pay (40, 0) values;

{code}

My intention is:

for every find of rule_id on the maximum pay_indicator (tables 3 query to get the max Show pay_indicator below) and on the other table I have to update this value max based on rule_id.

with max_tab

as

(

Select a.rule_id, max (pay_indicator) pay_indicator

rule_table a.,

b auth_table,

c pay_table

where a.rule_id = b.rule_id

and b.auth_id = c.auth_id

A.rule_id group

)

Update rule_pay

Set pay_max_indicator = (select max_tab.pay_indicator

of max_tab

where max_tab.rule_id = rule_pay.rule_id)

where

rule_id in (select rule_id from max_tab);

The above query does not.

Any help or suggestions are greatly appreciated.

Something like that?

SQL > select * from rule_pay;

RULE_ID PAY_MAX_INDICATOR

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

10                    0

20                    0

30                    0

40                    0

SQL > fusion in rule_pay PR

2. using (select a.rule_id, max (pay_indicator) pay_indicator

rule_table 3A,

auth_table 4 b,

pay_table 5 c

6 where a.rule_id = b.rule_id

7 and b.auth_id = c.auth_id

Group 8 by a.rule_id

9        ) u

10 on (u.rule_id = rp.rule_id)

11 when matched, then update

12 set rp.pay_max_indicator = u.pay_indicator

13;

4 lines merged.

SQL > select * from rule_pay;

RULE_ID PAY_MAX_INDICATOR

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

10                    1

20                    0

30                    1

40                    1

BTW, you can't put a WITH clause before an update.  It can only precede a select statement.

Tags: Database

Similar Questions

  • Update based on the missing values

    Hello

    I'm new to sql, attempted to retrieve results for the below scenario, but not able to do. Please let me know how I can do this.

    I created the sample data below

    create table id_data_upd (ID, ID_EXT, ID_VER) as

    Select '12', '123', 1' Union double all the

    Select '12', '345',' ' of all the double union

    Select '12', '446', "of all the double union

    Select ' 21 ', ' 2156', "of all the double union

    Select ' 22 ', ' 2346', 1' Union double all the

    Select ' 22 ', ' 2846', '0' of all the double union

    Select ' 22 ', ' 3456', '0' of all the double union

    Select ' 32 ', ' 4512', 1' Union double all the

    Select ' 33 ', ' 4942', '0' of all the double union

    Select '33', '5639', '0' double;

    My data source is

    IDID_EXTID_VER
    121231
    12345
    12456
    212156
    2223461
    2234560
    22325420
    3245121
    3349420
    3356390

    I'm trying to update the data for power two conditions

    (1) If for the same id, there are a several ID_EXT and also check if the ID_VER = 1, then all other documents with this ID the ID_VER should be updated 0

    (2) if there is a single record ID with ID_EXT unique with on no matter what ID_VER must be updated to iD_VER as 1

    Data in the target after updates

    IDID_EXTID_VER
    121231
    123450
    124560
    2121561

    I tried the County of ID_EXT (having count (ID_EXT) = 1), but records that should not be coming are also coming.

    Please tell me how I can do this.

    Best regards and thanks,

    Quentin

    Hello

    Try this

    4d68a426-e5d3-4f8c-A933-bb16364ea560 wrote:

    Hi Frank,.

    I expect the output as below.

    ID ID_EXT ID_VER
    12 123 1
    12 345 0
    12 456 0
    21 2156 1
    22 2346 1
    22 3456 0
    22 32542 0
    32 4512 1
    33 4942 0
    33 5639

    0

    Thank you

    Quentin

    with t as (  -- start sample data
    select '12' id, '123' id_ext , '1'  id_ver  from dual union all
    select '12', '345' , ' ' from dual union all
    select '12', '446' , '' from dual union all
    select '21', '2156' , '' from dual union all
    select '22', '2346' , '1' from dual union all
    select '22', '2846' , '0' from dual union all
    select '22', '3456' , '0' from dual union all
    select '32', '4512' , '1' from dual union all
    select '33','4942' , '0' from dual union all
    select '33', '5639' , '0' from dual )
    -- end sample data
    select id,id_ext,id_ver,
    case when row_number () over (partition by id order by id_ver desc nulls last)  > 1 then '0'
    else nvl(id_ver,'1') end new_id_ver,
    nvl(case when row_number () over (partition by id order by id_ver desc nulls last)  > 1 then '0'
    end,'1') maybe_new_id_ver
    from t
    

  • need to based on the primary key of different table 2 foreign key constraint

    I'm using Oracle 11.2.0.3.

    I have 2 table invl with invl_pk as primary key and org with org_pk as the primary key.

    In the third table entity_list, I have entity_pk that can be indvl_pk or org_pk.   How can I create this constraint?

    Thanks for your time.

    Hello

    Assuming that your model is correct, then what you describe is a normal type of ER modeling construction known as a relationship of the CRA. It is characterized by an entity with two or more links that are mutually exclusive to each other (XOR). A typical example may be where a person entity and an entity of the Organization relate both to a bank account entity, but a bank account can only be used by a person or an organization in a mutually exclusive way. In order to serve this on a diagram, an arc is drawn between effective relationships on the side of the child, where the name of a relationship of the CRA.

    Google will be of multiple examples and explanations.

    In a perspective of physical databases, this can be implemented in general in two different ways.

    1. assuming that all parents in the relationship of the CRA have identical PK, that is to say, a number that is generated, the child may have a FK of the same type and an entity of type column that defines what entity the FK is. The advantage of this is that it is a bit more compact and is expandable without schema changes if you want to add more relationships. The downside of this is that the CF cannot be defined at the database level and will build on the code to be executed or both in the application and the database as a trigger.

    2. regardless of types PK for parent entities, the child has a separate optional FK appropriate for each relationship in the arc. Each FK is defined at the database level while maintaining the exclusivity of relationships, a constraint is defined on the table that prevents several FK being filled for a given instance. This can be a little messier that there must be a (column/s) FK for each relationship in the arc, but the relationship may be limited to the level of the object database with the help of FK constraints and a check constraint. This could also be considered to be more technically correct implementation.

    Hope this helps

    André

  • Get the Max values and average of the different cycles in the single channel

    Hello

    I'm trying to get the Max values and average of the single channel that has different cycles it contains. I tried to use commands such as Chnclasspeak3 and chnpeakfind, but they were not useful for me. What I need is the Max values and average of the different cycles numbers saved in the data channel.

    Exampld if the string contains 5 numbers of repetitive cycles, then we must find the maximum values and the average of these 5 cycles in the single channel. Attached reference data. This is the .raw file and I have the plugin for it to use in diadem 11.1.

    Kind regards

    X. Ignatius

    Hello, Ignatius,.

    Sorry, it took some time to provide a replacement based on the script for the function. Please take a look at the attached script. I changed the script to use my function if the tiara-version is less than 12. My script function is not as fast and more stable than the implementation of tiara, but for now, it does the job

    Andreas

  • based on the prompt value see the FDF anayasis

    Hello

    I have a situation like this
    I use obiee 11g

    Based on the prompt value in him would like to show 3 different analayses.
    I mean that if the value of guest in the page has as abc data it should show the anlaysis1
    If the prompt value data as def it should show the anlaysis2
    If the prompt value data as the IGS, it should show the anlaysis3
    If the prompt value data as jkl it should show the anlaysis3

    Why 3 diff analysis is because I have my list 2 columns in each anayaiss and they are 3 different tables.

    One more thing

    Obiee do we have a show and a return value for a radiobutton/drop-down list. As show us abc how invites and returns 1. is possible?

    Thank you

    Published by: user123 on July 12, 2011 12:29

    You can have n number of reports, simply use the union all in the dash prompt sql results and this will give you the choice to select any report you want.

  • How to get the Max value in Essbase

    Hello

    I have problem to get the max value of 3 years in Essbase.
    How can I get the max value of Dec 2009, Dec 2010, Dec 2011.
    Suppose the value of Dec 2009 = 1000, dec 2010 = 1500 and Dec 2011 = 2000
    I want to get the max value of these three value, how can I do this in the Essbase calculation Script.
    Any idea?


    Thank you.

    Kind regards

    Joni

    You did not specify if year and period are there separate dimensions, in any case as always a number of different possbilities and I don't have much time today to think about, but only one method can be to use @MAXRANGE

    DIFFICULTY (other members to set, 'Dec')

    'MemberToStoreAgainst' = @MAXRANGE("MemberToFindMaxRangeFor","2009:"2011");

    ENDFIX

    See you soon

    John
    http://John-Goodwin.blogspot.com/

  • Find the max value

    Hi team,

    I have the following query where I need to find the max value

    [code]

    with t as)

    Select 'L1' R_nm, R_Data 'Data', 'Obj' R_Obj, r_prec 'Wd', '2' double val

    Union of all the

    Select 'L1', 'Data', 'Obj', 'No', '4' double

    Union of all the

    Select 'L2', 'Data', 'Obj', 'No', '4' double)

    Select t.*, max (val) on rk (partition r_nm, r_data, r_obj) of t

    [/ code]

    My results should be

    [code]

    r_nm r_data r_obj r_prec rk val

    L1 Data Obj 1 2 Wd

    L1 data Obj 4 No.2

    L2 Data Yes 1 1 Obj

    [/ code]

    Thank you


    Hello

    It seems that you are not interested in the MAX at all.  Looks like you want to classify the lines, such as one that has the lowest value is assigned the number 1, the 2nd line with the lowest values gets 2,... and line with him gets N-th lowest value N.  If is not serious if the MAX is 4, 2 or 420.

    Here's a way to do it:

    SELECT T.*

    , RANK () OVER (PARTITION BY r_nm, r_obj)

    ORDER BY val

    ) AS rk

    T

    ORDER BY r_nm, r_obj

    val

    ;

    Depending on how you want to deal with links, you can use ROW_NUMBER or DENSE_RANK instead of RANK.

  • Get the max value with other areas

    Hi all

    I have a table as below

    Value name
    - - - - - - - - - - - - - - - -
    A1 5
    A3 10
    A2 7
    A2 9
    A1 10

    What I get is the max (Value) and with the consolidation of its name

    Value name
    - - - - - - - - -
    A2 16

    Thank you
    Alex

    Published by: user8606416 on June 1, 2011 10:17

    Published by: user8606416 on June 1, 2011 10:26

    Depends on how you feel on one of the links of:

    SELECT name, value
    FROM (SELECT name, SUM(value)
          FROM table
          GROUP BY name
          ORDER BY 2 DESC)
    WHERE rownum = 1
    
    SELECT name, SUM(value)
    FROM table
    GROUP BY name
    HAVING SUM(value) >= ALL (SELECT SUM(value)
                              FROM table
                              GROUP BY name)
    

    among many other methods. The first takes a single arbitrary registration in the case of a tie, and the second shows all the related records.

    John

  • How to find the bpel instance in 11 g based on the index values

    We have 10 GB BPEL processes where we define the 4 index values for all instances. Whenever the support request comes, ask us the values of index and based on what we're looking for the process instance.

    We have now migrated bpel 10g and 11g process. How to find the bpel instance in 11 g based on the index values?

    You can go there. http://soasphere.blogspot.com/2011/04/how-to-Serch-composites-for-index.html. Do a join on the cube_instance and ci_indexes tables.

  • Get the MAX value of dimension 2 table

    I have a little trouble to find a value MAX (or MIN) of the second dimension in a 2-dimensional array. I do a query and the first dimension is the column name, the second is the values. I'm coming out of the MAX value of the column in the second dimension. ArrayMax said "the array passed cannot contain more than one dimension."

    Any ideas? I'm sure that this is possible.

    MaxValue = ArrayMax (QueryName ["FieldName"]);

    or Q of Q.

  • Max value in a table

    Hello

    Can someone explain me why I see different values in the attached VI.

    I have the array of strings, when I try to find the max value by using the Array function, I get a single value and the array of strings even when the conversion to digital, and then when I find the max value I get the different value? What I understand here?

    Thank you

    Repeat after me: take the max of a string array value is meaningless. He repeats it.

    Repeat after me: the characters ASCII codes are NOT the same as the numeric values. He repeats it.

  • kindly tell how to use the unique value of a table with the index 0

    kindly tell how to use the unique value of a table with the index 0

    Hi
     
    Yep, use Index Array as Gerd says. Also, using the context help ( + h) and looking through the array palette will help you get an understanding of what each VI does.
     
    This is fundamental LabVIEW stuff, perhaps you'd be better spending some time going through the basics.
     
    -CC
  • How to find the max value of scrollV in AS3?

    I used the response from Ned to get my 3 fields of scrolling text set based on the value of one of them. Thanks Ned!

    The other two text fields scroll to match the text box 'parent', but in the end – you get to the last scroll position - the other two do not update and 3 text fields are out of alignment. It's just at the end of the roll. The rest of the scroll values line up. I think that if I can find the scrollV max value, I can force the two others to scroll one last time when this value is reached by the text field "parent".

    The textfield object has a property for this: maxScrollV.

  • The Master Table column updated based on the sum of column Table detail


    With the help of JDev 11.1.1.6.

    I have a master-detail table based on a link to BC.

    The main table has a column that displays an InputText or an OutputText, based on the value in another column.

    If the InputText is displayed, the user can enter a value and the database will be updated with that value.

    If the OutputText is displayed, it must be a sum of a column in the secondary table.  Also, this value will be written in the database.

    Question:

    How can I fill the OutputText in the main table with the sum of the values in a column in the secondary table?

    The detail table column is a manually entered InputText field.

    Thank you.

    Create a spike in the main table and write in its expression as follows - DetailVoAccessorName.sum ("ColumnName");

    This will calculate the sum of column table detail and then you can set the value of the transient attribute to attribute DB on backup operation

    Ashish

  • Increment sequence based on the existence value

    Hello

    I have a requirement,
    I need the value sequence based on the values exist in the database.
    Let me explain with an example of a column, I have values such as 1,2,3,4,5,6,7,8,10,12,14, etc...

    Here I have to generate the value of the sequence 9,11,13 follows...


    Logically it please suggest me... I try with While loop I could not reach again.

    We want to help you.

    Concerning
    Rambeau
     select  level missing_values
       from  dual
       connect by level <= (select max(your_column) from your_table)
    minus
     select  your_column
       from  your_table
    /
    

    For example:

    with t as (
               select level n from dual connect by level < 9 union all
               select 10 from dual union all
               select 12 from dual union all
               select 14 from dual
              )
    -- end of on-the-fly data sample
     select  level missing_values
       from  dual
       connect by level <= (select max(n) from t)
    minus
     select  *
       from  t
    /
    
    MISSING_VALUES
    --------------
                 9
                11
                13
    
    SQL> 
    

    SY.

Maybe you are looking for

  • Impossible to use the card on my Equium A100 reader

    I put my SD HC card in the reader and ensured it locks in correctly.But nothing happens. Recently, I reformatted my hard drive. Can you please tell me which driver I need? I saw the latter on the toshiba download page, and to think that they might be

  • Laptop won't recognize that two of my 5 speakers

    Original title: Speaker Setup I have a 5 speaker installed on my HP laptop, but my laptop will only recognize the 2 rear speakers. I changed the speakers that cannot be recognized by the computer with those that are exactly the same model and it stil

  • Windows XP NOT reinstall USB controllers after restart

    I am running windows XP. My laptop stopped grateful hardware plug-and-play connected via USB and began asking drivers to be installed. I tried with the software that came with them (a mouse and a keyboard, and a flash player), but it does not work ei

  • My office and the taskbar icons keep disappearing.

    Whenever I turn on my computer, my desktop icons and the taskbar disappear when I click on them.

  • Duration field

    Using a storm, I noticed that the fields for the duration of the appointment and the time of an alarm have a field that we will you specify options on a go. I'm looking to set up an identical to the duration of the appointment with the selection fiel