Get 2 amounts of 2 columns of 2 tables without primary key

I would like to get the sum of column A of table 1 and the sum of column D in table 2 without using the primary key.

Because I can't link the two tables, the only corresponding value is not the primary key.

TABLE 1

COLUMN A.     COLUMN B |    COLUMN C

PAID NUMBER DESCRIPTION

15472 blabla1 S15-875

18515S15-875 blabla1

215526.52 blabla2 D17-517

...                               ...                           ...

TABLE 2

COLUMN D |     COLUMN E |    COLUMN F

INVOICENUMBERDESCRIPTION

185525 blabla1 S15-875

158520 D17-517 blabla2

8964D17-517

blabla2

I would like to see the number, the sum of which should be paid and what has been paid.

But as you can see, you can pay in several times and there may be several invoices.

There is therefore no link between two tables which is a primary key in both entities.

No idea how I could get it?

NUMBER |    DESCRIPTION |    BILL |     PAID

S15-875 185525 33987 blabla1

D17-517 blabla2 167484 215526.52

The tables can be attached on «NUMBER», regardless of weather conditions, as a primary key is defined.

sql> with table_1 as
  2    (         select 15472     as paid, 'S15-875' as "NUMBER", 'blabla1' as description from dual
  3    union all select 18515     as paid, 'S15-875' as "NUMBER", 'blabla1' as description from dual
  4    union all select 215526.52 as paid, 'D17-517' as "NUMBER", 'blabla2' as description from dual
  5    )
  6  , table_2 as
  7    (         select 185525    as invoice, 'S15-875' as "NUMBER", 'blabla1' as description from dual
  8    union all select 158520    as invoice, 'D17-517' as "NUMBER", 'blabla2' as description from dual
  9    union all select 8964      as invoice, 'D17-517' as "NUMBER", 'blabla2' as description from dual
 10    )
 11  select t1."NUMBER"     as "NUMBER"
 12  ,      t1.description  as description
 13  ,      ( select sum(t2.invoice) from table_2 t2 where t2."NUMBER" = t1."NUMBER" ) as invoice
 14  ,      sum(t1.paid)    as paid
 15  from   table_1 t1
 16  group by t1."NUMBER"
 17  ,        t1.description
 18  /

NUMBER  DESCRIP    INVOICE       PAID
------- ------- ---------- ----------
D17-517 blabla2     167484  215526.52
S15-875 blabla1     185525      33987

BTW: 'NUMBER' is a horrible name for a column. I strongly recommend to change something that is not a reserved word.

Tags: Database

