Joining three tables to get totals on three columns.

hi gurus,
i have spent hours and hours to get the desired result with this join query. 98% of the query bring results according to my expectations but for some reasons few results are duplicating. let me give you the structure of my tables than my query and than what i expect it to bring. here we go;
There are three tables
*1. SENDER*
Name Null? Type
SNAME VARCHAR2(50)
SPHONE1 NOT NULL VARCHAR2(15)
SPHONE2 VARCHAR2(15)
SPHONE3 VARCHAR2(15)
SCITY VARCHAR2(25)
SREMARKS VARCHAR2(200)
IDENTIFICATION VARCHAR2(20)
ADD1 VARCHAR2(30)
ADD2 VARCHAR2(30)
*2. TRANSACTION*
Name Null? Type
T_ID NOT NULL NUMBER
SPHONE1 VARCHAR2(15)
R_ID NUMBER
RB_ID VARCHAR2(10)
TRANSFER_TO VARCHAR2(25)
TDATE DATE
SOURCE VARCHAR2(20)
CURR VARCHAR2(10)
PAID VARCHAR2(10)
MOP VARCHAR2(10)
DOLLAR NUMBER
RATE NUMBER(5,2)
RUPPEE NUMBER
TREM1 VARCHAR2(200)
TREM2 VARCHAR2(100)
PPAYMENT VARCHAR2(10)
PROMISE VARCHAR2(50)
BALANCE NUMBER
DOLLAR2 NUMBER
*3. PAYMENTS*
Name Null? Type
PAYMENT_ID NOT NULL NUMBER
PDATE DATE
SPHONE1 VARCHAR2(15)
AMT_PAID NUMBER
BALANCE NUMBER
T_ID NUMBER
QUERY_
select a.sname, a.sphone1, nvl(sum(b.dollar),0) ORD, nvl(sum(b.dollar2),0) ADV,
nvl(sum(c.amt_paid),0) Paid
from sender a, transaction b, payments c
where a.sphone1 = b.sphone1
and b.t_id = c.t_id(+)
and b.paid in ('N','B')
group by a.sname, a.sphone1
order by 1
HERE IS THE RESULT OF THIS QUERY:_
SNAME SPHONE1 ORD ADV PAID
Muhammad Atif Ishtiaq 778 889 7195 2150 635 45
BRIEF DESCRIPTION:*
We have sender info in SENDER table, when they place order it goes to TRANSACTION table and when they pay it goes into PAYMENTS. some times they make payments in installment of a single order. which i record them in payments with their T_ID (Transaction ID) and this is the problem what i research until now. many customers place multiple orders over a period of time but dont pay and some times pay the orders in installment. i just want to present the whole story in one single row, as i mentioned above under query result. in this example the actual ORD suppose to be $2050 instead and ADV $605. PAID is OK.
SQL> select t_id, dollar, dollar2 from transaction where paid = 'B' and sphone1 = '778 889 7195';
T_ID DOLLAR DOLLAR2
18274 450
18278 1000 500
18263 100 30
18265 100
18266 100
18268 100 25
18269 200 50
SQL> select t_id, amt_paid from payments
where t_id in (select t_id from transaction
where paid = 'B' and sphone1 = '778 889 7195')
T_ID AMT_PAID
18263 20
18263 25
AND in the end i would like to calculate balance of the columns ((ORD+ADV) - (PAID))
Any help would be appreciated.
best regards.
NOTE: i am running oracle 8 and developer 6i

