How to upgrade a minimum value of column column in another table?

Hi all

I created a few tables as below, and it need to update a column in a table with a minimum value of the column of another table...

Scenarios explained as follows:
CREATE TABLE T_20 (CONTROL NUMBER(10) NOT NULL,
                            PO NUMBER(10) NOT NULL,
                            AMENDNO NUMBER(3) NOT NULL,
                            FACTOR VARCHAR2(3) NOT NULL,
                            COMMENT_X VARCHAR2(40),
                            FLAG VARCHAR2(1))


SQL> alter table t_20 add constraint t_20_pk primary key (control, po, amendno, factor);

SQL> DESC T_20;
 Name                                      Null?    Type
 --------------------------           -------- --------------
 CONTROL                           NOT NULL NUMBER(10)
 PO                                     NOT NULL NUMBER(10)
 AMENDNO                          NOT NULL NUMBER(3)
 FACTOR                             NOT NULL VARCHAR2(3)
 COMMENT_X                                      VARCHAR2(40)
 FLAG                                                 VARCHAR2(1)

Table altered.

CREATE TABLE T_30 (CONTROL NUMBER(10) ,
                            PO NUMBER(10) ,
                            AMENDNO NUMBER(3) ,
                            FACTOR VARCHAR2(3) ,
                            AMOUNT NUMBER(10,2),
                          USER_NAME VARCHAR2(4))
SQL> /

Table created.

SQL> INSERT INTO T_20 VALUES(101,1000,01,'MSC','NO COMMENT','Y')
  2  /

1 row created.

SQL> INSERT INTO T_20 VALUES(101,1000,02,'MSC','NO COMMENT','Y')
  2  /

1 row created.

SQL> INSERT INTO T_20 VALUES(101,1001,00,'NDP','NO COMMENT','Y')
  2  /

1 row created.

SQL> INSERT INTO T_20 VALUES(101,1001,01,'NDP','NO COMMENT','Y')
  2  /

1 row created.

SQL> INSERT INTO T_20 VALUES(102,1002,00,'ABC','NO COMMENT','Y')
  2  /

1 row created.


SQL>  INSERT INTO T_30 VALUES(101,1000,NULL,'MSC','400','A')
  2   /

1 row created.

SQL>  INSERT INTO T_30 VALUES(101,1000,NULL,'MSC','500','S')
  2   /

1 row created.

SQL>  INSERT INTO T_30 VALUES(101,1001,NULL,'NDP','600','I')
  2   /

1 row created.

SQL>  INSERT INTO T_30 VALUES(101,1001,NULL,'NDP','700','R')
  2  /

1 row created.

SQL> INSERT INTO T_30 VALUES(102,1002,NULL,'ABC','800','L')
  2  /

SQL> SELECT * FROM T_20
  2  /

 CONTROL     PO         AMENDNO      FACTOR     COMMENT_X         F 
----------         ----------    ----------        --------          --------------            ---- 
 101       1000          1       MSC       NO COMMENT           Y 
 101       1000          2       MSC       NO COMMENT           Y 
 101       1001          0       NDP       NO COMMENT           Y 
 101       1001          1       NDP       NO COMMENT           Y 
 101       1002          0       ABC       NO COMMENT           Y 

SQL> SELECT * FROM T_30;

   CONTROL         PO     AMENDNO         FAC     AMOUNT  USER                            
----------      ---------   ----------   ---      ----------      ----                            
       101         1000        null         MSC        400      A                               
       101         1000        null        MSC        500       S                               
       101         1001        null         NDP        600      I                               
       101         1001        null         NDP        700       R                               
       101         1002        null        ABC        800       L                               
NOW I WANT TO UPDATE T_30 THAT LOOKS AT BELOW:

By top T_30 initially AmendNo pass is null in T_30. Now I want to update
column based on a minimum value of AMENDNO in the column T_20
best way I can explain is:

