Using a view to join two tables

Thank you in advance for any advice you can lend.
I am using this code in my MySQL db to create a view.
select
    job.id as job_id,
    umr_cost_calculation.plant_name,
    max(umr_cost_calculation.id) as max_id
from
    job,
    umr_cost_calculation
where
    job.id = umr_cost_calculation.job_id
group by job.id , umr_cost_calculation.plant_name
I did this so I can join two tables and pull in the most current cost data for a specific plant. The report will, at times, show the wrong (older) data. I can re-run the report, filter to just the one job and see again the wrong data. When I add the max_id to the report, it display the id and updates the report with the correct data. It appears that the view was stale and by adding the ID to the report this fixed the issue.
1) Is this the best way to make this join? I don't see how Crystal supports a subquery to make a join (this is why I used the view).
2) If I leave the max_id on the report, will this force the view to always update?

Try:
Select
D1.EmpLoginID,
Count(D1.ID),
Count(D1.AlarmCode),
D1.EmpName,
D1.EmpAddress,
D2.Db2Count
FROM DB1.Data D1
LEFT JOIN (SELECT
empLoginID, Count(*) as Db2Count
FROM DB2.ALL_Database
WHERE site = 'Atlanta'
GROUP BY empLoginID
) D2
ON D1.EmpLoginID = D2.EmpLoginID
GROUP BY D1.empLoginID, D1.EmpName, D2.EmpAddress, D2.Db2Count
Order BY D1.empLoginID ASC
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles

