Using MAX on an INNER JOIN

Is there any way to use MAX on an INNER JOIN???
I am getting the MAX Date row on a previous INNER JOIN and then I want to use that result row to get an Account Balance. I know it's hard to explain without the SQL but I'll try...
So I want the MAX(BILLHDR.dt_bill)'s Account Balance...
SELECT PYMTADJ.ky_payment_adjustment,
PYMTADJ.ky_enroll,
PYMTADJ.dt_payment_adjustment,
PYMTADJ.at_payment_adjustment,
MAX(BILLHDR.dt_bill) Last_Bill_Date,
MAX(BILLHDR.dt_reading_from) "Last Bill Period From Date",
MAX(BILLHDR.dt_reading_to) "Last Bill Period To Date",
BILLHDR2.at_bill_account_balance
FROM PVIEW.PAYMENT_ADJUSTMENT PYMTADJ
LEFT OUTER JOIN PVIEW.BILL_HDR BILLHDR
ON PYMTADJ.ky_enroll = BILLHDR.ky_enroll
AND BILLHDR.dt_bill <= '15-Jun-2009'
INNER JOIN PVIEW.BILL_HDR BILLHDR2
ON BILLHDR.ky_enroll = BILLHDR2.ky_enroll
AND MAX(BILLHDR.dt_bill) = BILLHDR2.dt_bill
Any help would be GREATLY appreciated.
Thanks.
PSULionRP

[email protected] wrote:
But now the next riddle is this. I have another table I have to query against using those MAX Dates. Is there any way to use those MAX Dates as my Join criteria for another table???Sure, it is called in-line view:
SELECT whatever-you-need
  FROM (
SELECT   pymtadj.ky_payment_adjustment, pymtadj.ky_enroll,
         pymtadj.dt_payment_adjustment, pymtadj.at_payment_adjustment,
         MAX (billhdr.dt_bill) last_bill_date,
         MAX (billhdr.dt_reading_from) "Last Bill Period From Date",
         MAX (billhdr.dt_reading_to) "Last Bill Period To Date",
         MAX (billhdr2.at_bill_account_balance)KEEP (DENSE_RANK FIRST ORDER BY billhdr.dt_bill DESC) some_alis
    FROM pview.payment_adjustment pymtadj LEFT OUTER JOIN pview.bill_hdr billhdr
         ON pymtadj.ky_enroll = billhdr.ky_enroll
       AND billhdr.dt_bill <= TO_DATE ('15-Jun-2009', 'DD-Mon-YYYY')
         LEFT OUTER JOIN pview.bill_hdr billhdr2
         ON billhdr.ky_enroll = billhdr2.ky_enroll
GROUP BY pymtadj.ky_payment_adjustment,
         pymtadj.ky_enroll,
         pymtadj.dt_payment_adjustment,
         pymtadj.at_payment_adjustment
) inline_view JOIN another_table ON(inline_view."Last Bill Period From Date" = another_table.some_column1)SY.

