Slow Query: Join rows from table A with first match in table B

Hi,
I have been struggling with this for days. It is very slow:
With table with 4.5 mio records it took over 2h.
Records with anType 2 and 3 4 mio.
Records with anType 1 and 4 500,000
Different acWarehouse values: 20
Different acIdent values: 9799
Could this be written in any other way so that it would be faster.
anId | acWarehouse | acIdent | anType | anQty | anTotalQuantity
1| WarehouseA | IdentA | 1 | 100 | 100
2| WarehouseA | IdentA | 1 | 100 | 200
3| WarehouseA | IdentA | 1 | 100 | 300
4| WarehouseA | IdentA | 1 | 100 | 400
5| WarehouseA | IdentA | 2 | -100 | 100
6| WarehouseA | IdentA | 2 | -100 | 200
7| WarehouseA | IdentA | 2 | -100 | 300
8| WarehouseA | IdentA | 2 | -100 | 400
Result should be:
anId | anEdge_Transaction_Id | anQuantity
5| 1| 100
6| 2 | 100
7| 3 | 100
8| 4 | 100
Table definition:
CREATE TABLE iPA_Transaction
     ANID     NUMBER(9,0) -- PRIMARY KEY
,     ACWAREHOUSE     VARCHAR2(30 CHAR)
,     ACIDENT     VARCHAR2(16 CHAR)
,     ANTYPE     NUMBER(1)
,     ANQTY     NUMBER(19,4)
,     ANTOTALQUANTITY NUMBER(19,4) -- RUNNING TOTAL
ALTER TABLE iPA_Transaction ADD CONSTRAINT PK_Transaction PRIMARY KEY (anId);
CREATE INDEX IX_Transaction_TEST4 ON iPA_Transaction(acIdent,acWarehouse,anType,anTotalQuantity);
CREATE TYPE edge_transaction_data AS OBJECT (
     anId NUMBER(9,0)
,     anEdge_Transaction_Id NUMBER(9,0)
,     anQuantity NUMBER(19,4)
CREATE TYPE edge_transaction AS TABLE OF edge_transaction_data;
/Query:
     SELECT
          iPA_Transaction.anId
     ,     first_transaction.anEdge_Transaction_Id
     ,     first_transaction.anQuantity
     FROM
          iPA_Transaction
          INNER JOIN TABLE(
               CAST(
                    MULTISET(
                         SELECT
                              iPA_Transaction.anId
                         ,     MIN(transaction_stock.anId) KEEP (DENSE_RANK FIRST ORDER BY transaction_stock.anTotalQuantity) AS anEdge_Transaction_Id
                         ,     MIN(transaction_stock.anTotalQuantity) KEEP (DENSE_RANK FIRST ORDER BY transaction_stock.anTotalQuantity) AS anTotalQuantity
                         FROM
                              iPA_Transaction transaction_stock
                         WHERE
                              transaction_stock.anType IN (1,4)
                         AND transaction_stock.acIdent = iPA_Transaction.acIdent
                         AND transaction_stock.acWarehouse = iPA_Transaction.acWarehouse
                         AND transaction_stock.anTotalQuantity > (iPA_Transaction.antotalquantity + iPA_Transaction.anqty)
                    ) AS edge_transaction
          ) first_transaction ON (iPA_Transaction.anId = first_transaction.anId)
     WHERE
          iPA_Transaction.anType IN (2,3)
     ;-- EXECUTION PLAN
PLAN_TABLE_OUTPUT
Plan hash value: 1731335374
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 6634 | 362K| 107M (1)|357:36:32 |
| 1 | NESTED LOOPS | | 6634 | 362K| 107M (1)|357:36:32 |
|* 2 | TABLE ACCESS FULL | IPA_TRANSACTION | 3946K| 203M| 15004 (1)| 00:03:01 |
|* 3 | COLLECTION ITERATOR SUBQUERY FETCH| | 1 | 2 | 27 (0)| 00:00:01 |
| 4 | VIEW | | 1 | 39 | 6 (0)| 00:00:01 |
| 5 | SORT AGGREGATE | | 1 | 50 | | |
| 6 | INLIST ITERATOR | | | | | |
| 7 | TABLE ACCESS BY INDEX ROWID | IPA_TRANSACTION | 1 | 50 | 6 (0)| 00:00:01 |
|* 8 | INDEX RANGE SCAN | IX_TRANSACTION_TEST4 | 1 | | 5 (0)| 00:00:01 |
Query Block Name / Object Alias (identified by operation id):
1 - SEL$80EA2A9E
2 - SEL$80EA2A9E / IPA_TRANSACTION@SEL$1
3 - SEL$80EA2A9E / KOKBF$@SEL$2
4 - SEL$4 / KOKSDML$@SEL$540AC7B0
5 - SEL$4
7 - SEL$4 / TRANSACTION_STOCK@SEL$4
8 - SEL$4 / TRANSACTION_STOCK@SEL$4
Predicate Information (identified by operation id):
2 - filter("IPA_TRANSACTION"."ANTYPE"=2 OR "IPA_TRANSACTION"."ANTYPE"=3)
3 - filter("IPA_TRANSACTION"."ANID"=SYS_OP_ATG(VALUE(KOKBF$),1,2,2))
8 - access("TRANSACTION_STOCK"."ACIDENT"=:B1 AND "TRANSACTION_STOCK"."ACWAREHOUSE"=:B2 AND
("TRANSACTION_STOCK"."ANTYPE"=1 OR "TRANSACTION_STOCK"."ANTYPE"=4) AND
"TRANSACTION_STOCK"."ANTOTALQUANTITY">:B3+:B4 AND "TRANSACTION_STOCK"."ANTOTALQUANTITY" IS NOT NULL)
Column Projection Information (identified by operation id):
1 - (#keys=0) "IPA_TRANSACTION"."ANID"[NUMBER,22],
"IPA_TRANSACTION"."ACWAREHOUSE"[VARCHAR2,120], "IPA_TRANSACTION"."ACIDENT"[VARCHAR2,64],
"IPA_TRANSACTION"."ANTYPE"[NUMBER,22], "IPA_TRANSACTION"."ANQTY"[NUMBER,22],
"IPA_TRANSACTION"."ANTOTALQUANTITY"[NUMBER,22], VALUE(A0)[96]
2 - "IPA_TRANSACTION"."ANID"[NUMBER,22], "IPA_TRANSACTION"."ACWAREHOUSE"[VARCHAR2,120],
"IPA_TRANSACTION"."ACIDENT"[VARCHAR2,64], "IPA_TRANSACTION"."ANTYPE"[NUMBER,22],
"IPA_TRANSACTION"."ANQTY"[NUMBER,22], "IPA_TRANSACTION"."ANTOTALQUANTITY"[NUMBER,22]
3 - VALUE(A0)[96]
4 - "KOKSDML$"."KOKSDML$_C00000"[NUMBER,22], "KOKSDML$"."ANEDGE_TRANSACTION_ID"[NUMBER,22],
"KOKSDML$"."ANTOTALQUANTITY"[NUMBER,22]
5 - (#keys=0) MIN("TRANSACTION_STOCK"."ANTOTALQUANTITY") KEEP (DENSE_RANK FIRST ORDER BY
"TRANSACTION_STOCK"."ANTOTALQUANTITY")[22], MIN("TRANSACTION_STOCK"."ANID") KEEP (DENSE_RANK FIRST
ORDER BY "TRANSACTION_STOCK"."ANTOTALQUANTITY")[22]
6 - "TRANSACTION_STOCK".ROWID[ROWID,10], "TRANSACTION_STOCK"."ANID"[NUMBER,22],
"TRANSACTION_STOCK"."ACWAREHOUSE"[VARCHAR2,120], "TRANSACTION_STOCK"."ACIDENT"[VARCHAR2,64],
"TRANSACTION_STOCK"."ANTYPE"[NUMBER,22], "TRANSACTION_STOCK"."ANTOTALQUANTITY"[NUMBER,22]
7 - "TRANSACTION_STOCK".ROWID[ROWID,10], "TRANSACTION_STOCK"."ANID"[NUMBER,22],
"TRANSACTION_STOCK"."ACWAREHOUSE"[VARCHAR2,120], "TRANSACTION_STOCK"."ACIDENT"[VARCHAR2,64],
"TRANSACTION_STOCK"."ANTYPE"[NUMBER,22], "TRANSACTION_STOCK"."ANTOTALQUANTITY"[NUMBER,22]
8 - "TRANSACTION_STOCK".ROWID[ROWID,10], "TRANSACTION_STOCK"."ACIDENT"[VARCHAR2,64],
"TRANSACTION_STOCK"."ACWAREHOUSE"[VARCHAR2,120], "TRANSACTION_STOCK"."ANTYPE"[NUMBER,22],
"TRANSACTION_STOCK"."ANTOTALQUANTITY"[NUMBER,22]
Edited by: 939464 on 08-Jun-2012 02:30
Edited by: 939464 on 08-Jun-2012 02:32
Edited by: 939464 on 08-Jun-2012 02:36
Edited by: 939464 on 08-Jun-2012 04:39

Additional to what has just been said by Hoek, which I also quote, I feel this could be a problem similar to the one posted here.
[url:https://forums.oracle.com/forums/thread.jspa?threadID=2387388]SQL - Which positive covered the negative?
Could you please let us know a bit more about the logic of the output?
1) Do you want to know which transaction with positive quantity cover the current transaction with negative quantity?
2) How does it need to be partitioned?
3) Are the quantity always equal for corresponding transaction?
If I just look at your data I can do something really simple but it might be not what you need.
CREATE TABLE iPA_Transaction
ANID NUMBER(9,0) -- PRIMARY KEY
, ACWAREHOUSE VARCHAR2(30 CHAR)
, ACIDENT VARCHAR2(16 CHAR)
, ANTYPE NUMBER(1)
, ANQTY NUMBER(19,4)
, ANTOTALQUANTITY NUMBER(19,4) -- RUNNING TOTAL
ALTER TABLE iPA_Transaction ADD CONSTRAINT PK_Transaction PRIMARY KEY (anId);
CREATE INDEX IX_Transaction_TEST4 ON iPA_Transaction(acIdent,acWarehouse,anType,anTotalQuantity);
INSERT INTO iPA_Transaction VALUES(1, 'WarehouseA', 'IdentA', 1 , 100, 100);
INSERT INTO iPA_Transaction VALUES(2, 'WarehouseA', 'IdentA', 1 , 100, 200);
INSERT INTO iPA_Transaction VALUES(3, 'WarehouseA', 'IdentA', 1 , 100, 300);
INSERT INTO iPA_Transaction VALUES(4, 'WarehouseA', 'IdentA', 1 , 100, 400);
INSERT INTO iPA_Transaction VALUES(5, 'WarehouseA', 'IdentA', 2 , -100, 100);
INSERT INTO iPA_Transaction VALUES(6, 'WarehouseA', 'IdentA', 2 , -100, 200);
INSERT INTO iPA_Transaction VALUES(7, 'WarehouseA', 'IdentA', 2 , -100, 300);
INSERT INTO iPA_Transaction VALUES(8, 'WarehouseA', 'IdentA', 2 , -100, 400);
SELECT a.anid, b.anid anedge_transaction_id, -a.anqty anqty
  FROM ipa_transaction a, ipa_transaction b
WHERE     a.acwarehouse = b.acwarehouse
       AND a.acident = b.acident
       AND a.antype IN (2, 4)
       AND b.antype IN (1, 4)
       AND a.antotalquantity = b.antotalquantity;
      ANID ANEDGE_TRANSACTION_ID      ANQTY
         5                     1        100
         6                     2        100
         7                     3        100
         8                     4        100
{code}
Try to give additional details.
Regards.
Al                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Similar Messages

  • How  to retrieve one row from the number of records in a table

    i want to retrieve the rows from the number of records in a table without using the perticular column name.

    select count(*) from table
    /If you have your table currently analyzed, and no changes have been made, then you could
    select num_rows from user_tables where table_name = 'table'

  • When selecting a row from a view with a nested table I want just ONE entry returned

    Does a nested table in a view "EXPLODE" all values ALWAYS no matter the where clause for the nested table?
    I want to select ONE row from a view that has columns defined as TYPE which are PL/SQL TABLES OF other tables.
    when I specify a WHERE clause for my query it gives me the column "EXPLODED" with the values that mathc my WHERE clause at the end of the select.
    I dont want the "EXPLODED" nested table to show just the entry that matches my WHERE clause. Here is some more info:
    My select statement:
    SQL> select * from si_a31_per_vw v, TABLE(v.current_allergies) a where a.alg_seq
    =75;
    AAAHQPAAMAAAAfxAAA N00000 771 223774444 20 GREGG
    CADILLAC 12-MAY-69 M R3
    NON DENOMINATIONAL N STAFF USMC N
    U
    E06 11-JUN-02 H N
    05-JAN-00 Y Y
    USS SPAWAR
    353535 USS SPAWAR
    SI_ADDRESS_TYPE(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NUL
    L, NULL)
    SI_ADDRESS_TAB()
    SI_ALLERGY_TAB(SI_ALLERGY_TYPE(69, 'PENICILLIN', '11-JUN-02', NULL), SI_ALLERGY
    TYPE(74, 'SHELLFISH', '12-JUN-02', NULL), SIALLERGY_TYPE(68, 'PEANUTS', '13-J
    UN-02', NULL), SI_ALLERGY_TYPE(75, 'STRAWBERRIES', '13-JUN-02', NULL))
    SI_ALLERGY_TAB()
    75 STRAWBERRIES 13-JUN-02
    *******Notice the allergy entry of 75, Strawberries, 13-JUN-02 at the
    end. This is what I want not all the other exploded data.
    SQL> desc si_a31_per_vw
    Name Null? Type
    ........ Omitted uneeded previous column desc because of metalink
    character limit but the view is bigger then this.......
    DEPT_NAME VARCHAR2(20)
    DIV_NAME VARCHAR2(20)
    ADDRESSES SI_ADDRESS_TAB
    CURRENT_ALLERGIES SI_ALLERGY_TAB
    DELETED_ALLERGIES SI_ALLERGY_TAB
    SQL> desc si_allergy_tab
    si_allergy_tab TABLE OF SI_ALLERGY_TYPE
    Name Null? Type
    ALG_SEQ NUMBER
    ALG_NAME VARCHAR2(50)
    START_DATE DATE
    STOP_DATE DATE
    SQL> desc si_allergy_type
    Name Null? Type
    ALG_SEQ NUMBER
    ALG_NAME VARCHAR2(50)
    START_DATE DATE
    STOP_DATE DATE

    Can you explain what do you mean by the following?
    "PL/SQL tables (a.k.a. Index-by tables) cannot be used as the basis for columns and/or attributes"There are three kinds of collections:
    (NTB) Nested Tables
    (VAR) Varrying Arrays
    (IBT) Index-by Tables (the collection formerly known as "PL/SQL tables")
    NTB (and VAR) can be defined as persistent user defined data types, and can be used in table DDL (columns) and other user defined type specifications (attributes).
    SQL> CREATE TYPE my_ntb AS TABLE OF INTEGER;
    SQL> CREATE TABLE my_table ( id INTEGER PRIMARY KEY, ints my_ntb );
    SQL> CREATE TYPE my_object AS OBJECT ( id INTEGER, ints my_ntb );
    /IBT are declared inside stored procedures only and have slightly different syntax from NTB. Only variables in stored procedures can be based on IBT declarations.
    CREATE PROCEDURE my_proc IS
       TYPE my_ibt IS TABLE OF INTEGER INDEX BY BINARY_INTEGER;  -- now you see why they are called Index-by Tables
       my_ibt_var my_ibt;
    BEGIN
       NULL;
    END;That sums up the significant differences as it relates to how they are declared and where they can be referenced.
    How are they the same?
    NTB and VAR can also be (non-persistently) declared in stored procedures like IBTs.
    Why would you then ever use IBTs?
    IBTs are significantly easier to work with, since you don't have to instantiate or extend them as you do with NTB and VAR, or
    Many other highly valuable PL/SQL programs make use of them, so you have to keep your code integrated/consistent.
    There's a lot more to be said, but I think this answers the question posed by Sri.
    Michael

  • Join data from a file with an internal table

    Hi to everybody!!
    I need help, I don't know how to join two differents data, one of them is internal...
    This select fill my itab
        SELECT PERNR PERID VORNA NACHN  FROM PA0002 APPENDING TABLE itab_tabla
            WHERE
            PA0002~BEGDA <= SY-DATUM AND
            PA0002~ENDDA >= SY-DATUM AND
            PERID  = G_PERID.
         ENDLOOP.
    Now I have to insert inside the table more data that pass from a file...but I don't know how to join this... can anybody help me?
    Thanks a lot
    Regards,
    Rebeca

    HI,
    you will need to upload the file by Using the FM GUI_UPLOAD.
    this will take the data from the file to an internal table that you will have to declare.
    once you get the data in to your table,you can merge both the tables in to another final table or in the 1st internal table if all the fields are there and modify the table.so the table will have the records of both the tables.
    after you get the data in your second table"
    loop at itab2.
    move: <field 1> to itab1.
              <field 2> to itab1.
    modify itab1.
    endloop.

  • How to remove a row from a jtable with DefaultTableModel

    Hi to all,]
    I want to remove a row from jtable. I am using DefaultTableModel.
    I have got some example but every example is given with AbstractTableModel
    Please help me...
    Thanks and Regards

    I want to remove a row from jtable. I am using DefaultTableModel.How do you program without reading the API.
    The name of the method you use is "removeRow". How hard is that to find by reading the API?
    Not only do you not bother to read the API, you don't even bother to read and respond to your old postings when you get help:
    http://forum.java.sun.com/thread.jspa?threadID=5137773
    http://forum.java.sun.com/thread.jspa?threadID=5134667
    http://forum.java.sun.com/thread.jspa?threadID=5131162
    Your on your own in the future.

  • Adding/deleting rows from a treeTable with a read-only view object

    Hello --
    I'm getting a JBO-25016 error when I go to delete a row from a treeTable. I want to create the hierarchy from a stored procedure, have the user add and remove nodes without calling back to the database. I will iterate through the tree and see what has changed and made the necessary updates on the database manually.
    What is the correct method for adding/removing rows form the UI component?
    Thanks!
    Tom

    Thank you, Amit! That did the trick.
    Edited by: Tom on Apr 28, 2011 11:51 AM

  • Getting ORA-00903:invalid table name with both system and table owner

    Hi All,
    Oracle version 9.2
    I'm trying to retrieve some information from a few tables and import them to Excel. I haven't got much idea about ORACLE, but I'm not able to do anything.
    I open SQL PLus, and use CONNECT SYSTEM/[email protected] AS SYSDBA
    The console shows connected.
    I use select table_name,owner from dba_tables where owner='USER1';
    I can see the tables I want to access in the output.
    I do select * from USER1.TABLE1 and also tried with select * from TABLE1, both return ORA-00903:invalid table
    I also tried to connect with "CONNECT USER1/[email protected]" also shows connected, but then same error ORA-00903:invalid table
    Could anyone guide me so I can find out whats going on wrong??
    Thanks.. Best regards!

    Great! this worked! At least now I know I can read the data.
    Now I would like to get the data from this table into Excel 2007, but I can't install Office in the ORACLE server, so I have setup my client computer (Windows 2003 server with Excel 2007).
    I installed the ORACLE ODBC driver, and put the TNSName.ora file into the network admin folder.
    I successfully create the ODBC connector, and try connection is successfull. However, when I try to get the data, Excel send an error saying that it cannot list the tables!
    Anyway, any simple solution will do. If there is an easy way of making ORACLE create for example a CSV file with all the data from the table it will be good as well. What would be the easiest way?

  • Tabular: Row filter on USERNAME() still returns all rows if current user is NOT matched in table??

    I have a table (basically the same setup in the various dynamic security in tabular blogs) where the table contains WindowsUserName stored as domain\username.
    I have a Row Filter on the table: SecurityTable has a filter of =SecurityTable[WindowsUserName]=USERNAME()
    When a user connects to the tabular model AND their name is matched in one of the rows they return JUST the rows with their match.  Which makes sense.
    However, if a user connects and their Windows user name is NOT in a row in that table....they return ALL rows???
    That makes no sense to me.
    It behaves almost as if it is doing a check and if it DOESN'T find a match it just returns all values.
    What am I missing?
    --------------EDIT-----------
    Found the issue: that use was (unknown to me) a domain admin, so... admin rights trump all :)

    Resolved:
    User had admin rights, didn't realize this at the time.

  • Join rows from 2 tables with non-matching numeric intervals

    Hi,
    Could you please help me with the following:
    I have 2 tables - [Coal_Samples] and [Coal Type] Which I have pasted below. I want to create a single result set that matches as closley as possible [Coal Type].Coal_Type to the interval between [Coal_Samples].mfrom and [Coal_Samples].mto. I would have
    [Coal_Samples] as the primary table using a left outer join.
    The problem is that the intervals [Coal_Samples].mfrom do not match [Coal Type].mfrom and [Coal_Samples].mto does not match [Coal Type].mto. I want to match the [Coal_Samples] intervals ([Coal_Samples].mfrom-[Coal_Samples].mto) as closely as possible to
    the [Coal_Type].Coal_Type field.
    I have been able to acheive this in excel using a VLOOKUP statement - but I want to know how to do this with and SQL Script without duplicating the rows based in [Coal_Samples].mfrom-[Coal_Samples].mto interval. 
    COAL SAMPLES TABLE
    ID
    mFrom
    mTo
    SAMPLE NUMBER
    AD261
    57
    57.5
    SAMPLE_001
    AD261
    57.5
    58
    SAMPLE_002
    AD261
    58
    59
    SAMPLE_003
    AD261
    59
    60
    SAMPLE_004
    AD261
    60
    61
    SAMPLE_005
    AD261
    61
    62
    SAMPLE_006
    AD261
    62
    63
    SAMPLE_007
    AD261
    63
    64
    SAMPLE_008
    AD261
    64
    65
    SAMPLE_009
    AD261
    65
    66
    SAMPLE_010
    AD261
    66
    67
    SAMPLE_011
    AD261
    67
    68
    SAMPLE_012
    AD261
    68
    69
    SAMPLE_013
    AD261
    69
    70
    SAMPLE_014
    AD261
    70
    71
    SAMPLE_015
    AD261
    71
    72
    SAMPLE_016
    AD261
    72
    73
    SAMPLE_017
    AD261
    73
    74
    SAMPLE_018
    AD261
    74
    75
    SAMPLE_019
    AD261
    75
    76
    SAMPLE_020
    AD261
    76
    77
    SAMPLE_021
    AD261
    77
    78
    SAMPLE_022
    AD261
    78
    79
    SAMPLE_023
    COAL   TYPE TABLE
    ID
    Type_mFrom
    Type__mTo
    COAL TYPE
    AD261
    57
    68.6
    BROWN COAL
    AD261
    68.6
    75
    GREY COAL
    AD261
    75
    78.2
    BLACK COAL
    AD261
    78.2
    79.2
    BLACK COAL
    Example   RESULT SET
    ID
    mFrom
    mTo
    DOMINANT_COAL TYPE
    AD261
    57
    57.5
    BROWN COAL
    AD261
    57.5
    58
    BROWN COAL
    AD261
    58
    59
    BROWN COAL
    AD261
    59
    60
    BROWN COAL
    AD261
    60
    61
    BROWN COAL
    AD261
    61
    62
    BROWN COAL
    AD261
    62
    63
    BROWN COAL
    AD261
    63
    64
    BROWN COAL
    AD261
    64
    65
    BROWN COAL
    AD261
    65
    66
    BROWN COAL
    AD261
    66
    67
    BROWN COAL
    AD261
    67
    68
    BROWN COAL
    AD261
    68
    69
    BROWN COAL
    AD261
    69
    70
    GREY COAL
    AD261
    70
    71
    GREY COAL
    AD261
    71
    72
    GREY COAL
    AD261
    72
    73
    GREY COAL
    AD261
    73
    74
    GREY COAL
    AD261
    74
    75
    GREY COAL
    AD261
    75
    76
    BLACK COAL
    AD261
    75
    76
    BLACK COAL
    AD261
    76
    77
    BLACK COAL
    AD261
    77
    78
    BLACK COAL
    AD261
    78
    79
    BLACK COAL
    Can you please help with the logic of this query?
    Thanks very much.

    Well, here is the beginning of the solution:
    ;with cte as (select S.*, T.[Coal Type] as [Dominant_Coal],
    case when 
    T.m_from between S.m_from and S.m_to
    AND T.m_to between S.m_from and S.m_to then 0
    when T.m_from between S.m_from and S.m_to then t.m_from - S.m_from
    else S.m_to - T.m_to end as [Diff]
    from Coal_Samples S LEFT JOIN Coal_Type T
    ON T.m_from between S.m_from and S.m_to
    OR T.m_to between S.m_from and S.m_to),
    cte1 as (select *, row_number() over (partition by cte.ID order by Diff) as Rn
    from cte)
    select * from cte1 where Rn = 1 -- attempt to get best match
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles

  • Bex Query - Show rows from another characteristic

    Hi Guys,
    I have a problem, I need to create a query to be displayed in Excel that shows accounting informacion by number, like this:
    Account         Balance
    12110201       1 USD
    12110202       1 USD
    12110204       1 USD
    That works so far, the problem is that I need to add 2 more columns and in 1 of the new columns I need to show only some accounts, like this:
    Account          Balance        NewColumn1
    12110201       1 USD      
    12110202       1 USD            Balance of 12110202
    12110204       1 USD
    the other one is more challenging because it need to show informacion from a different account, like this:
    Account          Balance        NewColumn1            NewColumn2
    12110201       1 USD      
    12110202       1 USD           Balance of 12110202
    12110204       1 USD                                           Balance of 2406910000
    the first values came from my accounts tables that contain balance from sub accounts, but the value 2406910000 is in fact a sub account value.
    Can some of you expers can help me on this? Is it possible?
    Please help.

    hmmmm
    let me ask this:
    You have multiprovider A
    in that u have Chars
    Account
    Subaccount
    Source of subaccount
    & Keyfigures
    $ amounts
    okey now, lets say u want to display the data as follow:
    $amount-----newcolumn1
    Account----
    2244--$2200--
    2245--$3200--
    bal. of 1234
    2246--$4200--
    2247--$1200-----bal. of 734
    is that what u are asking to display....
    if yes, bring Account in Rows
    bring $amount in Collumns
    add new selection1, in that add $amounts, then add Subaccounts from characteristics
    that way it will show $ for subaccount
    for e.g. in above case it will show $value for 1234 which is subaccount of 2245.
    My question is when you say "Balance of 22140000" what is that you intend to display here....you want to show $ of Balance i.e $45, $56 etc, or
    do you want to show Subaccount Number i.e 1234 or
    do u want to show specific texts "Balance of 22140000"
    my response above was related to this only, what is that you want to show in ur reports?
    what is available in ur multiprovider?
    if you wanna show specific texts  "Balance of 22140000", this cannot be done because you cannot just create texts in Query designers.
    If you wanna show specific $ amounts for that subaccount, answer is create restricted keyfigure
    if you wanna show Subaccount Number i.e 1234 etc, that means  you will have to create Replacement path variable and then replace values of Subaccount number into that column.
    Maybe you will not need cell definition then.
    i hope you understand what i am trying to explain..
    dont hesitate to reply with detailed explaination, and any questions.

  • Inserting a db row from a form with a value passed in from another page

    We have a report (P1) on a table (T1). P1 has a link column (C), and C is the PK on T1. Clicking on a link (i.e., a row) in report P1, takes us to form (P2). P2 is used to allow the user to insert a new row into T1, with the constraint that one of the columns (X) in T1 must have the same value as X had in the linked row on P1. Also, X is a NOT NULL attribute.
    What we are doing is:
    First, passing C to P2 by setting the link name=P2_X_C and the link value=#C#
    Second, in P2, we are setting form element P2_C via a SQL statement:
    SELECT X
    FROM T1
    WHERE C = :P2_X_C
    P2 displays nicely. We set P2_C to display only, and allow the user to enter values for the other columns. The value displayed for X is not NULL and correctly matches the value from P1. However, when we hit the CREATE button on P2, we get error: ORA-01400: cannot insert NULL into "T1"."X". Unable to process row of table T1.
    APEX seems to have an "issue" with having a value in a form "set" and transmitted to the DB on INSERT.
    Any help would be greatly appreciated.

    Hi,
    1 For "Display Only" items that you are not populating directly from a database column, if you want to save the value, you have to ensure that Save Session State is set to Yes and these items are not actually submitted with the page. The same sort of thing would apply to Disabled or Read-Only items as browsers do not submit these items - though these are more tricky to deal.
    2 Passing the PK value is the most common way of identifying which record contains the values you need. Passing other values is also typical - eg, for applying filters for calendars or searches or for setting flags.
    3 For database columns, which should be based on the underlying table's data, you can use the Default Value settings - though be aware that these values do not exist in session state until the page is submitted. That's ok if you don't need to use the value anywhere else on your page
    Andy

  • How to display rows from index 1 to all in a table

    Hi,
    I am facing a problem please help me.
    I created a node in context with cardinality 1..n so that there will always be a element present for that node.
    From one view i am providing data to this node and adding new blank element to this node.
    I binded a table from view to this node so that it is displaying all the rows with a blank row at the start.
    Now  i want to display all the elements of that node except that blank first element in to a table

    Hi Amol,
    You can invalidate the node before and then add the elements from the second table or you can remove the blank element from the node.
    you can use the following code to remove an element
    int listSize = wdContext.node<Node Name>().size();
    if(listSize > 0)
    for(int i = listSize - 1; i >= 0; --i)
    wdContext.node<Node Name>().removeElement(wdContext.node<Node Name>().getElementAt(i));
    or use the following code to invalidate the node.
    wdContext.node<Node Name>().invalidate();

  • Deleting a row from a resultSet with nulls

    Hello All,
    I have stumbled across a bit of a problem when using a cachedRowSet
    If I select columns from my database that have null values, and I call deleteRow() I get a
    NullPointerException.
    at com.sun.rowset.internal.CachedRowSetWriter.deleteOriginalRow
    However if I don�t select the null columns in my cachedRowSet. I can delete no worries.
    I would of though that the cachedRowSet object could handle nulls but it seems it doesn�t, anyone know any workarounds for this?
    NOTE: using JTDS driver would that play a part?

    If I select columns from my database that have null
    values, and I call deleteRow() I get a Does the whole row a null record ?
    Please put a sample program so that the problem can be reproduced.
    >
    NullPointerException.
    at
    com.sun.rowset.internal.CachedRowSetWriter.deleteOrigi
    nalRow
    Also give the stack trace.

  • How to export Tables along with Data and also Tables without data

    Hi All,
    I have a strange situation here. I have a 2 existing schema's under one database. Now the client wants to have 4 more schema's to incorporate the new branches of his company.
    I want to know whether is it possible for me to run an expdp command by which i can have the data from the mentioned tables and only table structure of the remaining along with remaining database objects (procedure,functions,triggers,views,sequences etc).
    Since there are some 32 Master tables, whose data i need to capture in db dump in order to run the batch under new schema and the remaining tables will be populated with data from the new branch employees hence the need is for table’s structure only.

    Hi,
    you should run two different import comand.
    The first import with only metadata, just to recreate the structure.
    With the second import you will import data only for the tables you need.
    I think this is the simplier solution.
    Acr

  • Varient table update with changes in sap table

    Hi,
    is there any way with which i can update a varient table with the new values in sap standar table.
    example can be, i created varient table T_TEST, its having 2 characterstic MAT_1 and MG_1,
    MAT_1 is key field and is is having material number from mara , MG_1 is material group.
    so can i update my varient table automaticaly each and every time a new material is created ?
    while going through SAP help i come across
    http://help.sap.com/saphelp_erp60_sp/helpdata/en/23/0be7d831cb11d2909c0000e8a49aad/content.htm
    but its for transfering data from varient table to new database table , i want it other way round.
    please reply if you did it in past.
    Thanks
    Best Regards
    Edited by: Ritesh on Nov 4, 2010 10:00 PM

    technical teams got the ways to do it , closing thread.

Maybe you are looking for

  • In creation of function module

    hi,          i created one rfc enabled function module to update the database table agr_define. but, is it possible to create custom function module in updating the any database table by using one function module only by giving the table name and fil

  • How to get rid of marketing 'Welcome to Portal' page

    When I start Portal after install I get the Welcome to Oracle Portal page with all the marketing stuff. How can I get rid of that and just go to the login screen? I tried to copy the link of the login page once I got there, but that does not work, I

  • My mac air want shut down, had to force shut after computer freezing in itunes, box with saving itunes library has been up for 30 mins

    I have had a day of it. wanted to join my new pc computer to my itune account. one problem to many devices on my account and two are ones that got broken and couldn't be deauthrise. sooo i deauthrised all computers. my mac air was sitting beside me s

  • From TOAD to EXCEL

    Hi, 1-Is it possible to export the data from TOAD to EXCEL ? How ? 2-Any other way to export the result of a query on a ORACLE table to EXCEL ? Many thanks before.

  • Edge S430 recovery is broken - what do?

    Hello there. I've had my Lenovo Thinkpad Edge S430-3EG for about a year now. It runs windows 7 64bit. Now i decided to reformat it. I made some mistakes along the way, and now my recovery program won't work. I'll start from the beginning. I used the