try this:
WITH TRANSACTION AS (select 18274 t_id, 450 dollar, NULL dollar2, '778 889 7195' sphone1 FROM DUAL
                     UNION ALL
                     select 18278 t_id, 1000 dollar, 500 dollar2, '778 889 7195' sphone1 FROM DUAL
                     UNION ALL
                     select 18263 t_id, 100 dollar, 30 dollar2, '778 889 7195' sphone1 FROM DUAL
                     UNION ALL
                     select 18265 t_id, 100 dollar, NULL dollar2, '778 889 7195' sphone1 FROM DUAL
                     UNION ALL
                     select 18266 t_id, 100 dollar, NULL dollar2, '778 889 7195' sphone1 FROM DUAL
                     UNION ALL
                     select 18268 t_id, 100 dollar, 25 dollar2, '778 889 7195' sphone1 FROM DUAL
                     UNION ALL
                     select 18269 t_id, 200 dollar, 50 dollar2, '778 889 7195' sphone1 FROM DUAL
     PAYMENTS AS    (select 18263 t_id, 20 amt_paid FROM DUAL
                     UNION ALL
                     select 18263 t_id, 25 amt_paid FROM DUAL
     SENDER AS      (SELECT 'Muhammad Atif Ishtiaq' SNAME, '778 889 7195' SPHONE1 FROM DUAL
select a.sname,
       a.sphone1,
       nvl(ORD,0) ORD,
       nvl(ADV,0) ADV,
       nvl(amt_paid,0) Paid
  from sender a,
       (SELECT sphone1,
               sum(dollar) ord,
               sum(dollar2) adv
          from transaction b
        GROUP BY sphone1) b,
       (SELECT sphone1,
               SUM(amt_paid) amt_paid
          FROM payments p,
               transaction t
         where p.t_id=t.t_id
         GROUP BY sphone1) c
where a.sphone1 = b.sphone1
and b.sphone1=c.sphone1(+);

Similar Messages

  • Please help - Joining three tables and get row values into Column. Please help!

    Hi,
    There is a SourceTable1 (Employee) with Columns like EmployeeID,Name,DOB.
    There is a sourcetable2 (EmployeeCode) with columns like EmployeeID,Code,Order.
    There is a source table 3  #EmployeeRegioncode  and its columns are (EmployeeID , RegionCode , [Order] 
    The target table 'EmployeeDetails' has the following details. EmployeeID,Name,DOB,Code1,Code2,Code3,Code4,regioncode1
    regioncode2 ,regioncode3 ,regioncode4 
    The requirement is , the value of the target table columns the Code1,code2,code3 ,code4,code5 values should
    be column 'Code' from Sourcetable2 where its 'Order' column is accordingly. ie) Code1 value should be the 'Code' value where [Order] column =1, and Code2 value should be the 'Code' value where [Order] =2, and so on.
    Same is the case for Source table 3- 'Region code' column also for the columns  regioncode1
    regioncode2 ,regioncode3 ,regioncode4 
    Here is the DDL and Sample date for your ref.
    IF OBJECT_ID('TEMPDB..#Employee') IS NOT NULL DROP TABLE #Employee;
    IF OBJECT_ID('TEMPDB..#EmployeeCode') IS NOT NULL DROP TABLE #EmployeeCode;
    IF OBJECT_ID('TEMPDB..#EmployeeDetails') IS NOT NULL DROP TABLE #EmployeeDetails;
    ---Source1
    CREATE table #Employee 
    (EmployeeID int, Empname varchar(20), DOB date )
    insert into #Employee VALUES (1000,'Sachin','1975-12-12') 
    insert into #Employee VALUES (1001,'Sara','1996-12-10') 
    insert into #Employee  VALUES (1002,'Arjun','2000-12-12')
    ---Source2
    CREATE table #EmployeeCode 
    (EmployeeID int, Code varchar(10), [Order] int)
    insert into #EmployeeCode VALUES (1000,'AA',1) 
    insert into #EmployeeCode VALUES (1000,'BB',2)   
    insert into #EmployeeCode  VALUES (1000,'CC',3)  
    insert into #EmployeeCode VALUES  (1001,'AAA',1)  
    insert into #EmployeeCode  VALUES  (1001,'BBB',2)  
    insert into #EmployeeCode  VALUES  (1001,'CCC',3)  
    insert into #EmployeeCode  VALUES  (1001,'DDD',4)  
    insert into #EmployeeCode  VALUES  (1002,'AAAA',1)  
    insert into #EmployeeCode  VALUES  (1002,'BBBB',2)  
    insert into #EmployeeCode  VALUES  (1002,'CCCC',3)  
    insert into #EmployeeCode  VALUES  (1002,'DDDD',4)  
    insert into #EmployeeCode  VALUES  (1002,'EEEE',5)  
    ---Source tbl 3
    CREATE table #EmployeeRegioncode 
    (EmployeeID int, RegionCode varchar(10), [Order] int)
    insert into #EmployeeRegioncode VALUES (1000,'xx',1) 
    insert into #EmployeeRegioncode VALUES (1000,'yy',2)   
    insert into #EmployeeRegioncode  VALUES (1000,'zz',3)  
    insert into #EmployeeRegioncode VALUES  (1001,'xx',1)  
    insert into #EmployeeRegioncode  VALUES  (1001,'yy',2)  
    insert into #EmployeeRegioncode  VALUES  (1001,'zz',3)  
    insert into #EmployeeRegioncode  VALUES  (1001,'xy',4)  
    insert into #EmployeeRegioncode  VALUES  (1002,'qq',1)  
    insert into #EmployeeRegioncode  VALUES  (1002,'rr',2)  
    insert into #EmployeeRegioncode  VALUES  (1002,'ss',3)  
    ---Target
    Create table #EmployeeDetails
    (EmployeeID int, Code1 varchar(10), Code2 varchar(10),Code3 varchar(10),Code4 varchar(10),Code5 varchar(10) , regioncode1 varchar(10),
    regioncode2 varchar(10),regioncode3 varchar(10),regioncode4 varchar(10))
    insert into #EmployeeDetails  VALUES (1000,'AA','BB','CC','','','xx','yy','zz','')  
    insert into #EmployeeDetails  VALUES (1001,'AAA','BBB','CCC','DDD','','xx','yy','zz','xy')  
    insert into #EmployeeDetails VALUES (1002,'AAAA','BBBB','CCCC','DDDD','EEEE','qq','rr','ss','')  
    SELECT * FROM  #Employee
    SELECT * FROM  #EmployeeCode
    SELECT * FROM  #EmployeeRegioncode
    SELECT * FROM  #EmployeeDetails
    Can you please help me to get the desired /targetoutput?  I have sql server 2008.
    Your help is greatly appreciated.

    select a.EmployeeID,b.code1,b.code2,b.code3,b.code4,b.code5,c.Reg1,c.Reg2,c.Reg3,c.Reg4 from
    #Employee a
    left outer join
    (select EmployeeID,max(case when [Order] =1 then Code else '' end) code1,
    max(case when [Order] =2 then Code else '' end)code2,
    max(case when [Order] =3 then Code else '' end)code3,
    max(case when [Order] =4 then Code else '' end)code4,
    max(case when [Order] =5 then Code else '' end)code5 from #EmployeeCode group by EmployeeID) b
    on a.EmployeeID=b.EmployeeID
    left outer join
    (select EmployeeID,max(case when [Order] =1 then RegionCode else '' end) Reg1,
    max(case when [Order] =2 then RegionCode else '' end)Reg2,
    max(case when [Order] =3 then RegionCode else '' end)Reg3,
    max(case when [Order] =4 then RegionCode else '' end)Reg4 from #EmployeeRegioncode group by EmployeeID) c
    on a.EmployeeID=c.EmployeeID
    Thanks
    Saravana Kumar C

  • While Joining 3 tables am getting 2 rows,actual i need a single row

    Hi all,
        While joining 3 tables am getting 2 Rows, i need  1 row.
    please find my query and actual output and expected output.
    Help me out to handle this.
    select
            --MFU_D_C_INVESTOR_ID_DET,
            mfu_remove_junk(Q.CAN)                               CAN_ST,
            mfu_remove_junk(Q.INSTALLMENT_AMOUNT) AMOUNT_ST,
            mfu_remove_junk(x.Can )                                Can ,
            mfu_remove_junk(x.Amc_Code )                      Amc_Code ,
            mfu_remove_junk(x.Folio_No )                         Folio_No ,
            mfu_remove_junk(x.Order_Status )                  Order_Status ,
            mfu_remove_junk(x.User_Code )                    User_Code ,
            mfu_remove_junk(x.User_Txrn_No )               User_Txrn_No ,
            mfu_remove_junk(x.Group_Ord_No )              Group_Ord_No ,
            mfu_remove_junk(x.Ind_Txn_Ref_No )            Ind_Txn_Ref_No ,
            mfu_remove_junk(x.Pending_Txn_Ref_No )    Pending_Txn_Ref_No ,
            mfu_remove_junk(x.Appl_No )                       Appl_No ,
            mfu_remove_junk(x.Ts_Machine_Id )             Ts_Machine_Id ,
            mfu_remove_junk(x.Trxn_Date )                    Trxn_Date ,
            mfu_remove_junk(x.Trxn_Time )                    Trxn_Time ,
            mfu_remove_junk(x.Timestamp_No )             Timestamp_No ,
            mfu_remove_junk(x.Sch_Code )                   Sch_Code ,
            mfu_remove_junk(x.Reinv_Tag )                    Reinv_Tag ,
            mfu_remove_junk(x.Txn_Mode )                   Txn_Mode ,
            mfu_remove_junk(x.Trxn_Type )                   Trxn_Type ,
            mfu_remove_junk(x.Sub_Trxn_Type )           Sub_Trxn_Type ,
            mfu_remove_junk(x.Units )                          Units ,
            mfu_remove_junk(x.Amount )                      Amount ,
            mfu_remove_junk(x.All_Units_Flag )            All_Units_Flag ,
            mfu_remove_junk(x.Entity_Id )                    Entity_Id ,
            mfu_remove_junk(x.ENTITY_BRANCH_ID )  ENTITY_BRANCH_ID ,
            mfu_remove_junk(x.Location )                     Location ,
           (TO_CHAR(sysdate,'DD-MON-RRRR'))        CREATED_DATE
         From MFU_FTP_XML_FILES T
      Left Join Xmltable('/COMM_TXN_STRUCT/FILE_ROWS/FILE_ROW'
                          Passing T.Filecontent
                          Columns
    --MFU_D_C_INVESTOR_ID_DET
    Can                 Varchar2(500) Path  'INVESTOR_ID_DET/CAN',
    Amc_Code            Varchar2(500) Path  'INVESTOR_ID_DET/AMC_CODE',
    Folio_No            Varchar2(500) Path  'INVESTOR_ID_DET/FOLIO_NO',
    Order_Status        Varchar2(500) Path  'TRANSACTION_DET/ORDER_STATUS',
    User_Code           Varchar2(500) Path  'TRANSACTION_DET/USER_CODE',
    User_Txrn_No        Varchar2(500) Path  'TRANSACTION_DET/USER_TXRN_NO',
    Group_Ord_No        Varchar2(500) Path  'TRANSACTION_DET/GROUP_ORD_NO',
    Ind_Txn_Ref_No      Varchar2(500) Path  'TRANSACTION_DET/IND_TXN_REF_NO',
    Pending_Txn_Ref_No  Varchar2(500) Path  'TRANSACTION_DET/PENDING_TXN_REF_NO',
    Appl_No             Varchar2(500) Path  'TRANSACTION_DET/APPL_NO',
    Ts_Machine_Id       Varchar2(500) Path  'TRANSACTION_DET/TS_MACHINE_ID',
    Trxn_Date           Varchar2(500) Path  'TRANSACTION_DET/TRXN_DATE',
    Trxn_Time           Varchar2(500) Path  'TRANSACTION_DET/TRXN_TIME',
    Timestamp_No        Varchar2(500) Path  'TRANSACTION_DET/TIMESTAMP_NO',
    Sch_Code            Varchar2(500) Path  'TRANSACTION_DET/SCH_CODE',
    Reinv_Tag           Varchar2(500) Path  'TRANSACTION_DET/REINV_TAG',
    Txn_Mode            Varchar2(500) Path  'TRANSACTION_DET/TXN_MODE',
    Trxn_Type           Varchar2(500) Path  'TRANSACTION_DET/TRXN_TYPE',
    Sub_Trxn_Type       Varchar2(500) Path  'TRANSACTION_DET/SUB_TRXN_TYPE',
    Units               Varchar2(500) Path  'TRANSACTION_DET/UNITS',
    Amount              Varchar2(500) Path  'TRANSACTION_DET/AMOUNT',
    All_Units_Flag      Varchar2(500) Path  'TRANSACTION_DET/ALL_UNITS_FLAG',
    Entity_Id           Varchar2(500) Path  'TRANSACTION_DET/ENTITY_ID',
    ENTITY_BRANCH_ID    Varchar2(500) Path  'TRANSACTION_DET/ENTITY_BRANCH_ID',
    Location            Varchar2(500) Path  'TRANSACTION_DET/LOCATION'
    ) X
    on (1=1)
         left join XMLTABLE('/CT_SYS_STRUCT/FILE_ROWS/FILE_ROW'
                   PASSING T.FILECONTENT
                   columns
    CAN                varchar2(500)  PATH 'INVESTOR_ID_DET/CAN',
    INSTALLMENT_AMOUNT varchar2(500)  PATH 'TRANSACTION_DET/INSTALLMENT_AMOUNT'
    )Q
    on (1=1)
    Where
    TRUNC((TO_DATE(T.RETRIVED_DATE,'DD-MON-RRRR HH:MI:SS PM')))=TRUNC(SYSDATE-2)
    Order By Rownum,X.Can Asc;
    Actual O/P:
    15114LBA01  1000.00     (null)               (null)         (null)     (null)      (null)                 (null)               (null)                  
            (null)      (null)         15114LBA01    SMF         AC       MFU    40715117000    15114010001   1511401000102      
    Expected O/P:
    can_st                amount     can                 amc    order  user      trxn_no             group_ord         ind_txn_no
       15114LBA01   1000.00  
    15114LBA01    SMF 
    AC 
    MFU    40715117000    15114010001    1511401000102 

    Expected O/P:
    can_st      amount  can       amc order user trxn_no     group_ord   ind_txn_no
    15114LBA01 1000.00 15114LBA01 SMF
    AC
       MFU 40715117000 15114010001 1511401000102
    check http://docs.oracle.com/cd/E11882_01/server.112/e41084/ap_keywd001.htm#SQLRF55621
    to verify:
    with
    actual_output as
    (select '15114LBA01' can_st,1000.00 amount,null can,null amc,null order_,null user_,null trxn_no,null group_ord,null ind_txn_no from dual union all
    select null,null,'15114LBA01','SMF','AC','MFU',40715117000,15114010001,1511401000102 from dual
    select max(can_st) can_st,max(amount) amount,max(can) can,max(amc) amc,max(order_) order_,max(user_) user_,max(trxn_no) trxn_no,max(group_ord) group_ord,max(ind_txn_no) ind_txn_no
      from actual_output
    CAN_ST
    AMOUNT
    CAN
    AMC
    ORDER_
    USER_
    TRXN_NO
    GROUP_ORD
    IND_TXN_NO
    15114LBA01
    1000
    15114LBA01
    SMF
    AC
    MFU
    40715117000
    15114010001
    1511401000102
    select max(can_st) can_st,max(amount) amount,max(can) can,max(amc) amc,max(order_) order,max(user_) user,max(trxn_no) trxn_no,max(group_ord) group_ord,max(ind_txn_no) ind_txn_no
      from actual_output
    ORA-00923: FROM keyword not found where expected
    Regards
    Etbin

  • Get Total of a Column of Advanced Table and use it in Controller in OAF

    I have an advanced table on my custom page. I am calculating total of a column in my footer. I am acheiving this using the standard functionality of advanced table by setting total value property of my column to "TRUE" and displaying the total in the tablefooter components.
    In addition to this I want to retreive the total value from my footer and use it in my Controller for further validations but I am not able to acheive this.
    I tried some code already present in the Community for the same but nothing turned out right for me.
    Can anyone please help me on this.

       Hi there ,
      You can manually add the values of each row of a  column and make use of the value obtained in your controller .
    Here is the logic ,
    1) get the vo instnace attached to that table .
    2) Loop through every single row in the table .
    3) get the value and sum up the value and use it in your controller .
    int fetchRowCount = vo.getFetchRowCount();  
    voRowImpl row = null;  
    int count =0;
    if (fetchRowCount  > 0) { 
    RowSetIterator iter = vo.createRowSetIterator("Iter"); 
    iter.setRangeStart(0);
      iter.setRangeSize(fetchBidderRowCount);
       for (int i = 0; i < fetchBidderRowCount; i++) {
      row = voRowImpl iter.getRowAtRangeIndex(i);
       //use RowImpl getters   10.   
             Number personId = row.getPersonId();
    count =personId+count;         // add each and every value and have in variable
    iter.closeRowSetIterator();
    Regards ,
    Keerthi

  • Joining three tables to get specific data set

    table1
    id
    seq
    dat
    table2
    id
    seq
    empid
    taxid
    Table3
    empid
    taxid
    I want to find out records That have same id,seq,empid having different taxid column Common to three of the tables .
    Please help me

    Hi,
    There are a lot of different things you could mean.
    Here's how to do one of them:
    SELECT       t2.id,  t2.seq,  t2.empid
    FROM       table2     t2
    JOIN       table1     t1     ON     t2.id        = t1.id
                          AND     t2.seq        = t1.seq
    JOIN       table3     t3     ON     t2.emp_id  = t3.empid
                          AND     t2.taxid   = t3.taxid
    GROUP BY  t2.id,  t2.seq,  t2.empid
    HAVING       COUNT (DISTINCT t2.taxid) > 1
    ;It would help if you posted some sample data (CREATE TABLE and INSERT statements) from all 3 tables, and the results you want from that data.

  • How to join two tables and get the supply delivery date next to order?

    So there are two tables. One has customer's order no, ordered date, order quantity, available quantity and code of article-
    The other table comes form supply side where we have supply order no, article number, ordered qty, and delivery date.
    We keep stock so this can not be MOT (made to order) system.
    What i need is correct date of arrival to appear next to cusotmers spoecirfic order. The older cusotmers order get's the parts first, second oldest order is next in line etc.
    here is any example
    customer's order
    ref order
    art. code
    ordered qty
    available qty
    order date
    1809202491
    700497
    60
    0
    3.7.2014
    1809200528
    700497
    13
    0
    20.6.2014
    1809198640
    700497
    7
    0
    9.6.2014
    supply order
    supply order
    art. code
    qty orderd
    date of arrival
    4501243378
    700497
    50
    4.8.2014
    4501263437
    700497
    20
    6.10.2014
    There is actually a 3rd "table" and that sort of connects the two and that is stock on hand per art. code.
    The main issue is that stock is assigned to purchase orders only when it actually arrives in the warehouse.
    A human can easilly connect the dates of when the stock will arrive and quantities with correct customer's order. In this case the firts order will get 50 pcs in August while 10 pcs will remain on backorders. The missing 10 pcs Will arrive in October. The second order will get 10 pcs in october and 3 will remain on backorders with no delivery date. While the third customer orders does not have a delivery date.
    So how to make the SAP do this calculations and display the arrival date next to date of customer's order?

    I checked the instructions as i do not have access to this part. It seem this is a query. We had issues with queries in the past as not all codes from orders would appear in them. They never found the reason why that is happening.
    However, I think the main issue is that the information here is not connected and is separately provided for supply and for sales. So i doubt it can be connected in this query.
    edit: as you can see the only connection is stock on hand.
    and total number of various items we have is close to 100.000 of various article codes.

  • Select and calculate average on joining two tables - normal and archive.

    Hello,
    I am in process of writing a query to calculate (daily/or weekly/or monthly)average time taken by orders raised from different system. Order creation date is in one table, and order finalization date is in another table.
    Now both these tables are getting archived every three months, so in order to get the average for orders before these three months, i am forced to make an union of two archive tables for entry date, and two archives table for the end date. And this query is taking a lot of time to be executed. I am not a DB guy and have less idea of running the execution plan. Any help on this will be a great help for me.
    My current query is as below:
    SELECT DOP.SYSTEMID,AVG( H1.ENTRYDATE - DOP.ENTRYDATE) AS AVERAGETIME,
                TO_CHAR(DOP.ENTRYDATE ,'DD/MM/YY')  AS CREATIONDATE
                  FROM (
                     SELECT SYSTEMID,ENTRYDATE, INTERNALORDERID FROM ORDERS_TAB WHERE
                        ENTRYDATE
                        BETWEEN TO_DATE('01/10/2008','DD/MM/YY')
                        AND TO_DATE('21/10/2009','DD/MM/YY')
                     UNION  ALL
                     SELECT SYSTEMID,ENTRYDATE, INTERNALORDERID FROM ORDERS_TAB_ARCH
                        WHERE ENTRYDATE
                        BETWEEN TO_DATE('01/10/2008','DD/MM/YY')
                        AND TO_DATE('21/10/2009','DD/MM/YY')
                     )  DOP, 
                     SELECT ENTRYDATE,INTERNALORDERID FROM ORDERS_HEAD_HISTORY
                     WHERE
                        ENTRYDATE >TO_DATE('01/10/2008','DD/MM/YY')  AND STATUSID = 7
                     UNION ALL
                     SELECT ENTRYDATE,INTERNALORDERID FROM ORDERS_HEAD_HIST_ARCH
                        WHERE ENTRYDATE >TO_DATE('01/08/2008','DD/MM/YY')  AND STATUSID = 7 
                     ) H1
                       WHERE DOP.INTERNALORDERID=H1.INTERNALORDERID
                       GROUP BY DOP.MASTERSYSTEMID, TO_CHAR(DOP.ENTRYDATE ,'DD/MM/YY')Regards,
    Edited by: A R on Oct 23, 2009 9:37 AM

    Thanks etbin. Yes I am joining the archive table only when the date contains that of archive data. I am doing it conditionally via java.
    @damorgan.
    Version:
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
    PL/SQL Release 10.2.0.3.0 - Production
    "CORE     10.2.0.3.0     Production"
    TNS for Solaris: Version 10.2.0.3.0 - Production
    NLSRTL Version 10.2.0.3.0 - Production
    Query plan
    <ExplainPlan>
      <PlanElement object_ID="0" id="0" operation="SELECT STATEMENT" optimizer="ALL_ROWS" cost="276.594" cardinality="12.565" bytes="678.510" cpu_cost="38.788.639.859" io_cost="266.656" time="1.646">
        <PlanElements>
          <PlanElement object_ID="0" id="1" operation="HASH" option="GROUP BY" cost="276.594" cardinality="12.565" bytes="678.510" cpu_cost="38.788.639.859" io_cost="266.656" time="1.646">
            <PlanElements>
              <PlanElement object_ID="0" id="2" operation="NESTED LOOPS" cost="276.591" cardinality="12.565" bytes="678.510" cpu_cost="38.777.027.861" io_cost="266.656" time="1.646">
                <PlanElements>
                  <PlanElement object_ID="1" id="3" operation="VIEW" object_owner="HIDE_USER" object_instance="4" cost="276.587" cardinality="2" bytes="48" cpu_cost="38.777.004.963" io_cost="266.652" time="1.646">
                    <PlanElements>
                      <PlanElement object_ID="0" id="4" operation="UNION-ALL">
                        <PlanElements>
                          <PlanElement object_ID="0" id="5" operation="PARTITION LIST" option="ALL" cost="55.239" cardinality="1" bytes="17" partition_start="1" partition_stop="12" partition_id="5" cpu_cost="5.061.473.580" io_cost="53.942" time="329">
                            <PlanElements>
                              <PlanElement object_ID="2" id="6" operation="TABLE ACCESS" option="FULL" optimizer="ANALYZED" object_owner="HIDE_OBJECT_OWNER" object_name="ORDERS_HEAD_HISTORY" object_type="TABLE" object_instance="5" cost="55.239" cardinality="1" bytes="17" partition_start="1" partition_stop="12" partition_id="5" cpu_cost="5.061.473.580" io_cost="53.942" filter_predicates="&quot;ENTRYDATE&quot;&gt;TO_DATE('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') AND &quot;STATUSID&quot;=7" time="329"/>
                            </PlanElements>
                          </PlanElement>
                          <PlanElement object_ID="0" id="7" operation="PARTITION LIST" option="ALL" cost="221.348" cardinality="1" bytes="17" partition_start="1" partition_stop="12" partition_id="7" cpu_cost="33.715.531.383" io_cost="212.710" time="1.318">
                            <PlanElements>
                              <PlanElement object_ID="3" id="8" operation="TABLE ACCESS" option="FULL" optimizer="ANALYZED" object_owner="HIDE_OBJECT_OWNER" object_name="ORDERS_HEAD_HIST_ARCH" object_type="TABLE" object_instance="6" cost="221.348" cardinality="1" bytes="17" partition_start="1" partition_stop="12" partition_id="7" cpu_cost="33.715.531.383" io_cost="212.710" filter_predicates="&quot;ENTRYDATE&quot;&gt;TO_DATE('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') AND &quot;STATUSID&quot;=7" time="1.318"/>
                            </PlanElements>
                          </PlanElement>
                        </PlanElements>
                      </PlanElement>
                    </PlanElements>
                  </PlanElement>
                  <PlanElement object_ID="1" id="9" operation="VIEW" object_owner="HIDE_USER" object_instance="1" cost="2" cardinality="1" bytes="30" cpu_cost="11.449" io_cost="2" time="1">
                    <PlanElements>
                      <PlanElement object_ID="0" id="10" operation="UNION ALL PUSHED PREDICATE">
                        <PlanElements>
                          <PlanElement object_ID="4" id="11" operation="TABLE ACCESS" option="BY GLOBAL INDEX ROWID" optimizer="ANALYZED" object_owner="HIDE_OBJECT_OWNER" object_name="ORDERS_TAB" object_type="TABLE" object_instance="2" cost="1" cardinality="1" bytes="23" partition_start="ROW LOCATION" partition_stop="ROW LOCATION" partition_id="11" cpu_cost="5.730" io_cost="1" filter_predicates="&quot;ENTRYDATE&quot;&gt;=TO_DATE('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') AND &quot;ENTRYDATE&quot;&lt;=TO_DATE('2009-10-21 00:00:00', 'yyyy-mm-dd hh24:mi:ss')" time="1">
                            <PlanElements>
                              <PlanElement object_ID="5" id="12" operation="INDEX" option="UNIQUE SCAN" optimizer="ANALYZED" object_owner="HIDE_OBJECT_OWNER" object_name="ORDERS_TAB_PK" object_type="INDEX (UNIQUE)" search_columns="1" cost="1" cardinality="1" cpu_cost="3.823" io_cost="1" access_predicates="&quot;INTERNALORDERID&quot;=&quot;H1&quot;.&quot;INTERNALORDERID&quot;" time="1"/>
                            </PlanElements>
                          </PlanElement>
                          <PlanElement object_ID="6" id="13" operation="TABLE ACCESS" option="BY GLOBAL INDEX ROWID" optimizer="ANALYZED" object_owner="HIDE_OBJECT_OWNER" object_name="ORDERS_TAB_ARCH" object_type="TABLE" object_instance="3" cost="1" cardinality="1" bytes="23" partition_start="ROW LOCATION" partition_stop="ROW LOCATION" partition_id="13" cpu_cost="5.719" io_cost="1" filter_predicates="&quot;ENTRYDATE&quot;&gt;=TO_DATE('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') AND &quot;ENTRYDATE&quot;&lt;=TO_DATE('2009-10-21 00:00:00', 'yyyy-mm-dd hh24:mi:ss')" time="1">
                            <PlanElements>
                              <PlanElement object_ID="7" id="14" operation="INDEX" option="UNIQUE SCAN" optimizer="ANALYZED" object_owner="HIDE_OBJECT_OWNER" object_name="ORDERS_TAB_A_PK" object_type="INDEX (UNIQUE)" search_columns="1" cost="1" cardinality="1" cpu_cost="3.823" io_cost="1" access_predicates="&quot;INTERNALORDERID&quot;=&quot;H1&quot;.&quot;INTERNALORDERID&quot;" time="1"/>
                            </PlanElements>
                          </PlanElement>
                        </PlanElements>
                      </PlanElement>
                    </PlanElements>
                  </PlanElement>
                </PlanElements>
              </PlanElement>
            </PlanElements>
          </PlanElement>
        </PlanElements>
      </PlanElement>
    </ExplainPlan>All the columns involved are index.

  • JOIN 2 tables that have same column ?

    I need to learn how to join two tables that both have the same column name:
    tbl1 - idskey
    tbl2 - idskey
    the idskey column holds a id_number
    When I do the JOIN I would like to make sure that only Distinct records are joined from both tables and that any duplicates are removed in the final join. So if:
    Tbl1 has a idskey of: 12345
    and
    Tbl2 has a idskey of: 12345
    In the final JOIN I want to remove one of those duplicates.
    I actually need to join 3 tables that have the same linking column names for the join, but if I learn how to do this correctly on 2, that will be a start.
    10g for db, thanks!

    Hi,
    SELECT DISTINCT and GROUP BY are the most common ways to get unique results from non-unique keys. Exactly how you use them depends on exactly what you want to do.
    SELECT DISTINCT guarantees that no two rows in the result set, conisdering all columns, will be identical.
    GROUP BY produces one row from a set of rows that have a common feature. The values on that row may be a composite of values from various rows in that set (e.g., an average).
    Please post a small, specific example. For instance:
    "I have two rows in tbl1 ...
    and these fhtee rows in tbl2 ...
    Notice how there is one row with idskey=12345 in tbl1 but two such rows in tbl2.
    How can I get theses results ...
    where only one row has idskey=12345?"

  • How to prevent Oracle from using an index when joining two tables ...

    How to prevent Oracle from using an index when joining two tables to get an inline view which is used in an update statement?
    O.K. I think I have to explain what I mean:
    When joining two tables which have many entries sometimes it es better not to use an index on the column used as join criteria.
    I have two tables: table A and table B.
    Table A has 4.000.000 entries and table B has 700.000 entries.
    I have a join of both tables with a numeric column as join criteria.
    There is an index on this column in table A.
    So I instead of
      where (A.col = B.col)I want to use
      where (A.col+0 = B.col)in order to prevent Oracle from using the index.
    When I use the join in a select statement it works.
    But when I use the join as inline view in an update statement I get the error ORA-01779.
    When I remove the "+0" the update statement works. (The column col is unique in table B).
    Any ideas why this happens?
    Thank you very much in advance for any help.
    Regards Hartmut

    I think you should post an properly formatted explain plan output using DBMS_XPLAN.DISPLAY including the "Predicate Information" section below the plan to provide more details regarding your query resp. update statement. Please use the \[code\] and \[code\] tags to enhance readability of the output provided:
    In SQL*Plus:
    SET LINESIZE 130
    EXPLAIN PLAN FOR <your statement>;
    SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);Usually if you're using the CBO (cost based optimizer) and have reasonable statistics gathered on the database objects used the optimizer should be able to determine if it is better to use the existing index or not.
    Things look different if you don't have statistics, you have outdated/wrong statistics or deliberately still use the RBO (rule based optimizer). In this case you would have to use other means to prevent the index usage, the most obvious would be the already mentioned NO_INDEX or FULL hint.
    But I strongly recommend to check in first place why the optimizer apparently seems to choose an inappropriate index access path.
    Regards,
    Randolf
    Oracle related stuff:
    http://oracle-randolf.blogspot.com/
    SQLTools++ for Oracle:
    http://www.sqltools-plusplus.org:7676/
    http://sourceforge.net/projects/sqlt-pp/

  • How to join KNA1 table with SOOD, SOST or SOES table?

    Hi,
    I am creating an infoset and I cant join KNA1 with any of the following tables: SOST, SOES or SOOD.
    I am looking to get the following fields from the following table:
    KNA1 - KUNNR: Customer Number
    SOES - STATUS: Status
    SOOD- OBJNAM : Send Method
    SOOD - OBJDES : Document Title
    SOOD - CRONAM : Created by
    SOES- MSGV1 : Recipient
    SOST - STAT - DATE : Send date
    SOST - STAT - TIME : Send time
    I tried adding knvp table to find a common field but cant seem to join them. SOST, SOES and SOOD are easily joined with each other but KNA1 or KNVP cannot be joined, is there any intermediate table that I should use to join these tables to get the customer number against them or is there any other solution. kindly help.
    Regards,
    Moaz

    Hi,
    please try to connect KNA1 with SOOD using
    CALL METHOD cl_binary_relation=>read_links
       EXPORTING
         is_object           = ls_lpor
         it_relation_options = lt_relat
       IMPORTING
         et_links            = lt_links.
    where ls_por-instid = kna1-kunnr, ls_por-typeid = 'KNA1' and ls_por-catid = 'BO'
    and  lt_relat contains a line option 'I' 'EQ' 'NOTE'.
    Regards,
    Klaus

  • Getting no result by joining three tables in BMM

    Hi all,
    I have a requirement where I have a fact table and two ailas dimension table.
    Let say fact is w_account_f and dimension ailas dim_w_account_d_trans and dim_w_account_d_sal of w_account_d table
    I have join in physical layer
    w_account_f ----> dim_w_account_d_trans with forgin key join
    w_account_f ----> dim_w_account_d_sal with forgin key join
    In BMM i have join these three table but given content level filter in dimension like
    dim_w_account_d_trans.tans_col = 'active'
    dim_w_account_d_sal.sal > 4000
    when i am trying to pull the report from all three table
    w_account_f, dim_w_account_d_trans, dim_w_account_d_sal
    its not giving any result where as when i trying to pull report with fact any one dimension its giving me result.
    can anyone help me in finding the soulting how to get the data by joining all these three tables.

    Hi,
    Take the physical query with all three tables selected. Run the query in sql developer, remove the joins one by one and check where it fails. Modify the rpd model accordingly.
    Regards,
    Dpka

  • Please tell me the  Three table INNER JOIN Sql statement

    Hi experts,
      I got requirement like by using INNER JOIN i have to write the code in MY program i.e using 3 tables VBAK VBAP and VBUK.And the common field is VBELN .SO please give the  INNER JOIN SQL statement for above three tables...
    vbak-vbeln,erdat
    vbap-vbeln,posnr
    vbuk-vbeln,RFSTK
    Thanks in Advance

    hi guglani  please see my total code b.caus DATA is not extracting...once see the code  correct error.
    DATA:V_VBELN TYPE VBAK-VBELN.      "VBAK,VBAP AND VBUK
    SELECT-OPTIONS SORDER FOR V_VBELN.
    TYPES:BEGIN OF T_VBAK,
         VBELN TYPE VBELN_VA,
         ERDAT TYPE ERDAT,
      END OF T_VBAK.
    TYPES:BEGIN OF T_VBAP,
           VBELN TYPE VBELN_VA,
            POSNR TYPE POSNR_VA,
      END OF T_VBAP.
    TYPES:BEGIN OF T_VBUK,
        VBELN TYPE VBELN,
      RFSTK TYPE RFSTK,
      END OF T_VBUK.
    TYPES:BEGIN OF FS,
       VBELN TYPE VBELN_VA,
         ERDAT TYPE ERDAT,
    POSNR TYPE POSNR_VA,
      RFSTK TYPE RFSTK,
      END OF FS.
    DATA:WA1 TYPE T_VBAK,
         WA2 TYPE T_VBAP,
         WA3 TYPE T_VBUK,
         WA TYPE FS.
    DATA:ITAB1 TYPE TABLE OF T_VBAK,
          ITAB2 TYPE TABLE OF T_VBAP,
          ITAB3 TYPE TABLE OF T_VBUK,
          ITAB TYPE TABLE OF FS.
    select a~vbeln a~erdat b~posnr c~rfstk
           from vbak as a inner join vbAP as b on a~vbeln = b~vbeln
                          inner join vbuk as c on a~vbeln = c~vbeln
    into table itab
    where A~vbeln eq SORDER.
    IF NOT ITAB IS INITIAL.
      SORT ITAB BY VBELN.
    ENDIF.
    LOOP AT ITAB INTO WA.
       WRITE:/ WA-VBELN,WA-ERDAT,WA-rfstk.
       ENDLOOP.
       CLEAR WA.
       REFRESH ITAB.

  • Joining three tables

    I have to join three tables but i m getting error as given below:
    Error i m getting-
    Syntax error(missing operator) in query expression 'invoice.INVOICE_NO=inv_ent_main.INVOICE_NO INNER JOIN invrate ON invrate.INVOICE_NO=inv_ent_main.INVOICE_N'.
    My Query is-
    "select inv_ent_main.EXPORTER,inv_ent_main.INVOICE_NO,inv_ent_main.INVOICE_DATE,inv_ent_main.BALE_COUNT,inv_ent_main.TOTAL_AMOUNT,inv_ent_main.BALE_COUNT,inv_ent_main.TOTAL_PIECES,inv_ent_main.TOTAL_AREA,invoice.ORDER_NO,invoice.OTHER_REF,invoice.CONSIGN1,invoice.CONSIGN2,invoice.CONSIGN3,invoice.CONSIGN4,invoice.CONSIGN5,invoice.DLV_TERMS,invoice.PAYMENT_TERMS,invoice.PRE_CARRIAGE,invoice.PR_CARRIER,invoice.DESTINATION,invoice.COUNTRY_DESTINATION,invoice.VESS_FLIGHT,invoice.PORT_LOAD,invoice.PORT_DSCH,invoice.MARK1,invoice.MARK2,invoice.CURRENCY,invoice.GOODS_DESCRIPTION,invoice.CFB,invoice.GROSS_WEIGHT,invoice.NET_WEIGHT,invoice.WOOL,invoice.COTTON
    from inv_ent_main INNER JOIN invoice ON invoice.INVOICE_NO = inv_ent_main.INVOICE_NO INNER JOIN invrate ON invrate.INVOICE_NO = inv_ent_main.INVOICE_NO";
    MCA,LUCKNOW.

    The last character in the error message has missing "O" when compare to the actual query you have posted. Please rerun it an verify or you can try the below query. You need to post DDL for all the 3 tables so that we can test.
    select
    inv_ent_main.EXPORTER,
    inv_ent_main.INVOICE_NO,
    inv_ent_main.INVOICE_DATE,
    inv_ent_main.BALE_COUNT,
    inv_ent_main.TOTAL_AMOUNT,
    inv_ent_main.BALE_COUNT,
    inv_ent_main.TOTAL_PIECES,
    inv_ent_main.TOTAL_AREA,
    invoice.ORDER_NO,
    invoice.OTHER_REF,
    invoice.CONSIGN1,
    invoice.CONSIGN2,
    invoice.CONSIGN3,
    invoice.CONSIGN4,
    invoice.CONSIGN5,
    invoice.DLV_TERMS,
    invoice.PAYMENT_TERMS,
    invoice.PRE_CARRIAGE,
    invoice.PR_CARRIER,
    invoice.DESTINATION,
    invoice.COUNTRY_DESTINATION,
    invoice.VESS_FLIGHT,
    invoice.PORT_LOAD,
    invoice.PORT_DSCH,
    invoice.MARK1,
    invoice.MARK2,
    invoice.CURRENCY,
    invoice.GOODS_DESCRIPTION,
    invoice.CFB,
    invoice.GROSS_WEIGHT,
    invoice.NET_WEIGHT,
    invoice.WOOL,
    invoice.COTTON from inv_ent_main
    INNER JOIN invoice ON invoice.INVOICE_NO = inv_ent_main.INVOICE_NO
    INNER JOIN invrate ON invrate.INVOICE_NO = inv_ent_main.INVOICE_NO
    Regards, RSingh

  • How to get data from three tables (A,B,C) having one to many relation between A and B .and having one to many reation between b and c

    i have  three tables A,B,C.  there is one to many relation between A and B. and one to many relation existed between table b and c . how will get data from these three tables

    check if this helps:
    select * --you can always frame your column set
    from tableA a
    left join tableB b on a.aid=b.aid
    left join tableC c on c.bid=b.bid
    This is just a general query. However, we can help you a lot more, if you can post the DDL + sample data and required output.
    Thanks,
    Jay
    <If the post was helpful mark as 'Helpful' and if the post answered your query, mark as 'Answered'>

  • SQL Join Three Tables DW MX2004 ASP

    I'm trying to display records using three tables joined as
    shown in the SQL code below. I'm getting "no data" message when I
    test the result page from the search form. I'm using the advanced
    Dreamweaver form to enter the SQL commands. I have never used this
    feature of Dreamweaver before and I'm not sure if the code below
    follows the way DW MX will understand what I'm trying to do. Below
    is the code based on Standard SQL....not sure if DW compatible...
    This is what I typed in the SQL box:
    SELECT
    ResourceSkillMapping.resourceSkillMapID,ResourceSkillMapping.skillID,
    ResourceSkillMapping.competenceLevel,skill.skillname,resource.resourceName,resource.resour ceLoginId
    FROM ResourceSkillMapping INNER JOIN Skill ON
    ResourceSkillMapping.skillID = Skill.skillID
    INNER JOIN Resource ON
    ResourceSkillMapping.resourceSkillMapID =
    Resource.resourceSkillMapID
    WHERE (Skill.skillName = '%Skill_asp%') AND
    (Resource.resourceLoginID = '%Agent_asp%')
    AND (Resource.active = 1)
    These is typed in the variables box:
    Variable Default Runtime
    Skill_asp 1 Request.QueryString("skillname")
    Agent_asp 1 Request.QueryString("resourceLoginID")
    I'm using asp and the "Get" url parameters in the search form
    calling the result recordset in DW. I'm connecting to a SQL 2000
    server. Is this notation DW correct? Am I joining the tables
    correctly. The tables are ResourceSkillMapping, Skill and Resource.
    All I need is to be able to search by ResourceLoginID and by
    skillName on all active records (active=1). The plan is to be able
    to click on a result record and go to a detail page to update the
    "CompetencyLevel" field in the resourceSkillMapping table using the
    update behavior in DW.
    Please help as this project is due this Tue and have been
    trying every posibility I could think so far....I have read all the
    DW help facilities many pages on this and still keep getting the
    "no data" message
    Thank you,
    Carlos
    [email protected]

    >> WHERE (Skill.skillName = '%Skill_asp%') AND
    (Resource.resourceLoginID =
    >>'%Agent_asp%')
    I would say that this is where your problem is. Change it to:
    WHERE (Skill.skillName LIKE '%Skill_asp%') AND
    (Resource.resourceLoginID
    LIKE
    '%Agent_asp%')
    and your query should work, unless of course you are actually
    searching for
    2 strings that read %Skill_asp% and %Agent_asp% exactly.
    Pat.
    "ITCoreTeam" <[email protected]> wrote in
    message
    news:[email protected]...
    > I'm trying to display records using three tables joined
    as shown in the
    > SQL
    > code below. I'm getting "no data" message when I test
    the result page from
    > the
    > search form. I'm using the advanced Dreamweaver form to
    enter the SQL
    > commands.
    > I have never used this feature of Dreamweaver before and
    I'm not sure if
    > the
    > code below follows the way DW MX will understand what
    I'm trying to do.
    > Below
    > is the code based on Standard SQL....not sure if DW
    compatible...
    > This is what I typed in the SQL box:
    >
    > SELECT
    >
    ResourceSkillMapping.resourceSkillMapID,ResourceSkillMapping.skillID,
    >
    >
    ResourceSkillMapping.competenceLevel,skill.skillname,resource.resourceName,resou
    > rce.resourceLoginId
    > FROM ResourceSkillMapping INNER JOIN Skill ON
    ResourceSkillMapping.skillID
    > =
    > Skill.skillID
    > INNER JOIN Resource ON
    ResourceSkillMapping.resourceSkillMapID =
    > Resource.resourceSkillMapID
    > WHERE (Skill.skillName = '%Skill_asp%') AND
    (Resource.resourceLoginID =
    > '%Agent_asp%')
    > AND (Resource.active = 1)
    >
    > These is typed in the variables box:
    > Variable Default Runtime
    > Skill_asp 1 Request.QueryString("skillname")
    > Agent_asp 1 Request.QueryString("resourceLoginID")
    >
    > I'm using asp and the "Get" url parameters in the search
    form calling the
    > result recordset in DW. I'm connecting to a SQL 2000
    server. Is this
    > notation
    > DW correct? Am I joining the tables correctly. The
    tables are
    > ResourceSkillMapping, Skill and Resource. All I need is
    to be able to
    > search by
    > ResourceLoginID and by skillName on all active records
    (active=1). The
    > plan is
    > to be able to click on a result record and go to a
    detail page to update
    > the
    > "CompetencyLevel" field in the resourceSkillMapping
    table using the update
    > behavior in DW.
    > Please help as this project is due this Tue and have
    been trying every
    > posibility I could think so far....I have read all the
    DW help facilities
    > many
    > pages on this and still keep getting the "no data"
    message
    > Thank you,
    > Carlos
    > [email protected]
    >

Maybe you are looking for