Similar Messages

  • About a question using Update ... inner join ?

    select *
    FROM a
    INNER JOIN b ON a.ProductID=b.ProductID
    WHERE a.HeadID='000246'
    this statement is ok ;
    But the following statement does not work ! Why ?
    UPDATE a SET
    a.Quantity=a.PurchaseQuantity/b.ConversionGene
    FROM a
    INNER JOIN b ON a.ProductID=b.ProductID
    WHERE a.HeadID='000246'

    "Because Oracle syntactically does not support that type of construct..." Is a correct statement, but not because "It expects only one table in UPDATE statement". The synatax for an updateable join in Oracle requires a "proper" in-line view to be updated.
    As long as the table joined (in my example t1) has a declared unique constraint on the columns used to join by (in my example id), you can do it like:
    SQL> SELECT * FROM t;
            ID DESCR
             1 One
             2 Two
             3 Three
    SQL> SELECT * FROM t1;
            ID DESCR2
             1 Un
             2 Deux
    SQL> UPDATE (SELECT t.descr, t1.descr2
      2          FROM t
      3             JOIN t1 ON t.id = t1.id)
      4  SET descr = descr2;
    2 rows updated.
    SQL> SELECT * FROM t;
            ID DESCR
             1 Un
             2 Deux
             3 ThreeTTFN
    John

  • Regarding : What is used in place of inner join

    hi,
    i want to know that we use to have inner joins in our reports and i want to know is there anything which can be used in place of it?

    Hi,
    In place of inner join you can use  FOR ALL ENTRIES.
    reward point if useful.
    Regards
    Kumar M

  • Using MANDT field in Inner joins

    Experts,
    I need your suggestions on using MANDT field in JOINS. Example
    Select afield1 afield2 b~field1 from table1 as a
    inner join table 2 b on amandt = bmandt
                                     afield1 = bfield1
    where......
    Will it be a performance issue if I use the join without MANDT field?
    Anand

    probably stated in the note:
    mandt in the WHERE condition is automatically added, but not in ON-condition.
    Some databases had problems to figure out, that the mandit is always the same, i.e. did not use an index on second table. So adding mandt in ON condition can have impacts. In newer database releases this is no problem anymore. But anyway, the additional line in the ON-condition is no big deal.
    Siegfried

  • Help: Modifying inner join

    Hello Folks,
    I have an Access Query and am rewriting the whole query in Oracle to run a report.The thing is Am a beginner in oracle and am scratching my head on how to modify this inner join. please help me in rewriting this inner join in oracle. thanks a million
       ((Debtor INNER JOIN MAX_TRANS_DATE AS MAX_TRANS_DATE_1 ON Debtor .
            EVENT_ID = MAX_TRANS_DATE_1.EVENT_ID) INNER JOIN
            TMP$ALL_RECOVERY_TYPE
            ON(Debtor . EVENT_CASE_ID = TMP$ALL_RECOVERY_TYPE . EVENT_CASE_ID) AND
            (Debtor . EVENT_ID = TMP$ALL_RECOVERY_TYPE . EVENT_ID))
    INNER JOIN CUBS TRANSACTIONS
        ON (Debtor . EVENT_ID = CUBS TRANSACTIONS . EVENT_ID)
       AND (Debtor . EVENT_CASE_ID = CUBS TRANSACTIONS . EVENT_CASE_ID)

    Thanks for getting back. But still am unable to get the correct data. I will try to break it down.
    Microsoft Access Table MAX_TRANS_DATE menitoned above in the inner join was originally coming from table called TRANSACTIONS i.e.( MAX_TRANS_DATE = SELECT [Transactions].[EVENT_ID], Max([Transactions].[TRANSDATE]) AS MaxOfTRANSDATE
    FROM Debtor INNER JOIN Transactions ON [Debtor].[EVENT_ID]=[Transactions].[EVENT_ID]
    GROUP BY [Transactions].[EVENT_ID];
    Here the problem is am recreating the access database query in oracle database and we dont have MAX_TRANS_DATE in the Oracle database but we do have TRANSACTIONS table.
    So i just used TRANSACTIONS in the inner join instead of MAX_TRANS_DATE.
    But the result is wrong.The Field Transdate from TRANSACTIONS is pulling dates as 10/5/2006 instead of 02/01/2010. I suspect something is wrong with the joins.Please help me. I can provide the old access query and the new oracle query that i have created.
    Thanks
    Edited by: user11961230 on Mar 5, 2010 11:49 AM

  • Bind variables in inner join queries

    Hi all,
    can we use bind variables in inner join queries?
    eg:
    INNER JOIN PRTY_LOC_CODE_T plc ON (ppt.PRTY_REC_ID = plc.PRTY_REC_ID and plc.HIST_CTRL_IND = *0*_ and plc.DEL_IND = *'N'*_)
    regards
    sunil

    Dear,
    can we use bind variables in inner join queries?Where are you using this join? if in a stored procedure or stored function than you don't have to care about your variables. It will be automatically considered as a bind variable
    within your static SQL (i.e in your stored procedure)
    Hope this helps
    Mohamed Houri

  • Problem when doing inner join for three tables....

    DATA: V3 TYPE VBAP-MAKTL.
    SELECT single vbap~matkl into V3
           from VBAP
           inner join VBFA
           inner join LIPS
              on LIPSPOSNR = VBFAPOSNN
              and vbapvbeln = vbfavbelv
               and vbapposnr = vbfaposnv
                 where LIPSVBELN = VBFAVBELN = '0080000834'
                 and vbfa~vbtyp_n = 'J'
                 and LIPS~J_3ASIZE = '0884'.
    Error: Wrong expression "INNER" in from clause...

    Hello Tushar,
      Why you are using the 3 table inner join. If the volume of data is big then this statement will give to time out. Use "for all entries" statement to select the data from the table.
    Like
    select field1 field2 from LIPS into i_lips
        where LIPS~J_3ASIZE = '0884'.
    select f3 f4 from vbfa into i_vbfa for all entries in i_lips where field1 = i_lips-field1 and so on
    and 
    use same to select data from VBAP.
    Hope this will help

  • Using the max function in a inner joined statement

    I am trying to take from a database with two joined tables various datas from the record belonging to paul that has the highest entry number.
    String query = "SELECT Table1.name, Table1.idNumber, Table2.Address Table2.entryNum FROM Table1 INNER JOIN Table2 ON Table1.idNumber = Table2.idNumber WHERE Table2.entryNum = max ( Table2.entryNum) AND Table1.name = 'Paul'";
    The error message i get is.
    Cannot have aggregate function in WHERE clause
    Thanks for any help

    I am trying to take from a database with two joined
    tables various datas from the record belonging to paul
    that has the highest entry number.SELECT
    Table1.name,
    Table1.idNumber,
    Table2.Address
    Table2.entryNum
    FROM Table1 INNER JOIN Table2 ON Table1.idNumber = Table2.idNumber
    WHERE Table2.entryNum =
    ( select max( Table2.entryNum) from table1, table2
    where table1.idNumber=table2.idNumber
    AND Table1.name = 'Paul'" );

  • Relationship between tables while using inner joins.

    Hi,
    I had a few clarifications on "inner joins", and as i was going through the forum, i came across two points,
    1. In one of the threads it said " inner joins is applicable for tables that have a parent child relationship.
    2. In another thread it said " inner join is established from master table (the table on the left) to the transcation table (the table on the right)".
    I have two clarifications based on the above points.
    1. Is it necessary that the tables on which im performing an inner join should have a parent-child/children relationship or is it enough that the tables just have a common field.
    2.  Also is it necessary that the master table should come first, (or can i use any child table from where i can fetch the records when there is a mater table in my report) as shown below.
    Eg: select * <fields> from <master table> inner join <table> on <field> into <itab>.
    Edited by: Narayananchandran on Dec 27, 2010 12:31 PM

    have two clarifications based on the above points.
    1. Is it necessary that the tables on which im performing an inner join should have a parent-child/children relationship or is it enough that the tables just have a common field.
    2. Also is it necessary that the master table should come first, (or can i use any child table from where i can fetch the records when there is a mater table in my report) as shown below.
    Eg: select * <fields> from <master table> inner join <table> on <field> into <itab>
    1) NO
                      2) NO

  • How to use Inner join of table as Source in Merge statement in SQL

    Hi All,
        I am trying to make source as multiple tables output using Join while coding there is no any syntax error but when i am executing this statement is giving following error
    Following is the query 
    Merge Into EmpDept Target
    Using (select E.Address,e.Design,e.EmailId,e.EmpId,e.Ename,e.ManagerId, e.Salary,D.DeptId,D.DeptName,D.Location from Employee E Inner join Dept D on E.DeptId=D.DeptId )As Source (Address,Design,EmailId,EmpId,EName,ManagerId,Salary,DeptId,DeptName,Location)
    On Source.EmpId=Target.EmpId
    when not matched then
    Insert (Target.Address,Target.Design,Target.EmailId,Target.EmpId,Target.Ename,Target.ManagerId, Target.Salary,Target.DeptId,Target.DeptName,Target.Location)
    values
    (Address,Design,EmailId,EmpId,EName,ManagerId, Salary,DeptId,DeptName,Location)
    When matched then 
    Update set Target.Address = Source.Address ,Target.Design = Source.Design,Target.EmailId      = Source.EmailId     ,Target.Ename       = Source.Ename      ,Target.ManagerId = Source.ManagerId , Target.Salary        = Source.Salary       ,Target.DeptId      = Source.DeptId      ,Target.DeptName = Source.DeptName ,Target.Location    = Source.Location;
    This is error while executing the above merge statement 
    The insert column list used in the MERGE statement cannot contain multi-part identifiers. Use single part identifiers instead.
    Please suggest me where i am wrong.. 
    Niraj Sevalkar

    MERGE INTO EmpDept Target
    Using (SELECT E.Address,
    e.Design,
    e.EmailId,
    e.EmpId,
    e.Ename,
    e.ManagerId,
    e.Salary,
    D.DeptId,
    D.DeptName,
    D.Location
    FROM Employee E
    INNER JOIN Dept D
    ON E.DeptId = D.DeptId) AS Source (Address, Design, EmailId, EmpId, EName, ManagerId, Salary, DeptId, DeptName, Location)
    ON Source.EmpId = Target.EmpId
    WHEN NOT matched THEN
    INSERT (Address,
    Design,
    EmailId,
    EmpId,
    Ename,
    ManagerId,
    Salary,
    DeptId,
    DeptName,
    Location)
    VALUES (Address,
    Design,
    EmailId,
    EmpId,
    EName,
    ManagerId,
    Salary,
    DeptId,
    DeptName,
    Location)
    WHEN matched THEN
    UPDATE SET Address = Source.Address,
    Design = Source.Design,
    EmailId = Source.EmailId,
    Ename = Source.Ename,
    ManagerId = Source.ManagerId,
    Salary = Source.Salary,
    DeptId = Source.DeptId,
    DeptName = Source.DeptName,
    Location = Source.Location;

  • Why can't i use "INNER JOIN" in a query for SQL Server with JDBC??????

    Hi,
    I'm trying to execute some SQL queries and I just don't understand what's wrong.
    I�m using Tomcat and SQL Server in order to do this, but when I�m try to execute a query with a INNER JOIN statements Tomcat raise a SQL exception... at the very first time I thought there was a problem with database connection but I realize that a simple query to a table works pretty well. then I found out some problems with JDBC:ODBC.... so I install JDBC for SQL Server 2000 and test with the same simple query and works..... so, I come to a conclusion.... INNER JOIN or JOIN statements can't be used in JDBC..... please... somebody tell I�m wrong and give me a hand...
    I'm using TOMCAT 4 and JDK 1.4 SQL Server 2000
    Error occurs when executeQuery() is called.... not prepareStatement().... ??????
    Driver DriverRecResult = (Driver)Class.forName(driver).newInstance();
    Connection ConnRecResult = DriverManager.getConnection(DSN,user,password);
    PreparedStatement StatementRecResult = ConnRecResult.prepareStatement(query);
    ResultSet RecResult = StatementRecResult.executeQuery(); <---- Exception raise here
    So much tahnks in advance,

    That's exactly what I think, driver it's raising the exception, but I don't know why.... i test the same query with INNER JOIN directly from SQL Query Analyser and it's works perfectly, my problem ain't SQL, but JSP and JDBC 'cause i'm a newbie about these issues.
    Common sense tell me possible problems lie in SQLServer drivers 'cause i run the same pages on JRUN through jdbc:odbc and do works well, but by now i just depend on Tomcat.....
    I've installed SQL Server drivers for JDBC but i just find it doesn't work fully... could be the version of JDK i've installed? what version do i need?
    ( I'm running Tomcat 4 with JDK 1.4 & SQL Server 2000 W2K )
    thanks for reply.

  • How to use INNER JOIN in such case

    when i program as below[CASE1]. the code is able to active.
    CASE1:
    ==========================
    DATA: WK_BUKRS LIKE T001-BUKRS,
          WK_BUTXT LIKE T001-BUTXT,
          WK_TABLE(4) TYPE C VALUE 'T001'.
    START-OF-SELECTION.
      WK_BUKRS = 'DECN'.
      PERFORM GET_BUTXT USING WK_BUKRS WK_TABLE
                     CHANGING WK_BUTXT.
      WRITE: WK_BUTXT.
      FORM GET_BUTXT USING I_BUKRS LIKE T001-BUKRS
                           I_TABLE
                  CHANGING O_BUTXT LIKE T001-BUTXT.
        SELECT SINGLE BUTXT
                 INTO O_BUTXT
                 FROM (I_TABLE)
                WHERE BUKRS = I_BUKRS.
      ENDFORM.                   
    ===========================
    but when I need to INNER JOIN another table [CASE2]
    CASE2:
    =======================
    DATA: WK_BUKRS LIKE T001-BUKRS,
          WK_BUTXT LIKE T001-BUTXT,
          WK_TABLE(4) TYPE C VALUE 'T001'.
    START-OF-SELECTION.
      WK_BUKRS = 'DECN'.
      PERFORM GET_BUTXT USING WK_BUKRS WK_TABLE
                     CHANGING WK_BUTXT.
      WRITE: WK_BUTXT.
      FORM GET_BUTXT USING I_BUKRS LIKE T001-BUKRS
                           I_TABLE
                    CHANGING O_BUTXT LIKE T001-BUTXT.
        SELECT SINGLE BUTXT
                 INTO O_BUTXT
                 FROM (I_TABLE) AS G INNER JOIN BKPF AS H
                   ON GBUKRS = HBUKRS
                WHERE G~BUKRS = I_BUKRS.
      ENDFORM.          
    =================================
    Syntax error:
    Wrong expression "INNER" in FROM clause. WHERE condition.
    Can anybody help me to solve the problem.
    My requirement is to use INNER JOIN with variable table

    hi slam,
    chk this sample code.
    hi,
    table emp
    empno name
    a sasi
    b xxx
    c yyy
    table sal
    empno salary
    a 1000
    b 2000
    Inner join
    select eempno ename
    s~sal
    into table int_table
    from emp as e
    inner join sal as s
    on
    eempno = sempno.
    if you made inner join between table a and b by emp no
    the selection retrives only if the condition satisfy the output will be
    a sasi 1000
    b xxx 2000
    rgds
    anver
    if hlped mark points
    Message was edited by: Anversha s

  • Inner join query used with 7 Database tables

    HI All,
    In a report they used the Inner join Query with 6 Data base table..now there is a performance issue with at query.
    its taking so much of time to trigger that query. Please help how to avoid that performance issue for that.
    In that 2 database tables containing lakhs of records..
    According to my knowledge it can be avoided by using secondary indexs for those 2 database tables..
    and by replacing the Inner join Query with FOR ALL ENTRIES statement.
    i want how to use the logic by using FORALL ENTRIES statement for this..
    So, please give you proper suggestion to avoid this issue..
    Thanking you.
    Moderator message: Please Read before Posting in the Performance and Tuning Forum
    Edited by: Thomas Zloch on Oct 16, 2011 10:27 PM

    Hi,
    And what do you mean with "they used"? If "SAP used" then yo will need to ask a SAP for note
    FOR ALL ENTRIES is quite good described in help. Please search forum also.
    Without query it won't be possible to tell how it can be optimized, however you can try to use SE30/SAT and ST05. Maybe it will help you.
    BR
    Marcin Cholewczuk

  • How to use BKPF and BSEG without using  inner join

    Hi,
    can anybody plz tell me the logic about how to complete the report without inner joining bkpf and bseg.Is this report can be really be made by using only bseg,bkpf ?
    *selection-criteria .
    BKPF-USNAM
    BKPF-CPUDT
    BKPF-BUDAT
    BKPF-GJAHR
    BKPF-BUKRS
    BKPF-BELNR
    BSEG-SAKNR
    BSEG-LIFNR
    BSEG-KUNNR
    BSEG-ZUONR
    BSEG-SGTXT
    Output fields required.
    BKPF-BUKRS
    BKPF-BELNR
    BKPF-GJAHR
    BKPF-MONAT
    BKPF-BLART
    BKPF-CPUDT
    BKPF-BLDAT
    BKPF-BUDAT
    BKPF-USNAM
    BKPF-XBLNR
    BKPF-STBLG
    BKPF-BKTXT
    BKPF-WAERS
    BKPF-KURSF
    BSEG-BUZEI
    BSEG-BSCHL
    BSEG-KOART
    BSEG-UMSKZ
    BSEG-SHKZG
    BSEG-MWSKZ
    BSEG-PSWBT
    BSEG-PSWSL
    BSEG-DMBTR
    BSEG-SAKNR
    BSEG-HKONT
    BSEG-KOSTL
    BSEG-KUNNR
    BSEG-VBELN
    BSEG-LIFNR
    BSEG-ANLN1
    BSEG-ANLN2
    BSEG-AUFNR
    BSEG-MATNR
    BSEG-MENGE
    BSEG-MEINS
    BSEG-ZUONR
    BSEG-SGTXT
    Thanks,
    Rahman
    Moderator Message: Please search before posting your question. Thread locked.
    Edited by: Suhas Saha on Jan 31, 2012 3:07 PM

    Quick question - what's preventing your from using built-in functions?
    I suggest mapping your Time Dim to a standard Time Dim and work with it - you'll save a lot more time. You'd even need this table if you wanted to work around Todate/AGO.
    It is possible to model this functionality w/o functions, but it'll be time consuming task, similar to using Time Series Wizard in Siebel Analytics. You'll need to build aliases and views along with some complex joins (such as TIME.KEY=TIME.KEY-365), introduce a bunch of variables to control. Also, you might not get much flexibility in terms of years. For each year, you'd need an alias table. (TIME_DIM_PY).

  • Drag-n-n-drop query joins uses WHERE, not INNER JOIN syntax

    When I highlight a few tables and drag them onto the sql worksheet, it will build a select statement for me and join the tables by their foreign keys.
    That's a nice feature, thanks!
    Three questions. Is it possible to:
    1. get it to use the INNER JOIN and LEFT OUTER JOIN syntax instead of joining the tables in the WHERE clause?
    2. control the table aliases so that it will automatically use the "standard alias" for the table instead of A, B, C, etc.?
    3. get it to not put the schema name into the query?
    Thanks!
    Edited by: David Wendelken on Nov 22, 2008 1:48 PM. Grammar mistake.

    Hi Gopi,
    Your code is Good.
    But try to avoid Inner join with more number of Tables ...because this is a performance issue..
    try to use..
    select (primary key fields mainly,other fields) from LIKP into itab where bolnr in p_bolnr(paramater).
    next try to use for all entries option..
    select (primary key fields mainly,other fields) from VBFA for all entries in itab where (give the condition)....
    simillarly do for the other select ....ok this will try to reduce the performance issue....
    <b><REMOVED BY MODERATOR></b>
    Message was edited by:
            Alvaro Tejada Galindo

Maybe you are looking for

  • Confirmation of order

    dear all,               can u pls let me know the difference between confirmation of order by CORK and COR6N in process industry. rgds Amitabh

  • How to use Multiple Hierachies.

    Hi friends, Iam new to BW reporting . In one of my report ia want to use more than one hierarhies for GL account. But it is allowing me only on hierarchy at a time . I tried using creating variables but still allowing me only one hierarchy at a time

  • Old fonts from System 7 & Mac OS 8 & OS 9: Safe to install in 10.6.4?

    I've been accumulating fonts for years and years. I recently got a new MacBook Pro and it's now running 10.6.4. I still have, backed up on an external drive, hundreds of freeware, shareware and pro-level fonts which I acquired way back in the distant

  • What accessories do I need to enable us to watch a movie on tv which is on my ipad?

    What accessories do I need to buy to enable us to transfer film from ipad to tv?.                         Thanks, linda.

  • How to open link in new window using Mountain Lion

    When I upgraded to Mountain Lion, opening a link closes the window I'm in when it opens the new one.  Example, I'm in email, click on a link, and then have to reopen email.  The back button doesn't work.  Where is the setting to change this so my cur