Similar Questions

  • maximum value for a column in a table without using rownum and rowid, group

    Hi all

    Is it possible to recover the maximum value in a column of a table without help:
    1 rownum and rowid.
    2. the Group
    3. any built-in function of Oracle.

    Kindly let me know if possible.


    Kind regards.

    Published by: J2EE_Life on January 23, 2012 21:06
    select e1.empno
      from scott.emp e1
      left outer join scott.emp e2 on e1.empno < e2.empno
     where e2.empno is null
    
  • 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

  • The sidebar to view the columns of the table without opening table

    In earlier versions of SQL developer, when I clicked on the button "expand" next to a table, it would be a list of columns that the table had without open the table in the main window. If I right click on the name of the table, there are open table.

    Is there a way to get 2.1 to return to this behavior?

    Open objects with a single click Explorer to be controlled through tools | Preferences | Database | ObjectViewer | Open the object by a simple click. I have it turned off and that it must be a double click on the name or right click and select open before the opening of the object browser. However, even with this, I need to click on the name of the object (not the 'expand' button) before the object viewer opens. By clicking on the button "expand" always expands the list of columns and open object viewer regardless of the value of preference.

    theFurryOne

  • Order columns (which make up the primary key constraint) matter in where clause of DML query for use of indexing mechanism to operate.

    Version of DB: database Oracle 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production


    I have a table my_table as below:
    create table my_table
    (
    my_code varchar2 (6).
    my_id varchar2 (24).
    forced pk_1 primary key (my_code, my_id)
    );


    Primary_key here's a composite key that contains columns 1 (my_code) and 2 (my_id).
    Is there that a difference in the way below queries is executed in terms of performance (use of indexing in the extraction).

    (a) select * from my_table where my_code = '123' and my_id = "456";
    (b) select * from my_table where my_id = '456' and my_code = '123';


    The understanding I have the order of the column in the where clause should be identical to the sequence in
    What primary key draws for indexing to be used by oracle in oracle other DML queries ignores indexing
    However when I used explain plain both show the same query cost with single scan with index rowid.
    so I don't know if I'm wrong in the concept that I have. Kindly help.

    Thanks in advance,
    Gerard

    Your question is answered in the Performance Tuning Guide

    Using indexes and clusters

    14.1.4 choosing composite indexes

    A composite index contains several key columns. Composite indexes can provide additional benefits compared to the index to single column:

    • Improved selectivity

      Sometimes the two or more columns or expressions, each with a low selectivity can be combined to form a composite with a high selectivity.

    • Reduced IO

      If all columns selected by a query are a composite index, then Oracle may return these values in the index without access to the table.

    A SQL statement can use a path on a composite index if the statement contains constructions that use a main part of the index.

    Note:

    This is no longer the case with the skip index scans. See "Index Skip Scans".

    A main part of an index is a set of one or more columns that have been specified first and consecutively in the list of columns in the CREATE INDEX statement that created the index.

  • addition of populated table's primary key column

    Hi all


    I am trying to add a column to a table that is already filled. The column to be added must be the primary key.
    As expected, oracle jumped to the top of the error (ORA-01758) of a column NOT NULL cannot be added, unless the table is empty.

    A possible way is to create a new temp table and then transfer all current data... rename the temporary table later.

    Is there another way to accomplish this?

    Thank you

    Hello

    As error suggested you cannot add a constraint not null on the new column to the table that is already filled. What else you can do next to you, you have a fairly simple and fast to create a temporary table and moving data?
    You can add the new column but null, update the column with values and alter the table of marking column as not null. But your idea of empty temporary table is simple and fast.

    CREATE TABLE my_objects
    AS
       SELECT 'my_new_column' my_new, owner
       FROM all_objects
       WHERE ROWNUM < 1;
    
    _Not Null_
    ALTER TABLE MY_OBJECTS
    MODIFY(MY_NEW  NOT NULL);
    
    _Primary key_
    ALTER TABLE MY_OBJECTS
     ADD CONSTRAINT MY_OBJECTS_PK
     PRIMARY KEY
     (MY_NEW);
    

    Concerning

  • Query to get the data of the column and the metadata in the same set of results.

    Is it possible to build a query to get the values of the columns in a table and also be able to get some metadata (data type, data_length, data_precision, data_scale) for columns in the same set of results.

    If I use a join, have a common value to join on the two tables?

    you use a cross join, not requiring common values.

    create table T (n number, d date, v varchar2(30));
    insert into T values (1,sysdate,'ABC');
    commit;
    
    select C.column_name, c.data_type, c.data_length,
    case c.column_id
     when 1 then to_char(T.N)
     when 2 then to_char(T.D)
     when 3 then T.V
    end VALUE
    from USER_TAB_COLUMNS C, T
    where C.table_name='T'
    order by c.column_id;
    
  • Need help to build the query/pl-sql block to get the query result and the name of column from DB table in the form of key-value pairs.

    Hi Experts,

    I have a DB table has columns of more than 50.

    I question this table, it should only return one line at any time. as sqldeveloper below image.

    here, I need to build block pl/sql-query, Discover the column in the table as a key and query result as values.

    Eg:     Key                         -  Value

    TASK_EVENT_ID - 1765

    EVENT_TYPE - ASR_UPDATE

    ... etc until all of the columns in my table.

    Experts please comment on that point, appreciate your help on this.

    Thank you

    -Vincent.

    Here is an approach using DBMS_SQL to iterate over the columns of key / value to assign... (Little code snipped for brevity)

    create or replace procedure (task_expired)

    v_store_id in full,

    v_task_action_id in full,

    v_job_id in full

    )

    as

    -[SNIP code...]

    v_sql VARCHAR2 (4000): = ' select * from my_table where PK = 123';  -Your SQL here!

    v_v_val VARCHAR2 (4000);

    v_n_val NUMBER;

    v_d_val DATE;

    v_ret NUMBER;

    c NUMBER;

    d NUMBER;

    col_cnt INTEGER.

    f BOOLEAN;

    rec_tab DBMS_SQL. DESC_TAB;

    col_num NUMBER;

    vAsString VARCHAR2 (4000);

    BEGIN

    -[SNIP code...]

    Message_properties. CORRELATION: = "EDF_EVENT";

    MSG: = SYS. AQ$ _JMS_BYTES_MESSAGE. Construct();

    Msg.set_string_property ('queueName', ' shipping/csi_cth');

    Msg.set_string_property ('MODE', 'CR8');

    c: = DBMS_SQL. OPEN_CURSOR;

    DBMS_SQL. PARSE (c, v_sql, DBMS_SQL. NATIVE);

    d: = DBMS_SQL. Execute (c);

    DBMS_SQL. DESCRIBE_COLUMNS (c, col_cnt, rec_tab);

    1.col_cnt J

    LOOP

    CASE rec_tab (j) .col_type

    WHEN 2 THEN

    DBMS_SQL. DEFINE_COLUMN (c, j, v_n_val);      -Number

    WHEN 12 CAN

    DBMS_SQL. DEFINE_COLUMN (c, j, v_d_val);      -Date

    ON THE OTHER

    DBMS_SQL. DEFINE_COLUMN (c, j, v_v_val, 2000);   -Else treat as varchar2

    END CASE;

    END LOOP;

    LOOP

    v_ret: = DBMS_SQL. FETCH_ROWS (c);

    WHEN OUTPUT v_ret = 0;

    1.col_cnt J

    LOOP

    -Fetch each column to the correct data type based on coltype

    CASE rec_tab (j) .col_type

    WHEN 2 THEN

    DBMS_SQL. COLUMN_VALUE (c, j, v_n_val);

    vAsString: = to_char (v_n_val);

    WHEN 12 CAN

    DBMS_SQL. COLUMN_VALUE (c, j, v_d_val);

    vAsString: = to_char (v_d_val, ' DD/MM/YYYY HH24:MI:SS');

    ON THE OTHER

    DBMS_SQL. COLUMN_VALUE (c, j, v_v_val);

    vAsString: = v_v_val;

    END CASE;

    Msg.set_string_property (rec_tab (j) .col_name, vAsString);

    END LOOP;

    END LOOP;

    DBMS_SQL. CLOSE_CURSOR (c);

    DBMS_AQ. ENQUEUE (queue_name-online 'cbus.aqjms_common',

    Enqueue_options => Enqueue_options,

    Message_properties => Message_properties,

    Payload-online msg,

    Msgid => Message_handle);

    dbms_output.put_line ('00 Msgid =' |) Message_handle);

    dbms_output.put_line('===Done=');

    -[SNIP code...]

    END;

    /

  • How to refer to the primary key column of newly inserted rows of tabular form

    Hello

    I use APEX 4.2.0.00.27 with Oracle DB 11.2.0.3.0.

    I work with a tabular presentation wizard-created for insert and update a table using the integrated SRM process (sequence 10).  I'm trying to use a process of anonymous block of PL/SQL (sequence 30) to make another manipulation of table after the records were inserted or updated.  The manual process is associated with my tabular form and I use the variables of name of column binding in my program block.

    My (rsn_test) table has 3 columns: test_id (number), test_nbr (number), test_id2 (number).  Test_id column is identified as the primary key and the type of the source already exists a sequence rsn_test_seq.  Column test_id2 gets its default value 0 to a hidden page element.

    I would use my manual process for updating the value of the test_id2 column.  If it's 0 then I want to put the value of the column test_id.  If it is any other value, then it must remain at this value.  My logic works very well for an existing line, but I'm running into a problem with the newly added lines.  The new lines get inserted, but the test_id2 column remains the default value 0.  I can tell the debugger that the SRM process is triggered first and inserts the line, then my manual dealing with fires.  The problem seems to be that the connection variable: TEST_ID for the primary key column remains NULL after insertion.  I don't know how to get the value of the column test_id of my newly created line to use in my PL/SQL block to my update.

    Process of PL/SQL:

    DECLARE
    BEGIN
       :P7_SHOW := NULL;
       :P7_SHOW := NVL(:TEST_ID2,555) || ' and ' || NVL(:TEST_ID,787) || ' and ' || NVL(:TEST_NBR,9999);
       IF :TEST_ID2 = 0 AND :TEST_ID IS NOT NULL THEN
          UPDATE rsn_test
             SET test_id2 = :TEST_ID
           WHERE test_id = :TEST_ID;
       ELSE
          :TEST_ID2 := :TEST_ID2;
       END IF;
    END;
    
    

    Excerpt from the debugger:

    0.01625 0.00010 Processes - point: ON_SUBMIT_BEFORE_COMPUTATION
    0.01635 0.00008 Branch point: Before Computation
    0.01643 0.00003 Process point: AFTER_SUBMIT
    0.01646 0.00022 Tabs: Perform Branching for Tab Requests
    0.01668 0.00008 Branch point: Before Validation
    0.01676 0.00024 Validations:
    0.01700 0.00135 Perform basic and predefined validations:
    0.01835 0.00020 Perform custom validations:
    0.01855 0.00049 ...Validation "TEST_NBR must be numeric" - Type: ITEM_IS_NUMERIC
    0.01904 0.00007 ......Skip for row 1 because row hasn't changed
    0.01911 0.00016 ......Skip for row 2 because row hasn't changed
    0.01927 0.00012 ...Validation "TEST_ID2 must be numeric" - Type: ITEM_IS_NUMERIC
    0.01939 0.00007 ......Skip for row 1 because row hasn't changed
    0.01945 0.00018 ......Skip for row 2 because row hasn't changed
    0.01964 0.00005 Branch point: Before Processing
    0.01968 0.00004 Processes - point: AFTER_SUBMIT
    0.01972 0.00588 ...Process "ApplyMRU" - Type: MULTI_ROW_UPDATE
    0.02560 0.00154 ...Execute Statement: declare function x return varchar2 is begin begin for c1 in ( select "RSN_TEST_SEQ".nextval pk from sys.dual ) loop return c1.pk; end loop; end; return null; end; begin wwv_flow.g_value := x; end;
    0.02714 0.00140 ......Row 3: insert into "APPPCSRSN"."RSN_TEST" ( "TEST_ID", "TEST_NBR", "TEST_ID2") values ( :b1, :b2, :b3)
    0.02854 0.00011 ...Process "ApplyMRD" - Type: MULTI_ROW_DELETE
    0.02865 0.00004 ......Skip because condition or authorization evaluates to FALSE
    0.02869 0.00015 ...Process "Process Submit" - Type: PLSQL
    0.02884 0.00007 ......Skip for row 1 because row hasn't changed
    0.02891 0.00012 ......Skip for row 2 because row hasn't changed
    0.02903 0.00012 ......Process row 3
    0.02915 0.00429 ...Execute Statement: begin DECLARE BEGIN :P7_SHOW := NULL; :P7_SHOW := NVL(:TEST_ID2,555) || ' and ' || NVL(:TEST_ID,787) || ' and ' || NVL(:TEST_NBR,9999); IF :TEST_ID2 = 0 AND :TEST_ID IS NOT NULL THEN UPDATE rsn_test SET test_id2 = :TEST_NBR WHERE test_id = :TEST_ID; ELSE :TEST_ID2 := :TEST_ID2; END IF; END; end;
    0.03344 0.00013 ...Session State: Saved Item "P7_SHOW" New Value="0 and 787 and 1300"
    0.03356 0.00004 Branch point: After Processing
    0.03360 0.00048 ...Evaluating Branch: "AFTER_PROCESSING" Type: REDIRECT_URL Button: (No Button Pressed) Condition: (Unconditional)
    0.03407 0.00013 Redirecting to f?p=290:7:8717971109610:::::&success_msg=0%20row(s)%20updated%2C%201%20row(s)%20inserted.Success%2FEBD244168556408CBA714E3974918C09%2F
    0.03420 0.00012 Stop APEX Engine detected
    0.03432 0.00007 Stop APEX Engine detected
    0.03439 - Final commit
    
    

    Any suggestions?

    I have run tests on

    https://apex.Oracle.com/pls/apex/f?p=83488:1 demo/demo

    to see your problem.

    I have 2 solution for your problem.
    I add trial NOT tabular just usual block of PL/SQL

    BEGIN
    I'm IN (SELECT TEST_ID FROM RSN_TEST WHERE TEST_ID2 = 0)
    LOOP
          UPDATE RSN_TEST
             SET test_id2 = TEST_ID
           WHERE test_id = i.TEST_ID;
      END LOOP;
    END;

    and works very well, you can see in the sample.

    The other solution is to show new generated TEST_ID

    Adding a sequence as a default value for a column in a table field

    And to execute your procedure.

    I get how is with the good luck of time.

    By

  • When I change a column is a primary key the associated non-unique index to become unique?

    So basically I already tried this and it shows me that the associated index is not unique.

    create table employees2 in select * from employees;

    create index emp_idx on employees2 (employee_id);

    ALTER employees2 table add primary key (employe_id) using index emp_idx;

    Select * from user_indexes where index-name = "EMP_IDX";

    I was wondering if I right assuming that when you change a column to a primary key or unique while using a given index that does not have the respective index become unique.

    The textbooks I use are sometimes a little hard to understand because of the wording, also, I want to just ask someone with a little more experience than me.

    Thank you.

    your test did give the correct answer: the index is not unique if it serves to bear a unique or primary key constraint. Indeed, it is one of the benefits of the use of no unique indexes in support of UK/PK constraints (since it allows to set the unusable index before to make bulk loads; and, of course, they have also some disadvantages - for example, they need an additional logical reading to reach a line). Richard Foote explains the details in https://richardfoote.wordpress.com/2008/06/04/primary-keys-and-non-unique-indexes-whats-really-happening/ (and other items).

  • How can I update the primary key column

    Hello
    Can you suggest me best workaround/algorithm for task below:

    (Oracle 10g, Solaris OS.)
    Location:
    Table a primary key column P 'Code', tables children F1, F2,..., reference to F15 "Code_P' foreign key column"P.Code"column, and we do not know which of the child tables data available for a particular value"P.Code.
    Task:
    Change the value "P.Code" from 100 to 200. So that the result would be that record P [Code = 100] should be updated:
    update P set
    Code = 200
    where Code = 100;
    And child tables 'Code_P' column should be updated as:
    update F1, F2, .., F15 set
    P_code = 200
    where P_code = 100;
    The best solution would be that you can easily repeat this task.

    Edited by: CharlesRoos the 28.12.2010 12:10

    Will there be FK constraints in place?
    If so, you may need to modify them so that their application may be temporarily deferred.

    The gameplan would be something like this:

    -Making of CF can be deferred, immediate departure.
    -Alter FK set delay.
    -Update of Parent table.
    -Update of the children tables.
    -Immediate constraints set
    s ' engage;

  • Get the amount of the last date

    Greedings,

    Sorry for my question of amateur but how is it possible to get the (column1) amount of the last date (column2)
    To get the last date we use Column2 but how to get the amount of this line of Column1?

    Thank you for your help

    Another way using analytical functions...

    SQL> select distinct first_value(sal) over (order by hiredate desc) from emp;
    
    FIRST_VALUE(SAL)OVER(ORDERBYHIREDATEDESC)
    -----------------------------------------
                                         1100
    

    Order by hiredate desc, gives us the dates in descending order, the 'maximum' date is first, then we can get the first_value of the column we want based on that (give us the first value of lines in ascending order of lines). Because this is an analytic function, it applies the same value to all the rows in the table, then use separate to get just the value of a.

  • How can I get the amount of free disk space using the space using windows 7 Please share example using GetDiskSpace

    How can I get the amount of disk space by using windows 7 Please share example using GetDiskSpace I already studied http://digital.ni.com/public.nsf/allkb/9958B8E473C4EF1786256BBC0053B64F

    Reading your question a little more in detail, I doubt if you are using the Win32 GetDiskFreeSpace API function (including my previous post is the solution) of the function GetDiskSpace from the Toolbox of the programmer.

    With respect to the other, it works even with capacity greater than 2 GB disks, but you must use the appropriate formatting code to display the returned value. I updated my example to display the total of free space in the debug output window and added a comparison with the value of 3 GB using UInt64TypeCompareUInt box to tools programming interface.

  • Get the index of the column of table removed

    Is there a way to get the index of the column in a 2D table that has been deleted? In my application, the user has the possibility to use the right click option 'remove a column' on a 2D picture, but I would like to use this information to remove the same column in a different table programmatically. Is this possible?

    There is a file published in the documents of the community that refers to a location in a sheet of the user has clicked.  Use it like the image below.

    https://decibel.NI.com/content/docs/doc-22434

  • How to get the amount of memory installed?

    (It's under the Windows operating system)

    I have do a comparative analysis program for the performance statistics for some complex calculations based on the hardware.

    What is the best way to get the amount of RAM installed programmatically?

    How do I know if the OS is 32 or 64 bit?

    Thank you!

    Well, it does not resemble the Memstats VI returns the memory installed. It returns a number, but I can't match the amount of memory installed on my system. The GetCurrentPlatform seems to return only the name of the OS, not whether 32-bit or 64-bit. I see nothing in the App-> target list containing this information. At least to 8.2. Did not check in 2009.

    Altenbach says:

    (It's under the Windows operating system)

    I have do a comparative analysis program for the performance statistics for some complex calculations based on the hardware.

    What is the best way to get the amount of RAM installed programmatically?

    How do I know if the OS is 32 or 64 bit?

    Thank you!

    Define "better". Best in speed? Better compatibility on multiple versions of the Windows operating system? Best in terms of access to this information without having to delve too in all sorts of types of data Windows and DLLs?

    Would you consider .NET stand for the 'best '. If so, the amount of RAM installed can be gained from easily using the Microsoft.VisualBasic.Devices.ComputerInfo class:

    To determine whether your OS is 32 - bit or 64 - bit are you trying to make a distinction between a 32 bit on a 64-bit CPU vs OS a 64 bits on 64-bit OS? I know that the WMI class can be used to obtain this information, so if you try to make this distinction, you could watch the Win32_ComputerSystem and Win32_Processor classes. For example, I think that what follows is what you would see:

    OS 64-bit on x64processor:
    Win32_ComputerSystem.systemType = pc x 64
    Win32_Processor.addresswidth = 64
    Win32_Processor.architecture = 9
    Win32_Processor.DataWidth = 64

    32 bit on x86processor OS:
    Win32_ComputerSystem.systemType = x 86 pc
    Win32_Processor.addresswidth = 32
    Win32_Processor.architecture = 0
    Win32_Processor.DataWidth = 32

    OS 32 bit on x64processor:
    Win32_ComputerSystem.systemType = x 86 pc
    Win32_Processor.addresswidth = 32
    Win32_Processor.architecture = 9
    Win32_Processor. DataWidth = 64

    I've attached an example on how to access the classes with .NET. Not pretty, but not too bad either.

Maybe you are looking for