Similar Messages

  • 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/

  • 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

  • Need help to join two tables using three joins, one of which is a (between) date range.

    I am trying to develop a query in MS Access 2010 to join two tables using three joins, one of which is a (between) date range. The tables are contained in Access. The reason
    the tables are contained in access because they are imported from different ODBC warehouses and the data is formatted for uniformity. I believe this cannot be developed using MS Visual Query Designer. I think writing a query in SQL would be suiting this project.
    ABCPART links to XYZPART. ABCSERIAL links to XYZSERIAL. ABCDATE links to (between) XYZDATE1 and ZYZDATE2.
    [ABCTABLE]
    ABCORDER
    ABCPART
    ABCSERIAL
    ABCDATE
    [ZYXTABLE]
    XYZORDER
    XYZPART
    XYZSERIAL
    XYZDATE1
    XYZDATE2

    Thank you for the looking at the post. The actual table names are rather ambiguous. I renamed them so it would make more sense. I will explain more and give the actual names. What I do not have is the actual data in the table. That is something I don't have
    on this computer. There are no "Null" fields in either of the tables. 
    This table has many orders (MSORDER) that need to match one order (GLORDER) in GLORDR. This is based on MSPART joined to GLPART, MSSERIAL joined to GLSERIAL, and MSOPNDATE joined if it falls between GLSTARTDATE and GLENDDATE.
    [MSORDR]
    MSORDER
    MSPART
    MSSERIAL
    MSOPNDATE
    11111111
    4444444
    55555
    2/4/2015
    22222222
    6666666
    11111
    1/6/2015
    33333333
    6666666
    11111
    3/5/2015
    This table has one order for every part number and every serial number.
    [GLORDR]
    GLORDER
    GLPART
    GLSERIAL
    GLSTARTDATE
    GLENDDATE
    ABC11111
    444444
    55555
    1/2/2015
    4/4/2015
    ABC22222
    666666
    11111
    1/5/2015
    4/10/2015
    AAA11111
    555555
    22222
    3/2/2015
    4/10/2015
    Post Query table
    GLORDER
    MSORDER
    GLSTARTDATE
    GLENDDATE
    MSOPNDATE
    ABC11111
    11111111
    1/2/2015
    4/4/2015
    2/4/2015
    ABC22222
    22222222
    1/5/2015
    4/10/2015
    1/6/2015
    ABC22222
    33333333
    1/5/2015
    4/10/2015
    3/5/2015
    This is the SQL minus the between date join.
    SELECT GLORDR.GLORDER, MSORDR.MSORDER, GLORDR.GLSTARTDATE, GLORDR.GLENDDATE, MSORDR.MSOPNDATE
    FROM GLORDR INNER JOIN MSORDR ON (GLORDR.GLSERIAL = MSORDR.MSSERIAL) AND (GLORDR.GLPART = MSORDR.MSPART);

  • How do you join two tables from different Oracle schemas using a subquery

    I am trying to join two tables from different Oracle schemas using a subquery. I can extract data from each of the tables without a problem. However, when I combine the select statements using a subquery I get the Oracle error *'ORA-00936: missing expression'*. Since each SELECT statement executes on its own without error I don't understand what is missing. The result set I am trying to get is to match up the LINE_ID from PDTABLE_12_1 in schema DD_12809 with the MAT_DESCRIPTION from table PDTABLE_201 in schema RA_12809.
    The query is as follows:
    sql = "SELECT [DD_12809].[PDTABLE_12_1].LINE_ID FROM [DD_12809].[PDTABLE_12_1] JOIN " _
    + "(SELECT [RA_12809].[PDTABLE_201].MAT_DESCRIPTION " _
    + "FROM [RA_12809].[PDTABLE_201]) AS FAB " _
    + "ON [DD_12809].[PDTABLE_12_1].PIPING_MATER_CLASS = FAB.PIPING_MATER_CLASS"
    The format of the query is copied from a SQL programming manual.
    I also tried executing the query using a straight JOIN on the two tables but got the same results. Any insight would be helpful. Thanks!
    Edited by: user11338343 on Oct 19, 2009 6:55 AM

    I believe you are receiving the error because you are trying to JOIN on a column that doesn't exist. For example you are trying to join on FAB.PIPING_MATER_CLASS but that column does not exist in the subquery.
    If you want to do a straight join without a subquery you could do the following
    SELECT  DD_12809.PDTABLE_12_1.LINE_ID
    ,       FAB.MAT_DESCRIPTION
    FROM    DD_12809.PDTABLE_12_1
    JOIN    RA_12809.PDTABLE_201    AS FAB ON DD_12809.PDTABLE_12_1.PIPING_MATER_CLASS = FAB.PIPING_MATER_CLASS  HTH!

  • Join two table using two link field

    hi all
    i need to left join two table, and to link two table need to match two field
    ex: the link field is store_name and id
    i use below sql statement
    SELECT A1.store_name, A1.id, A1.card, A2.sale, A2.history
    FROM Georgraphy A1, Store_Information A2
    WHERE A1.store_name = A2.store_name(+)
    AND A1.id = A2.id(+)
    but it's wrong
    please tell me how to left join A1 and A2, thx!

    Whats your aim?
    Any error message?
    SQL> select * from geography;
            ID STORE_NAME
             1 a
             1 b
             2 a
             2 b
    SQL> select * from store_information;
            ID STORE_NAME
             1 a
             3 d
    SQL> SELECT *
      2  FROM Geography A1, Store_Information A2
      3  WHERE A1.store_name = A2.store_name(+)
      4  AND A1.id = A2.id(+);
            ID STORE_NAME         ID STORE_NAME
             1 a                   1 a
             2 a
             2 b
             1 b                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Joining two tables using PL/SQL

    here i am trying to join two tables can any one tell me what is wrong with this syntex
    CREATE OR REPLACE PROCEDURE test IS
    CURSOR c1 IS SELECT seq,fname,lname from t1;
    CURSOR c2 IS SELECT seq1,q,a from t2;
    userjob number;
    BEGIN
         OPEN c1;
    insert into t3 values(c1.seq,c1.fname,c1.lname);
         FETCH c1.seq INTO userjob;
    FOR c1rec IN c2 LOOP
    IF (c1rec.seq=c1.seq and c1rec.q1='why') THEN
    insert into t3 values(c1rec.q1,c1rec.a1);
    elsif (c1rec.seq=c1.seq and c1rec.q1='what') then
    insert into t3 values(c1rec.q2,c1rec.a2);
    elsif (c1rec.seq=c1.seq and c1rec.q1='when') then
    insert into t3 values(c1rec.q3,c1rec.a3);
    elsif (c1rec.seq=c1.seq and c1rec.q1='where') then
    insert into t3 values(c1rec.q4,c1rec.a4);
         END IF;
         END LOOP;
         END;
    /

    You should always fetch a cursor before using it's values. All columns in the select should be fetched into variables or a record-variable. You can't refer to the cursor-columns values with c1.seq etc.
    r1 c1%rowtype;
    l_found boolean;
    BEGIN
    OPEN c1;
    FETCH c1 INTO r1;
    insert into t3 values(r1.seq,r1.fname,r1.lname);
    l_found := c1%found;
    close c1;
    if l_found
    then
    It is also better to close the cursor and check if the select resulted in a row. With this code you will only retrieve one row even if the select will result in multiple rows.
    But I agree with all the others that this can probably be done more efficiently with one SQL statement.

  • Joining two tables having no common fields using one select query

    Hi Experts,
    How to join two tables which are NOT having any field in common using only one select query?
    Your help will be appreciated.
    Thank you.

    Identify a third table (or more tables) with common fields with your two tables, or change your question either removing JOIN or removing NO COMMON FIELDS, else you wont get many responses and will be left alone in outer space, as suggested by Thomas.
    If you acturally require what you written, better execute two select and merge the two internal tables merging every record from first table with every record of second table, til no more memory is available.
    Regards,
    Raymond

  • Joining Two Tables by Database View

    HI There,
    I'm trying to join two tables BNKA and LFBK to get the Bank details according to the requirement.
    Now, I wanted to go head and create a Generic datasource and bring the fields from these two tables by Vew.
    Can someone please explain me step by step to create the database view?
    One more thing, do we need to have anything in common between two tables BNKA and LFBK ?
    I really appreciate if someone can guide me through step by step method to create Database Table view?
    Thanks
    Madhuri

    Hi Madhuri,
    With out having common fields, we can not create data base view based on two tables.
    1) goto SE11
    2) select data base view and give the name and create.
    3) in left side give the tables names
    4)  In left side define the relation
    check the below article
    http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/10a89c00-7dd7-2d10-6f83-cd24ee6d517c?QuickLink=index&overridelayout=true
    Regards,
    Venkatesh

  • Join two table (Inner Join)

    how to join two tables using inner join.

    Tariq,
    Pretty vague question.  You can create joins in an ABAP program, or while creating a view, or when creating a SAP query of one type or another.  Some people download tables and then join them using desktop software.  If you can elaborate your question I may be able to give you a better answer.
    I recently joined two wooden tables at a picnic.  I used 24 gauge galvanized wire combined with duct tape, so I guess you couldn't really call that an 'inner join'.
    Best Regards,
    DB49

  • How to join two tables

    hi
    how to join two tables using inner join  if the first table has two primary keys and second table has 3 primary keys

    Would describe type of joins in ABAP, which might differ with other joins.
    The join syntax represents a recursively nestable join expression. A join expression consists of a left-hand and a right- hand side, which are joined either by means of INNER JOIN or LEFT OUTER JOIN. Depending on the type of join, a join expression can be either an inner (INNER) or an outer (LEFT OUTER) join. Every join expression can be enclosed in round brackets. If a join expression is used, the SELECT command circumvents SAP buffering.
    On the left-hand side, either a single database table, a view dbtab_left, or a join expression join can be specified. On the right-hand side, a single database table or a view dbtab_right as well as join conditions join_cond can be specified after ON. In this way, a maximum of 24 join expressions that join 25 database tables or views with each other can be specified after FROM.
    AS can be used to specify an alternative table name tabalias for each of the specified database table names or for every view. A database table or a view can occur multiple times within a join expression and, in this case, have various alternative names.
    The syntax of the join conditions join_cond is the same as that of the sql_cond conditions after the addition WHERE, with the following differences:
    At least one comparison must be specified after ON.
    Individual comparisons may be joined using AND only.
    All comparisons must contain a column in the database table or the view dbtab_right on the right-hand side as an operand.
    The following additions not be used: NOT, LIKE, IN.
    No sub-queries may be used.
    For outer joins, only equality comparisons (=, EQ) are possible.
    If an outer join occurs after FROM, the join condition of every join expression must contain at least one comparison between columns on the left-hand and the right-hand side.
    In outer joins, all comparisons that contain columns as operands in the database table or the view dbtab_right on the right-hand side must be specified in the corresponding join condition. In the WHERE condition of the same SELECT command, these columns are not allowed as operands.
    Resulting set for inner join
    The inner join joins the columns of every selected line on the left- hand side with the columns of all lines on the right-hand side that jointly fulfil the join_cond condition. A line in the resulting set is created for every such line on the right-hand side. The content of the column on the left-hand side may be duplicated in this case. If none of the lines on the right-hand side fulfils the join_cond condition, no line is created in the resulting set.
    Resulting set for outer join
    The outer join basically creates the same resulting set as the inner join, with the difference that at least one line is created in the resulting set for every selected line on the left-hand side, even if no line on the right-hand side fulfils the join_cond condition. The columns on the right-hand side that do not fulfil the join_cond condition are filled with null values.
    Note
    If the same column name occurs in several database tables in a join expression, they have to be identified in all remaining additions of the SELECT statement by using the column selector ~.
    Example
    Join the columns carrname, connid, fldate of the database tables scarr, spfli and sflight by means of two inner joins. A list is created of the flights from p_cityfr to p_cityto. Alternative names are used for every table.
    PARAMETERS: p_cityfr TYPE spfli-cityfrom,
    p_cityto TYPE spfli-cityto.
    DATA: BEGIN OF wa,
    fldate TYPE sflight-fldate,
    carrname TYPE scarr-carrname,
    connid TYPE spfli-connid,
    END OF wa.
    DATA itab LIKE SORTED TABLE OF wa
    WITH UNIQUE KEY fldate carrname connid.
    SELECT ccarrname pconnid f~fldate
    INTO CORRESPONDING FIELDS OF TABLE itab
    FROM ( ( scarr AS c
    INNER JOIN spfli AS p ON pcarrid = ccarrid
    AND p~cityfrom = p_cityfr
    AND p~cityto = p_cityto )
    INNER JOIN sflight AS f ON fcarrid = pcarrid
    AND fconnid = pconnid ).
    LOOP AT itab INTO wa.
    WRITE: / wa-fldate, wa-carrname, wa-connid.
    ENDLOOP.
    Example
    Join the columns carrid, carrname and connid of the database tables scarr and spfli using an outer join. The column connid is set to the null value for all flights that do not fly from p_cityfr. This null value is then converted to the appropriate initial value when it is transferred to the assigned data object. The LOOP returns all airlines that do not fly from p_cityfr.
    PARAMETERS p_cityfr TYPE spfli-cityfrom.
    DATA: BEGIN OF wa,
    carrid TYPE scarr-carrid,
    carrname TYPE scarr-carrname,
    connid TYPE spfli-connid,
    END OF wa,
    itab LIKE SORTED TABLE OF wa
    WITH NON-UNIQUE KEY carrid.
    SELECT scarrid scarrname p~connid
    INTO CORRESPONDING FIELDS OF TABLE itab
    FROM scarr AS s
    LEFT OUTER JOIN spfli AS p ON scarrid = pcarrid
    AND p~cityfrom = p_cityfr.
    LOOP AT itab INTO wa.
    IF wa-connid = '0000'.
    WRITE: / wa-carrid, wa-carrname.
    ENDIF.
    ENDLOOP.

  • Join Two tables in UI So that it look like one table.

    Can anyone provide me how to join table in viewing..
    Rite now i am reteiving fields in one table and other field in another table. But while viewing i want to join on UI.
    I am getting gap between these 2 tables.
    How to do that?
    Thanks

    Hi Nitin,
    why you want to join two tables.? if you want to display the data from more then one data source (Node ) then you can use table variant in that case. See the following Links for reference hope it will help.
    [Using Table Cell Variants in NW 2004s|http://wiki.sdn.sap.com/wiki/display/WDJava/UsingTableCellVariantsinNW2004s]
    [Play Sudoku: Using Table Cell Variants in Web Dynpro Java|http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/c0310fd2-f40d-2a10-b288-bcbe0810a961]
    Regards
    Jeetendra

  • ORA-01776: I can't insert on a view comprised by two tables

    Good morning:
    I'm trying to do an insertion on a view comprised by the join of two tables (departments and employees) and it failed.
    The view is this one:
    CREATE OR REPLACE FORCE VIEW "EMPDPTO" ("EMPLOYEE_ID", "LAST_NAME", "EMAIL", "HIRE_DATE", "JOB_ID", "DEPARTMENT_ID", "DEPARTMENT_NAME") AS
    SELECT employee_id, last_name, email, hire_date, job_id, d.department_id, department_name
    FROM employees e, departments d
    WHERE e.department_id = d.department_id
    My insertion command
    INSERT INTO empdpto
    VALUES(743, 'Bermejo', '[email protected]', '12/12/09', 'IT_PROG', 10, 'Administration');
    throws a ORA-01776: it's not possible to modify more than one base table through a join union.
    On the other side I've seen in documentation that in certain circunstances is possible to do an insertion on a complex view.
    Is it possible to do an insertion on a view comprised by two tables?. Under what circunstances?.
    Thanks

    From SQL Reference CREATE VIEW section Notes on Updatable Views http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_8004.htm#i2065510
    >
    If you want a join view to be updatable, then all of the following conditions must be true:
    The DML statement must affect only one table underlying the join.
    For an INSERT statement, the view must not be created WITH CHECK OPTION, and all columns into which values are inserted must come from a key-preserved table. A key-preserved table is one for which every primary key or unique key value in the base table is also unique in the join view.
    For an UPDATE statement, the view must not be created WITH CHECK OPTION, and all columns updated must be extracted from a key-preserved table.
    >
    The first condition is not true for the view you have created.
    Edited by: P. Forstmann on 26 janv. 2010 20:17

  • Join two tables TPAR, TVPG

    Dear Gurus,
    I am a functional Consultant and do not know the ABAP.
    I want to know that is there any way to join two tables (TPAR, TVPG) and extract the filed PARGR from table "TVPG" and field ERNAM from Table "TPAR" into a third table.
    Wishes
    Abhishek

    Hi,
    it´s complicated, because you must identify what you whant to relacionated....
    view... V_TPAR  you can see up level partner function...
    is the best i can do.sorry.
    but  if you identify the element between the two tables, you can do a  Z* view.
    reguards,
    Miguel
    If this message help you , dont forget to give point´s. thanks

  • Join two tables which stored in difference database

    Hi, i have a problem how to join two tables which are stored in difference databases. I'm using MS access and need to use Jsp to solve it. is anyone have idea to solve it. thxx

    Hi,
    I think you cannot join the tables which are in two diff databases. You have to connect to each database ,get the result and manipulate in your program. Use a bean.
    cheers,
    Narayana

Maybe you are looking for