Help in join of two tables

Hi,
I have following 2 tables
CREATE TABLE a
sr_no INT
CREATE TABLE b
sr_no INT,
startdate DATE,
remark VARCHAR2(30)
One to many mapping relationship exists between the two tables.
for each sr_no in table a ther exists multiple/no record in table b.
If there are multiple entries in table b for a single entry in table a,
then i want remark data which is corresponding to minimum of the startdate in table b
Example data of table b
1     10/15/2009     one
1     10/29/2009     one1
2     10/28/2009     two
3     10/27/2009     three
4     10/26/2009     four
minimum date of the first 2 records is 10/15/2009.so i want remark as "one" when i join the 2 tables.
How do we achieve that ?

Something like this?
SELECT  SUB.SR_NO
,       SUB.STARTDATE
,       B.REMARK
FROM
        SELECT  SR_NO
        ,       MIN(STARTDATE) AS STARTDATE
        FROM   B
        GROUP BY SR_NO
)       SUB
JOIN    B       ON B.SR_NO = SUB.SR_NO AND B.STARTDATE = SUB.STARTDATEThat is one possibility. There are many, many ways to achieve this kind of result.

Similar Messages

  • Help in joining of two tables

    Hi, I want to join table #1 and #2 and want to get results from #1 but want to  include all timelines of #1 and #2 in results. Please help on this query. Thanks.
    create table #1 (code_p char(4), code_h char(2), code_date datetime, Area char(2), agent char(5))
    insert into #1 values ('DEHG','2','1901-01-01','AF','ST')
    insert into #1 values ('DEHC','3','1901-01-01','AG','ST')
    insert into #1 values ('DEHL','1','1901-01-01','AS','GT')
    create table #2 (code_p char(4), code_h char(2), code_date datetime)
    insert into #2 values ('DEHG','2','1901-01-01')
    insert into #2 values ('DEHG','2','2010-01-01')
    insert into #2 values ('DEHC','3','1901-01-01')
    insert into #2 values ('DEHC','3','2010-01-01')
    insert into #2 values ('DEHC','3','2011-01-01')
    insert into #2 values ('DEHL','1','1901-01-01')
    Select * from #1 a
    join #2 b on a.code_p = b.code_p and a.code_h = b.code_h and
    a.code_date = b.code_date
    --Expected output
    code_p code_h code_date Area agent
    DEHG 2  1901-01-01    AF ST
    DEHG 2  2010-01-01     AF ST
    DEHC 3  1901-01-01    AF ST
    DEHC 3  2010-01-01     AF ST
    DEHC 3  2011-01-01     AF ST
    DEHL 1  1901-01-01    AF ST 

    --Your Query 
    Select a.code_p, a.code_h, b.code_date, a.Area, a.agent from #1 a
    join #2 b on a.code_p = b.code_p and a.code_h = b.code_h and
    a.code_date = b.code_date
    --YOUR Query OUTPUT
    DEHG 2
    1901-01-01 00:00:00.000
    AF ST   
    DEHC 3
    1901-01-01 00:00:00.000
    AG ST   
    DEHL 1
    1901-01-01 00:00:00.000
    AS GT   
    Can you try this 
    select A.code_p,A.code_h,B.code_date,A.Area,A.agent from #1 A 
    join #2 B on A.code_h = b.code_h   
    where A.code_h in (3,2,1)
    DEHG 2
    1901-01-01 00:00:00.000
    AF ST   
    DEHG 2
    2010-01-01 00:00:00.000
    AF ST   
    DEHC 3
    1901-01-01 00:00:00.000
    AG ST   
    DEHC 3
    2010-01-01 00:00:00.000
    AG ST   
    DEHC 3
    2011-01-01 00:00:00.000
    AG ST   
    DEHL 1
    1901-01-01 00:00:00.000
    AS GT  
    Please Mark This As Answer if it helps to solve the issue

  • Join this two table HELP

    Hi to all.. i have this two table , the problem is i dunno how to join this two table into one, becoz in the forms i have two button "BUTTON A IS FOR TABLE A WHICH IS THE EMPLOYEE.. AND BUTTON B IS FOR ADMIN... WHAT I WANT IS TO MAKE THIS TWO BUTTON INTO ONE BUTTON.. i;m using form6i and run in client server..
    Guys any one here know plz help how to do this..
    TABLE 1
    BEGIN
    iF :username IS NULL THEN
    MESSAGE ('Please enter Username ');
    MESSAGE ('Please enter Username ');
    GO_ITEM('username');
    RAISE FORM_TRIGGER_FAILURE;
    end if;
    IF :PASSNAME1 IS NULL THEN
    MESSAGE('Please enter password.');
    MESSAGE('Please enter password.');
    GO_ITEM('PASSNAME1');
    RAISE FORM_TRIGGER_FAILURE;
    end if;
    select username,passname
    into :pass.username,:pass.passname1
    from Employees
    where :pass.username=username
    and :pass.passname1=passname;
    Message('Access Granted');
    Message('Access Granted');
    go_block('maina');
    hide_view('pass1');
    exception
    when no_Data_found then
    set_alert_Property('wrong',Alert_Message_text,'Invalid Password');
    if show_alert('wrong')=alert_button1 then
              go_item('PASSNAME1');
              :passname1:=null;
              end if;
    end;
    TABLE 2
    BEGIN
    iF :username IS NULL THEN
    MESSAGE ('Please enter Username ');
    MESSAGE ('Please enter Username ');
    GO_ITEM('username');
    RAISE FORM_TRIGGER_FAILURE;
    end if;
    IF :PASSNAME1 IS NULL THEN
    MESSAGE('Please enter password.');
    MESSAGE('Please enter password.');
    GO_ITEM('PASSNAME1');
    RAISE FORM_TRIGGER_FAILURE;
    end if;
    select username,passname
    into :pass.username,:pass.passname1
    from Admin1
    where :pass.username=username
    and :pass.passname1=passname;
    Message('Access Granted');
    Message('Access Granted');
    go_block('maina');
    hide_view('pass1');
    exception
    when no_Data_found then
    set_alert_Property('wrong',Alert_Message_text,'Invalid Password');
    if show_alert('wrong')=alert_button1 then
              go_item('PASSNAME1');
              :passname1:=null;
              end if;
    end;

    like i told you before in this posting How to join this two tables
    use a union
    select username,passname
    into :pass.username,:pass.passname1
    from Employees
    where :pass.username=username
    and :pass.passname1=passname
    union
    select username,passname
    into :pass.username,:pass.passname1
    from Admin1
    where :pass.username=username
    and :pass.passname1=passname;

  • Prcedure to joining of two tables and pls explore the primary & foriegn key

    Dear All,
    Am new to this forum looking forward to have some discussions and guidance reg ABAP.
    <b>My query is</b>
    what is the prcedure to joining of two tables and pls explore the primary & foriegn key relationships.
    Your kind help is required in this query .
    Thanks n regards
    Carol P

    Hi Carol,
    Joining two tables fetch data from tables that has relevent entry based on the keys. like LIKP and LIPS these are delivery tables. if the business requirement is fetching delivery data that are exists in both two tables. here LIKP will conatins a single entry against a condition and LIPS may contains several entry because one delivery may has multiple line items. so, fetch the data joining the two tables on vbeln that is delivrey number. this is used to increase the performance of the program except for all entries.
    Foreign key is poiting twowards primary key; it may be 1:1 , 1:many and many:many. if field1 of table a is pointing towards field1 of table b. then based on these condition at the time of entry SAP will check how many entry can be entred in field1 of table a.
    regards
    Krishnendu

  • Subject: How to do join between two tables using something like SE16

    SE16, SE11 provide form based interface query information from a single table. Is there a way to do join between two tables without creating an infoset erc? I am looking for something similar to sql join but in SAP BI 7.0
    Thanks.

    Hi
    Pls look into below links. Hope this helps you.
    1. http://help.sap.com/saphelp_46c/helpdata/EN/d2/cb45bf455611d189710000e8322d00/content.htm
    2. http://help.sap.com/saphelp_46c/helpdata/EN/d2/cb45a5455611d189710000e8322d00/content.htm
    Regards
    Sirigiri

  • Sql join on two tables where column like another column

    hi all,
    been trying to get the results from a join between two tables where one column is like another one.
    table1 (nameA) examples: black2, green1
    table2 (nameB) example: black2.location.uk.com, green1.location.uk.com
    so for example I want to match all those in table1 with black2 to the column in table2 that begins with black2 and so on if you see what im trying to get at!
    my sql so far:
    select * from  schema.table1 a
    join schema.table2 b
    on a.nameA like concat('%', b.nameB, '%'); but it errors:
    ORA-00909: invalid number of arguments
    00909. 00000 - "invalid number of arguments"
    Any help or advice would be greatly appreciated, thankyou!

    Either of these should do it...
    select * from  schema.table1 a
    join schema.table2 b on a.nameA like '%'||b.nameB||'%';or
    select * from  schema.table1 a
    join schema.table2 b on instr(b.nameB,a.nameA) > 0

  • Can we implement the custom sql query in CR for joining the two tables

    Hi All,
    Is there anyway to implement the custom sql query in CR for joining the two tables?
    My requirement here is I need to write sql logics for joining the two tables...
    Thanks,
    Gana

    In the Database Expert, expand the Create New Connection folder and browse the subfolders to locate your data source.
    Log on to your data source if necessary.
    Under your data source, double-click the Add Command node.
    In the Add Command to Report dialog box, enter an appropriate query/command for the data source you have opened.
    For example:
    SELECT
        Customer.`Customer ID`,
        Customer.`Customer Name`,
        Customer.`Last Year's Sales`,
        Customer.`Region`,
        Customer.`Country`,
        Orders.`Order Amount`,
        Orders.`Customer ID`,
        Orders.`Order Date`
    FROM
        Customer Customer INNER JOIN Orders Orders ON
            Customer.`Customer ID` = Orders.`Customer ID`
    WHERE
        (Customer.`Country` = 'USA' OR
        Customer.`Country` = 'Canada') AND
        Customer.`Last Year's Sales` < 10000.
    ORDER BY
        Customer.`Country` ASC,
        Customer.`Region` ASC
    Note: The use of double or single quotes (and other SQL syntax) is determined by the database driver used by your report. You must, however, manually add the quotes and other elements of the syntax as you create the command.
    Optionally, you can create a parameter for your command by clicking Create and entering information in the Command Parameter dialog box.
    For more information about creating parameters, see To create a parameter for a command object.
    Click OK.
    You are returned to the Report Designer. In the Field Explorer, under Database Fields, a Command table appears listing the database fields you specified.
    Note:
    To construct the virtual table from your Command, the command must be executed once. If the command has parameters, you will be prompted to enter values for each one.
    By default, your command is called Command. You can change its alias by selecting it and pressing F2.

  • Join on two tables using "LIKE"

    Hi all,
    I need make join on two tables (QMEL and STXL) using keys for connection:
    - field for join of first table is (QMEL-QMNUM - Notif. number - Char 12)
    - field for join of second table is  (STXL-TDNAME - Char 70)
    If it is connection based on EQ, I think, it's no problem but I need to connect it on base of 'LIKE'.
    Example:
    QMEL-QMNUM = '100100698075'
    I would like get all rows from STXL which contain or even better start with notif. number.
    Examples I would like connect with QMEL-QMNUM:
    STXL-TDNAME = '100100698075'
    STXL-TDNAME = '1001006980750001'
    STXL-TDNAME = '10010069807500010001'
    STXL-TDNAME = '10010069807500010002'
    etc..
    Am I able to manage that with select which join these two tables this way?
    Thanks for any solution
    Vaclav Hosek.

    Hi,
    Write 2 separate selects for this.
    select * from QMEL
    where......
    if sy-subrc = 0.
    loop at i_qmel into wa.
    r_tdname-option = 'CP'.
    r_tdname-sign = 'I'
    concatenate wa-qnum '*' into r_tdname-low.
    append r_tdname.
    endloop.
    select *
    from STL
    where tdname in r_tdname.
    endif.

  • Help view to get two table entries in search help

    Hi,
    I have created the help view for join two tables and that help view was  called in the selection method for creating search help of the custom table field. since it has show inthe selection screen , it is fetching only one table entries. and the shown entries are displaying but not passing.
    Please provide the steps to get the two table entries and the displayed entries should pass.
    Thanks in Advance,
    siva

    Hi sivanag,
    1. Try your view with SE16/SE16N to see if it really finds what you expect
    2. Use the same data elements in help view as on selection screen
    3. read documentation and F1 help on all fields you see in the search help definition - especially only field marked as EXPORT parameter will be put back on screen.
    But, sincerely, I don't know what your selection-screen looks like.
    Regards,
    Clemens

  • Regarding joining of two tables with same feilds

    hello gurus
    i am new to abap programming. i have to extract data from two tables ie coss and cosp tables . one table has 180 records and another table has 500 records i want to extract these two table records into internal table(680 records) and then i have to populate it to flatfile through gui download. can any one give me use code to work on it.these two tables has same set of feilds.

    Hi,
    First you should verify if you want to get all the data from both tables regardless if its the same record. If it is, then you can follow this logic:
    select * from coss into table it_coss_cosp. "step 1
    select * from cosp appending table it_coss_cosp. "step 2
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
      BIN_FILESIZE                  =
        filename                      = data.txt
      FILETYPE                      = 'ASC'
      APPEND                        = ' '
      WRITE_FIELD_SEPARATOR         = ' '
      HEADER                        = '00'
      TRUNC_TRAILING_BLANKS         = ' '
      WRITE_LF                      = 'X'
      COL_SELECT                    = ' '
      COL_SELECT_MASK               = ' '
      DAT_MODE                      = ' '
    IMPORTING
      FILELENGTH                    =
      tables
        data_tab                      = it_coss_cosp
    EXCEPTIONS
      FILE_WRITE_ERROR              = 1
      NO_BATCH                      = 2
      GUI_REFUSE_FILETRANSFER       = 3
      INVALID_TYPE                  = 4
      NO_AUTHORITY                  = 5
      UNKNOWN_ERROR                 = 6
      HEADER_NOT_ALLOWED            = 7
      SEPARATOR_NOT_ALLOWED         = 8
      FILESIZE_NOT_ALLOWED          = 9
      HEADER_TOO_LONG               = 10
      DP_ERROR_CREATE               = 11
      DP_ERROR_SEND                 = 12
      DP_ERROR_WRITE                = 13
      UNKNOWN_DP_ERROR              = 14
      ACCESS_DENIED                 = 15
      DP_OUT_OF_MEMORY              = 16
      DISK_FULL                     = 17
      DP_TIMEOUT                    = 18
      FILE_NOT_FOUND                = 19
      DATAPROVIDER_EXCEPTION        = 20
      CONTROL_FLUSH_ERROR           = 21
      OTHERS                        = 22
    IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    But if you plan to get those record which exists in both tables then use a join condition for your select statement.
    Hope this helps...
    P.S. Please award points for useful answers.

  • Join between two tables in SAP ( actual sales vs budget sales)

    I have one table with  actual data sales in SAP .This table has a name   CE11000
    This table contain amounts about : customers ,Products, Quantity of Sales ,amount of sales ,month witch did the sales and the year.
    The other table from SAP has the budget sales . This table is CE21000
    This table contain the same fields : customers ,Products, Quantity of Sales ,amount of sales ,month witch did the sales and the year but all this for budget .
    In the budget table ( CE21000)there are no records for a particular make if it was not budgeted to make a sale.
    Sometimes i have the same problem with the actual data table (CE11000) ...I have not records witch can use to join the results for a report witch take the comparison between actual sales and budget sales.
    I would like to remind you that the report should have four "dimensions"
    a) year
    b) month
    c) customer
    d) type of product
    I try to use left outer join with a customer fields in two tables but i have  simultaneously the field with type of product .
    I want to see in one year (for example february of 2011)  for one customer the type of products (quantity and anmounts) witch buy (ACTUAL)  and that to  compare with the corresponding data if available
    Any help would appreciate
    thanks

    Haggar
    Your first query matches on two conditions BOTH being true. If there is a D and no matching E, you'll see the columns from D but none from E.
    Your second query takes all Emplyees, and lines up 2 (possibly different) rows from D against each
    E columns | D columns (match e.deptid=d.dept_id) | D columns (match ep_id=de_id)
    So you may have any mix of:
    E       | D1      | D2
    E       | nothing | D2
    nothing | D1      | D2
    E       | D1      | nothing
    E       | nothing | nothing
    nothing | D1      | nothing
    nothing | nothing | D2(NB I'm assuming FULL OUTER JOIN is commutative - ie the order is unimportant - but I may be wrong in which case the last row won't occur)
    You will get a different number of rows (as well as different columns) in each case.
    Try it!
    Regards Nigel

  • Doubt on creating join between  two tables

    Hi friends,
    I have two tables , ekes and eine
    my requirement is  if Ekes-ebtyp = 'X1'.
    then Ekes-ebtyp = EKES-EINDT + Eine-j_3alitra.
    the doubt is there is no primary key common between this table . actually in my whole program for purchase order i used EBELN for extracting details when two tables are involved . in this two tables no two primary keys are common to extract data ...is there any ways please suggest..
    the code i wrote is
    loop at li_ekes assigning <fl_ekes>.
    read table li_eine assigning <fl_eine> with table key (here i need to give some unique fields to join these two fields please guide me which field i can use.).
    <fl_ekes>-ebtyp = <fl_ekes>-EINDT + <fl_eine>-j_3alitra.

    Hi,
    Both tables have field EBELN in common.
    You can use it to extract data from both.
    Hope this helps.
    reward if helpful.
    Regards,
    Sipra

  • Regarding logical joining of two table  by using ODI interface

    I have a two table in database.i have to logically add them, with the help of ODI interface.which should reflect in target.
    there is a smple cinario regarding ,convertion of relational table to flat file.
    but how to reflect mutiple table through joining in target from source.

    Hi
    Please correct my understan on your query
    You want to say that, you have two tables at source side and want to store data to target table, later from target to flat file
    or
    You have two target table want to store data into flat file
    Thanks
    Phani

  • Multiple foreign key joins between two tables

    Hi,
    I have a question about building a repository.
    I have a date dimension and one fact. The fact table has about 10 foreign key columns that link to the date Dimension.
    In this case should I create 10 aliases to create joins in the Physical and BMM layer or is there any other way to handle this situation.
    I am asking this question because 10 aliases can get very confusing for me at the later point of time while creating reports.
    Using OBIEE 10.1.3

    Hi
    I have a follow up question on this.
    I am okay with not seeing the different date tables under the Subject area. Even if it just shows a it as a Simple DATE Dimension I am good with it.
    In this case which is the efficient way, creating 10 aliases or creating 10 joins in the physical layer. I just figured out that we can create multiple joins between the same set of two tables but do not know how will that effect the way BI server works.
    Please help me in understanding this concept.
    thanks
    This request id for OBIEE 10.1.3

  • SQL Join between two tables two columns, but the data in the join condition could be null.

    hello all,
    can anyone help me write an outer join b/n the two tables below. The joining condition has if's and or's.
    table 1 has 2 million rows, table 2 is very small
    TABLE1
    CUSTOMER_ID
    CITY
    STATE
    1
    SKOKIE
    IL
    2
    CHICAGO
    IL
    3
    CARY
    NC
    ERIE
    PA
    PHILLY
    PA
    CHARLOTE
    NC
    2 MILLION
    CITYXY
    STATEX
    TABLE2
    CITY
    STATE
    CONTACT
    IL
    OJO
    ERIE
    BRITT
    PA
    MIKE
    PITTSBURG
    PA
    HILTON
    N043
    TAT
    affi
    B
    affi
    R
    b0b
    Q
    b0b
    CHARLOTE
    NC
    b0b
    problem :: for all the data in table1, I need to find out the CONTACT from table 2 And the join condition would be as below
    1. either both TABLE1.CITY=TABLE2.CITY AND TABLE1.STATE=TABLE2.STATE and get CONTACT
    OR
    2. TABLE1.CITY=TABLE2.CITY AND TABLE2.STATE IS NULL  and get the value of CONTACT
    OR
    3. TABLE1.STATE=TABLE2.STATE AND TABLE2.CITY is null and get the value of CONTACT
    I need a query like this
    SELECT A.CUSTOMER_ID, A.CITY, A.STATE, B.CONTACT
    FROM TABLE1 A, TABLE2 B
    WHERE (join condition fitting in the 3 condition mentioned above)

    Dear OP,
    Do you want something like this?
    > with t1 as
    -- Start of SAMPLE DATA
    (select 1 CUSTOMER_ID, 'SKOKIE' CITY, 'IL' STATE from dual union
    select 2, 'CHICAGO', 'IL'  from dual union
    select 3, 'CARY', 'NC' from dual union
    select 4, 'ERIE', 'PA'  from dual union
    select 5, 'PHILLY', 'PA'  from dual union
    select 6, 'CHARLOTE', 'NC' from dual)
    t2 as
    (select null CITY, 'IL' STATE, 'OJO' CONTACT from dual union
    select  'ERIE', null, 'BRITT'  from dual union
    select null, 'PA', 'MIKE'  from dual union
    select 'PITTSBURG', 'PA', 'HILTON'  from dual union
    select 'N043', 'TAT', 'affi'  from dual union
    select null,'B', 'affi'  from dual union
    select null,'R', 'b0b'  from dual union
    select null,'Q', 'b0b'  from dual union
    select 'CHARLOTE', 'NC', 'b0b'  from dual
    --- END IF SAMPLE Data
    select * from t1 full outer join t2
    on ( nvl(t1.city,t2.city) = nvl(t2.city,t1.city)
    and nvl(t1.state,t2.state) = nvl(t2.state,t1.state) )
    order by 1,2,3,4
    CUSTOMER_ID CITY     STATE CITY      STATE CONTACT
              1 SKOKIE   IL              IL    OJO    
              2 CHICAGO  IL              IL    OJO    
              3 CARY     NC                           
              4 ERIE     PA    ERIE            BRITT  
              4 ERIE     PA              PA    MIKE   
              5 PHILLY   PA              PA    MIKE   
              6 CHARLOTE NC    CHARLOTE  NC    b0b    
                               N043      TAT   affi   
                               PITTSBURG PA    HILTON 
                                         B     affi   
                                         Q     b0b    
                                         R     b0b    
    12 rows selected
    Elapsed: 00:00:00.112
    Hope this is helpful. If not please let us know what is you desired result (sample) if your data was like above?
    vr,
    Sudhakar

Maybe you are looking for