In the table T_20
For CONTROL 101-> there are two numbers with 1000 but with different Amendno like 1, 2 in.
and
In the T_30 Table for CONTROL 101-> there are two IN. with 1000 but currently null. REQUIREMENT IS NOW
THE TWO DOCUMENTS SHOULD BE UPDATED WITH MIN (AMENDNO) FOR THIS CONTROL T_20. IN THIS CASE FOR THIS CONTROL
101 MIN (AMENDNO) IS '1' (1, 2)

SO 101 RECORDS (BOTH) OF NOW T_30 SHOULD BE UPDATED WITH '1' AS AMENDNO

Once again 101 now have one another IN '1001' with two different 0.1 IN. now T_30 AmendNo should be updated with min (0.1) which is
'0'

production is expected to be lower after update:
SQL> SELECT * FROM T_30;

   CONTROL      PO              AMENDNO  FAC     AMOUNT      USER                            
----------       ----------   ----------      ---        ---------     ----                            
       101       1000         1          MSC        400                      A                               
       101       1000         1          MSC        500          S                               
       101       1001         0          NDP        600           I                               
       101       1001         0          NDP        700           R                               
       101       1002         0          ABC        800           L  
Propose any query pls...

Thanks in advance...

I'll try level better to format the pure code in the post...

Concerning
Prasanth

Try this

update t_30 x
   set amendno = (
                   select amendno
                     from (
                            select control, po, factor, min(amendno) amendno
                              from t_20
                          ) y
                    where x.control = y.control
                      and x.po = y.po
                      and x.factor = y.factor
                 )
 where exists (
                   select amendno
                     from (
                            select control, po, factor, min(amendno) amendno
                              from t_20
                          ) y
                    where x.control = y.control
                      and x.po = y.po
                      and x.factor = y.factor
             )
          

Tags: Database

Similar Questions

  • Retrieve two values of column in a table

    Hi I want to retrieve two values of column in a table and store the values as a single column value in another table... How to do with it?

    Use in this way:
    Insert in tab2 (col1) select col1 | col2 from tab1;

  • How to have a minimum value of 0 by using the graphical editor

    Okay, so I thought he did this automatically based on the lowest point, in this case, a line graph. He did it for one of my graphics, but not the other, and I tried everything I could think of to fix this on my own.

    I have three charts created at the present time. The first points ranging from 0 to 25, and the axis will display a label value minimum value 0 label and a maximum of 30. So far so good.

    The second graph has only points ranging from 25 to 29, and its axis is displays a minimum value of 24 label and the label of the maximum value of 30. Photo below. It's exactly what I want for the third chart as well.

    The third diagram has points ranging from 30 to 40, and if by the logic of the second chart, its x axis should show a minimum value label, say, 28 and a maximum of, say, 42. Instead, it ranges from 0-50, as shown below.

    Any ideas?

    Hi, Honoré,.

    Type a value in the Min box.

    Kind regards

    Ian.

  • How to set the minimum value of a graphic waveform window?

    I have a graphics property of waveform "minimum value", but the graph is not take the value that I gave him. He tends to zero, as well as the first two sliders that I put on the graph. The maximum value "will be" no problem, just like the second slider value. Everyone knows about this problem? Here's a screenshot...

    Is there a reason why my chart does not have what I have to say?

    Thank you

    Sometimes you may have problems if the new minimum is higher than the maximum of old.  Try to write at least again in another node in property after you set the maximum value

    Without seeing your VI and some parameters of the example, (old max and min) new max and min, it is otherwise difficult to say.

  • How to get all minimum values for a table of valuable documents?

    I need to get the list of records of the minimum value for a table that has the structure and the data below
    create table emp (name varchar2(50),org varchar2(50),desig varchar2(50),salary number(10),year number(10));
    
    insert into emp (name,org,desig,salary,year) values ('emp1','org1','mgr',3000,2005);
    insert into emp (name,org,desig,salary,year) values ('emp1','org1','mgr',4000,2007);
    insert into emp (name,org,desig,salary,year) values ('emp1','org1','mgr',7000,2007);
    insert into emp (name,org,desig,salary,year) values ('emp1','org1','mgr',7000,2008);
    insert into emp (name,org,desig,salary,year) values ('emp1','org1','mgr',7000,2010);
    commit;
    
    
    SELECT e.name,e.org,e.desig,min(e.year) FROM emp e,(
    SELECT e1.name,e1.org,e1.desig,e1.salary FROM emp e1
    GROUP BY (e1.name,e1.org,e1.desig,e1.salary)
    HAVING COUNT(*) >1) min_query
    WHERE min_query.name = e.name AND min_query.org = e.org AND min_query.desig =e.desig
    AND min_query.salary = e.salary
    group by (e.name,e.org,e.desig);
    With the above query, I can get the year less value where the EMP has maximum salary. It returns a single record. But I want all the records that are minimal compared to the value of the year max

    Power required
    emp1     org1     mgr     7000     2008
    emp1     org1     mgr     7000     2007
    Please help me with this...

    Hello

    Aggregate functions reduce the result set with one row per group.
    Almost all of the aggregate functions are homologous analytics that can give the same results without restricting the result set. Since you want the output to contain several lines per group, then the analytic functions are probably better for you.

    Here's one way:

    WITH      got_analytics     AS
    (
         SELECT     name, org, desig, salary, year
         ,     MAX (salary)  OVER ( PARTITION BY  name, org, desig)          AS max_salARY
         ,     ROW_NUMBER () OVER ( PARTITION BY  name, org, desig, salary
                                    ORDER BY        year  DESC
                           )                              AS year_num
         FROM    emp
    )
    SELECT     name, org, desig, salary, year
    FROM     got_analytics
    WHERE     salary          = max_salary
    AND     year_num     > 1
    ;
    
  • How do subtract you different values for items in a specific table?

    Hi LabVIEW community.

    I am new to LabVIEW and Im working on my first application. One of my challenges I have problems is to know how to avoid specific values of some elements in a table.

    My application is to measure the distance between sensor and magnet, however the initial distance for each magnet sensor is different. I have converted the tension up to a distance using the Math node for each element and now need to correct these values by subtracting their initial value.

    The source is a table 1 d of DBL64 composed of 32 elements and each of these values must have a different value subtracted.

    for example:

    Element 1 - 5.557

    element 2 - 2.34

    3-7 654 element and so on

    I tried to separate them (index table) and subtract the values of each item and then combine them again. This was not effetive. I need to write these values in a .txt file and also to use them in another calculation.

    I have attached my attempted under VI.

    Any advice would really be apprecieated.

    You don't need to look it. There is a very simple method. Generate a table that contains all your desired offsets.  Then he subtract off the coast.

    This is the VI even with this change:

  • With CVI SQL Toolkit, how to add the new variable param column in a table.

    Dear all:

    I used the CVI Sql toolkit to create a database, but now I don't know how to add a new column to a table in variable param.

    I know, to add a column with the name of constant column could be down by below:

    DBImmediateSQL ((hdbc, "alter table table1 add column1 char [100]");

    But if Column1 is a param variablae how can I write the code? Please advise?  It will be appreciated if you could give me an example.

    Best regards!

    HI -.

    If you look at the parameters that you pass to the SQL function, you can see that the second parameter is a string constant. In your code, you can create an array of characters (string). You can then use sprintf to programmatically determine what will be the contents of this variable. In the function call, you can put the variable instead of the literal string, and you will have a customizable SQL statement.

    Hope this helps-

    John M

  • using a Case statement to display the values of column even as table headers

    I have a database that has been set up kind of tricky, in that some tables are made up of columns that are field names and one link to another table that contain the data for these field... also names there ID displays as many lines instead of as a row of data. I would like to write a query to extract that information and display it as one line for each ID.

    example: table 1

    ID, name, itemresponse and itemcode, Description

    Dee Jones Childrens Hospital 123 SITE location

    123 dee Jones, Date of appointment next NEXTAPPT 22/04/2014

    123 dee Jones was on bedrest NOSHOW reason do not show

    I would like to have the display of data as a record on this same patient, instead of as 3 separate registers (with the Description column in table 1

    Table 2

    ID, name, place, Date of the next meeting, show no reason

    123 dee Jones Childrens Hospital 22/04/2014 was the bed rest

    I have included the code I worked on below my question, for me to get the desired results should I use the Case statement and it's the right way to do it below?

    (SELECT
    PAT.ID "PATIENT ID."
    PAT. FIRST | » '|| PAT. "PATIENT FIRST NAME."
    PAT. "PATIENT DOB," DATE_OF_BIRTH
    DS. Date 'Date of inpatients ',.
    Dai. ITEM_CODE "Item Code",
    Dai. DESCRIPTION "Description."
    Siad. ITEM_RESPONSE,

    Max (CASE WHEN dai. ITEM_CODE IS "SITE" THEN DAI. Decription
    ANOTHER NULL
    END) AS "name of the hospital."

    Max (CASE WHEN DAI.) ITEM_CODE IS 'NEXTAPPT' THEN DAI. ITEM_Description
    ANOTHER NULL
    END) AS 'next Appt Date. "

    Max (CASE WHEN DAI.) ITEM_CODE IS "LASTDATE" THEN DAI. Description
    ANOTHER NULL
    END) AS 'last visit Date.

    Max (CASE WHEN DAI.) ITEM_CODE IS 'NOSHOW' THEN DAI. Description
    ANOTHER NULL
    END) THAT "the reason has not shown".

    Of
    (PAT.ID "IDENTIFYING PATIENT",
    PAT. FIRST | » '|| PAT. "PATIENT FIRST NAME."
    PAT. "PATIENT DOB," DATE_OF_BIRTH
    DS. Date 'Date of inpatients ',.
    Dai. ITEM_CODE "Item Code",
    Dai. DESCRIPTION "Description."
    Siad. ITEM_RESPONSE

    Of

    dm_sessions Ds,
    dm_session_assessments dsa,
    DAS dm_admin_sections,
    dm_session_assessment_items Siad,
    Dai dm_admin_items,
    Po Patient_Orders,
    Inventory inv,
    Patients_Table PAT
    dm_admin_categories CAD

    where dsa.session_Id = ds.session_Id
    and PAT.ID = ds. Patient_ID
    and dsa.excluded_assessment =' no
    and dac.category_code = 'OPEN'

    and dsa.section_id = das.section_id
    and das.category_id = dac.category_id
    and das.section_code = 'northwest'
    and dai.section_id = das.section_id
    and dsai.item_id = dai.item_id
    and dsai.session_assessment_Id = dsa.session_assessment_Id
    and dsai.excluded =' no
    - and Dai.ITEM_ID = Dsai.ITEM_ID
    and Dai.ITEM_CODE IN ('SITE', 'NEXTAPPT', 'LASTDATE', 'NOSHOW')
    and po.patient_ID = ds.patient_ID
    and in. NDC_NO = inv. NDC_NO
    and inv. TDRUG_ABBREV = in. DRUG_ABBREV

    PAT.ID GROUP, PAT. FIRST | » '|| PAT. LAST, PAT. DATE_OF_BIRTH, ds. Date, dai. ITEM_CODE, dai. DESCRIPTION, Siad. ITEM_RESPONSE))

    Hello

    DATE is not a very good name for a column.  Use something like EVENT_DATE, that will not be confused with a keyword from Oracle, instead.

    I don't see something like this column in the CREATE TABLE statement.  He will always be the same value on all lines?

    Here's a way to get the results you requested:

    SELECT id

    first name | ' ' || LastName AS name

    Date of birth

    MAX (CASE WHEN item_code = "NEXTAPPT", THEN item_response END) AS next_appointment,

    MAX (CASE WHEN item_code = "LASTDATE" THEN item_response END) AS last_visit_date,

    MAX (CASE WHEN item_code = "SITE" THEN item_response END) AS location

    FROM mytable1

    GROUP BY id, firstname, lastname, dob

    ;

    Of course, I can't test it very well until you post INSERT statements for the sample data.

    I guess that 2 or more lines of the entry have the same id, then they will necessarily be first name last name, the name and date of birth, too.  No doubt they are standardized in your actual tables.

  • How to get the name constraintlist and column in a table?

    Hello


    can someone please tell me how to get the type of constraint, and it is imposed on the names of columns in a table.




    Thank you
    Nico

    Hello

    Use the two tables:

    select * from all_constraints
    
    select * from ALL_CONS_COLUMNS
    

    Sorry SY, you were faster than me...

    Published by: user11268895 on August 19, 2010 13:27

  • Update the value of the column based on another value of the column to another table

    Hi all

    I have something very confused me and need your help.

    Having two tables A and B.

    Table A have 2 column (+ id + and desc1)

    Table B have column 2 also (+ transnum + and desc2)

    Now, I want to update the column desc2 of table B identical desc1 of table was where transnum of Table B same as the id of the table has.

    I use this SQL

    update of a2 set a2.desc2 = a1.desc1 of a2 on a2.transnum = a1.id inner join a1

    but this error occurs

    Error from line 5 in order:
    update of a2 set a2.desc2 = a1.desc1 of a2 on a2.transnum = a1.id inner join a1
    Error in the command line: 5 column: 35
    Error report:
    SQL error: ORA-00933: SQL not correctly completed command
    * 00933. 00000 - "command not properly ended SQL."
    * Question: *.

    * Action. *

    Hope someone can help me. TQ for help...
    SQL> create table a1 (id number(2),des varchar2(10));
    
    Table created.
    
    SQL> create table b1 (transnum number(2),des varchar2(10));
    
    Table created.
    
    SQL> insert into a1 values (1,'maran');
    
    1 row created.
    
    SQL> insert into b1 values (1,'ram');
    
    1 row created.
    
    SQL> commit;
    
    Commit complete.
    
    SQL> update b1 set des=(select des from a1 where b1.transnum=a1.id);
    
    1 row updated.
    
    SQL> select * from b1;
    
      TRANSNUM DES
    ---------- ----------
             1 maran
    
  • How to find the name of a column in a table

    Hi all

    I have a Table that contains approximately 100 columns.


    I need to find the column name, to find in specific columns containing data like 'abc '.

    Ex: I have a table like temp_table_name
    the columns are col1, col2, col3, col4, col5, col6, col7, col8,.... col99
    I'm looking for with data specific like 'abc '.

    Need to find the name of the table column that contains data of 'abc '.

    Please suggest me. How to find in a single query... :-)

    Actually I'm looking through a column of the table as below
    Select count (*) in the temp_table_name where col1 = 'abc '.

    FOR I IN 1.20 LOOP
    ln_num_of_rows: = 0;
    lv_column_name_search: = "col" | I have;

    lv_sql: = ' select count (*) in the temp_table_name where ' | lv_column_name_search | "= abc";

    EXECUTE IMMEDIATE lv_sql INTO ln_num_of_rows;

    IF ln_num_of_rows > 0
    -column found in lv_column_name_search
    EXIT;
    END IF;
    END LOOP;

    But I'm gettig performance problem.

    How can I do this in any other alternative way? could you suggest any way possible as soon as POSSIBLE.

    It's probably never going to be effective - realistic, you have to scan completes the picture.

    You could probably improve a little things by doing something like

    SELECT DISTINCT column_name
      FROM (
        SELECT (CASE WHEN col1 = 'abc' THEN 'col1'
                    WHEN col2 = 'abc' THEN 'col2'
                    WHEN col3 = 'abc' THEN 'col3'
                    ...
                    WHEN col20 = 'abc' THEN 'col20'
                    ELSE NULL
                    END) column_name
          FROM table_name
        WHERE col1 = 'abc'
            OR col2 = 'abc'
            ...
            OR col20 = 'abc'
      )
    

    but it's never going to be incredibly fast.

    Justin

  • How to upgrade are the value of the column and the new column value created

    Hi all

    create the table xxc_temp

    as

    Select donnees_1 'AB103', 'AAA' data_2 data_3 '123', 'RED' data_4, NULL status in all the double union

    Select donnees_1 'AB105', 'BBB' data_2, '222' data_3, data_4 'BLUE', NULL status in all the double union

    Select donnees_1 'AB106', 'CCC' data_2, '333' data_3, data_4 'BLUE', the double NULL status

    create the table xxc_base

    as

    Select donnees_1 'AB103', 'AAA' data_2, '123' data_3 data_4 'RED', 'Inactive' status of Union double all the

    Select donnees_1 'AB105', 'BBB' data_2, '222' data_3 data_4 'BLUE' status 'Active' of all the double union

    Select donnees_1 'AB106', 'CCC' data_2, '333' data_3, data_4 'BLUE', 'Inactive' status of double

    I need to update the status column xxc_temp with xxc_base value of the status column and also if the user tries to insert new values/lines in the xxc_temp

    So how can I update the status column? I mean automatically set to day and also new values/lines

    I need to update only that are Inactive then these lines should be updated in the temporary table

    We can do this by using the stored function, but I need to update the new status of lines also

    Please help me

    Here's another way to do it, using VIRTUAL function column as expression to get the status of the other table

    CREATE or REPLACE FUNCTION GET_STATUS (pDATA_1 IN TANK) RETURN VARCHAR2 DETERMINISTIC

    AS

    vstatus VARCHAR2 (8);

    BEGIN

    SELECT status in vstatus FROM xxc_base WHERE donnees_1 = pDATA_1;

    RETURN vstatus;

    END;

    /

    DROP TABLE 'XXC_TEMP ';

    CREATE TABLE 'XXC_TEMP '.

    ('DONNEES_1' CHAR (5),

    "DATA_2" TANK (3).

    "DATA_3" TANK (3).

    "DATA_4' VARCHAR2 (4).

    'STATUS' VARCHAR2 (8) GENERATED ALWAYS AS (SUBSTR (GET_STATUS (DATA_1), 1, 8)) VIRTUAL

    ) ;

    INSERT INTO 'XXC_TEMP' (data_3, donnees_1, data_2, data_4))

    Select "AB103' donnees_1, data_2 'AAA', '123' data_3, 'RED' data_4 of any double union

    Select donnees_1 'AB105', 'BBB' data_2, '222' data_3 data_4 'BLUE' of all the double union

    Select donnees_1 'AB106', 'CCC' data_2 data_3 '333', 'BLUE' double data_4);

  • How to highlight the minimum value in a column?

    I need to highlight the lowest value in each column in my spreadsheet. I try to use the conditional nail, but I can't find a function to work as I need.

    One way would be to calculate the minutes in a cell of a footer row, select the cells in the body of the column and apply conditional highlighting like this:

    You must click on the chip address first, and then select the comparison cell, A8 here, then be sure to keep the row and column.

    The A8 from the example formula is = MIN (A).

    The result is a bold 2 (not shown in the screenshots).

    SG

  • How can I compare the value of column within the COUNT() function?

    I want to compare the value of the column within the COUNT() function as below how ever it gives me the error. I want to calculate the percentage, how can I do this task?
    Thank you.


    SELECT department_id
    , COUNTY (salary < 250000) / COUNT (*)
    OF plch_employees
    GROUP BY department_id
    ORDER BY department_id;
    SELECT  department_id.
            COUNT(CASE WHEN salary < 250000 THEN 1 END) / COUNT(*)
      FROM  plch_employees
      GROUP BY department_id
      ORDER BY department_id
    /
    

    SY.

  • How to check the account of a column value is less than the other value of column in a table with a single column

    Hello

    Please let me know how to query the table and get the records that are less than the other column.

    ID item_id item_type

    3     1           r1

    2 4 r2

    1 5 r3

    6 5 r4

    I need to get all the lines in which item_id < id

    o/p

    ID item_id item_type

    3     1           r1

    6 5 r4

    SELECT ID, ITEM_ID, ITEM_TYPE

    Of

    WHERE ITEM_ID<>

Maybe you are looking for