Joins help

help me on joins ,
1). What is joins?
2). Where it is used?
3). How it is used?
4). difference in them?
please help me with example points will be awarded,
thank you,
Regards,
Jagrut BharatKumar Shukla

Hi,
Really there are two types, inner join and left outter join. Inner join is when you know that there is a 1.1 or 1.n link between the tables. Here you will only get a hit if there is a link in the two tables. When it is not known whether there is a match in the second table, then here is where the left outer join comes in. It will still give you the values from the first table, even though it might not find a match in the second table, the fields from the second table will simply be blank.
Please more information, please check this links.
http://help.sap.com/saphelp_nw2004s/helpdata/en/cf/21ec77446011d189700000e8322d00/frameset.htm
http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb39c4358411d1829f0000e829fbfe/frameset.htm
Try to go thro info. Inner join and left outer join is possible in sap abap.
... FROM tabref1 [INNER] JOIN tabref2 ON cond
Effect
The data is to be selected from transparent database tables and/or views determined by tabref1 and tabref2. tabref1 and tabref2 each have the same form as in variant 1 or are themselves Join expressions. The keyword INNER does not have to be specified. The database tables or views determined by tabref1 and tabref2 must be recognized by the ABAP Dictionary.
In a relational data structure, it is quite normal for data that belongs together to be split up across several tables to help the process of standardization (see relational databases). To regroup this information into a database query, you can link tables using the join command. This formulates conditions for the columns in the tables involved. The inner join contains all combinations of lines from the database table determined by tabref1 with lines from the table determined by tabref2, whose values together meet the logical condition (join condition) specified using ON>cond.
Inner join between table 1 and table 2, where column D in both tables in the join condition is set the same:
Table 1 Table 2
A
B
C
D
D
E
F
G
H
a1
b1
c1
1
1
e1
f1
g1
h1
a2
b2
c2
1
3
e2
f2
g2
h2
a3
b3
c3
2
4
e3
f3
g3
h3
a4
b4
c4
3
Inner Join
A
B
C
D
D
E
F
G
H
a1
b1
c1
1
1
e1
f1
g1
h1
a2
b2
c2
1
1
e1
f1
g1
h1
a4
b4
c4
3
3
e2
f2
g2
h2
Example
Output a list of all flights from Frankfurt to New York between September 10th and 20th, 2001 that are not sold out:
DATA: DATE LIKE SFLIGHT-FLDATE,
CARRID LIKE SFLIGHT-CARRID,
CONNID LIKE SFLIGHT-CONNID.
SELECT FCARRID FCONNID F~FLDATE
INTO (CARRID, CONNID, DATE)
FROM SFLIGHT AS F INNER JOIN SPFLI AS P
ON FCARRID = PCARRID AND
FCONNID = PCONNID
WHERE P~CITYFROM = 'FRANKFURT'
AND P~CITYTO = 'NEW YORK'
AND F~FLDATE BETWEEN '20010910' AND '20010920'
AND FSEATSOCC < FSEATSMAX.
WRITE: / DATE, CARRID, CONNID.
ENDSELECT.
If there are columns with the same name in both tables, you must distinguish between them by prefixing the field descriptor with the table name or a table alias.
Note
In order to determine the result of a SELECT command where the FROM clause contains a join, the database system first creates a temporary table containing the lines that meet the ON condition. The WHERE condition is then applied to the temporary table. It does not matter in an inner join whether the condition is in the ON or WHEREclause. The following example returns the same solution as the previous one.
Example
Output of a list of all flights from Frankfurt to New York between September 10th and 20th, 2001 that are not sold out:
DATA: DATE LIKE SFLIGHT-FLDATE,
CARRID LIKE SFLIGHT-CARRID,
CONNID LIKE SFLIGHT-CONNID.
SELECT FCARRID FCONNID F~FLDATE
INTO (CARRID, CONNID, DATE)
FROM SFLIGHT AS F INNER JOIN SPFLI AS P
ON FCARRID = PCARRID
WHERE FCONNID = PCONNID
AND P~CITYFROM = 'FRANKFURT'
AND P~CITYTO = 'NEW YORK'
AND F~FLDATE BETWEEN '20010910' AND '20010920'
AND FSEATSOCC < FSEATSMAX.
WRITE: / DATE, CARRID, CONNID.
ENDSELECT.
Note
Since not all of the database systems supported by SAP use the standard syntax for ON conditions, the syntax has been restricted. It only allows those joins that produce the same results on all of the supported database systems:
Only a table or view may appear to the right of the JOIN operator, not another join expression.
Only AND is possible in the ON condition as a logical operator.
Each comparison in the ON condition must contain a field from the right-hand table.
If an outer join occurs in the FROM clause, all the ON conditions must contain at least one "real" JOIN condition (a condition that contains a field from tabref1 amd a field from tabref2.
Note
In some cases, '*' may be specified in the SELECT clause, and an internal table or work area is entered into the INTO clause (instead of a list of fields). If so, the fields are written to the target area from left to right in the order in which the tables appear in the FROM clause, according to the structure of each table work area. There can then be gaps between table work areas if you use an Alignment Request. For this reason, you should define the target work area with reference to the types of the database tables, not simply by counting the total number of fields. For an example, see below:
Variant 3
... FROM tabref1 LEFT [OUTER] JOIN tabref2 ON cond
Effect
Selects the data from the transparent database tables and/or views specified in tabref1 and tabref2. tabref1 und tabref2 both have either the same form as in variant 1 or are themselves join expressions. The keyword OUTER can be omitted. The database tables or views specified in tabref1 and tabref2 must be recognized by the ABAP-Dictionary.
In order to determine the result of a SELECT command where the FROM clause contains a left outer join, the database system creates a temporary table containing the lines that meet the ON condition. The remaining fields from the left-hand table (tabref1) are then added to this table, and their corresponding fields from the right-hand table are filled with ZERO values. The system then applies the WHERE condition to the table.
Left outer join between table 1 and table 2 where column D in both tables set the join condition:
Table 1 Table 2
A
B
C
D
D
E
F
G
H
a1
b1
c1
1
1
e1
f1
g1
h1
a2
b2
c2
1
3
e2
f2
g2
h2
a3
b3
c3
2
4
e3
f3
g3
h3
a4
b4
c4
3
Left Outer Join
A
B
C
D
D
E
F
G
H
a1
b1
c1
1
1
e1
f1
g1
h1
a2
b2
c2
1
1
e1
f1
g1
h1
a3
b3
c3
2
NULL
NULL
NULL
NULL
NULL
a4
b4
c4
3
3
e2
f2
g2
h2
Example
Output a list of all custimers with their bookings for October 15th, 2001:
DATA: CUSTOMER TYPE SCUSTOM,
BOOKING TYPE SBOOK.
SELECT SCUSTOMNAME SCUSTOMPOSTCODE SCUSTOM~CITY
SBOOKFLDATE SBOOKCARRID SBOOKCONNID SBOOKBOOKID
INTO (CUSTOMER-NAME, CUSTOMER-POSTCODE, CUSTOMER-CITY,
BOOKING-FLDATE, BOOKING-CARRID, BOOKING-CONNID,
BOOKING-BOOKID)
FROM SCUSTOM LEFT OUTER JOIN SBOOK
ON SCUSTOMID = SBOOKCUSTOMID AND
SBOOK~FLDATE = '20011015'
ORDER BY SCUSTOMNAME SBOOKFLDATE.
WRITE: / CUSTOMER-NAME, CUSTOMER-POSTCODE, CUSTOMER-CITY,
BOOKING-FLDATE, BOOKING-CARRID, BOOKING-CONNID,
BOOKING-BOOKID.
ENDSELECT.
If there are columns with the same name in both tables, you must distinguish between them by prefixing the field descriptor with the table name or using an alias.
Note
For the resulting set of a SELECT command with a left outer join in the FROM clause, it is generally of crucial importance whether a logical condition is in the ON or WHERE condition. Since not all of the database systems supported by SAP themselves support the standard syntax and semantics of the left outer join, the syntax has been restricted to those cases that return the same solution in all database systems:
Only a table or view may come after the JOIN operator, not another join statement.
The only logical operator allowed in the ON condition is AND.
Each comparison in the ON condition must contain a field from the right-hand table.
Comparisons in the WHERE condition must not contain a field from the right-hand table.
The ON condition must contain at least one "real" JOIN condition (a condition in which a field from tabref1 as well as from tabref2 occurs).
Note
In some cases, '*' may be specivied as the field list in the SELECT clause, and an internal table or work area is entered in the INTO clause (instead of a list of fields). If so, the fields are written to the target area from left to right in the order in which the tables appear in the llen in der FROM clause, according to the structure of each table work area. There can be gaps between the table work areas if you use an Alignment Request. For this reason, you should define the target work area with reference to the types of the database tables, as in the following example (not simply by counting the total number of fields).
Example
Example of a JOIN with more than two tables: Select all flights from Frankfurt to New York between September 10th and 20th, 2001 where there are available places, and display the name of the airline.
DATA: BEGIN OF WA,
FLIGHT TYPE SFLIGHT,
PFLI TYPE SPFLI,
CARR TYPE SCARR,
END OF WA.
SELECT * INTO WA
FROM ( SFLIGHT AS F INNER JOIN SPFLI AS P
ON FCARRID = PCARRID AND
FCONNID = PCONNID )
INNER JOIN SCARR AS C
ON FCARRID = CCARRID
WHERE P~CITYFROM = 'FRANKFURT'
AND P~CITYTO = 'NEW YORK'
AND F~FLDATE BETWEEN '20010910' AND '20010920'
AND FSEATSOCC < FSEATSMAX.
WRITE: / WA-CARR-CARRNAME, WA-FLIGHT-FLDATE, WA-FLIGHT-CARRID,
WA-FLIGHT-CONNID.
ENDSELECT.
Regards,
Priyanka.

Similar Messages

  • Query with FULL OUTER JOIN , help pleaseeeeeeeeeeee...

    Hi everyone,
    I'm trying to write a query for a report in Oracle SQL, but i just can't figure out how to do it.
    I'm using Oracle 10g release 1.0 database and i execute my queris in SQL* PLUS ( eventually i'm gonna use them in Oracle Report Builder ) .
    here's what i have:
    i have four tables that are used for our inventory application. lets call them INCOMMING , INCOMMING_ITEMS , OUTGOING , OUTGOING_ITEMS.
    as you may have guessed , INCOMMING_ITEMS is the detail table for INCOMMING ( joined by IID column) and also OUTGOING_ITEMS is the detail table for OUTGOING ( joined by OID column ).
    here is the structure of them :
    INCOMMING
    IID varchar2
    CDATE date
    INCOMMING_ITEM
    IID varchar2
    PART_NO number
    QTY number
    OUTGOING
    OID varchar2
    CDATE date
    OUTGOING_ITEM
    OID varchar2
    PART_NO number
    QTY number
    now , the query i want, should return part_no , cdate , sum of OUTGOING qty , sum of INCOMMING qty .
    the result of the query should be sth like this :
    part_no     cdate     O_qty     I_qty
    100     01/05/06     10     0
    100     01/05/07     20     60
    200     01/06/02     0     50
    300     01/06/02     30     40
    this means that for some dates and for some parts, i may not have INCOMMING or OUTGOING data, but if at least one of the 2 last columns has a non-zero data, i should show the row ( like the first and third rows of my example result), and if both have data for the same PART_NO and the same CDATE, both should be showed in the same row. ( like the second row in my example result)
    i tried so much and came up with several huge and also time consuming queries, but then i read abt FULL OUTER JOIN somewhere and tried using that. here is what i came up with :
    SELECT
    PART_NO , CDATE , sum(II.QTY) I_QTY , SUM (OI.QTY) O_QTY
    FROM
         (OUTGOING O INNER JOIN OUTGOING_ITEM OI USING ( OID ) )
    FULL OUTER JOIN
         (INCOMMING I INNER JOIN INCOMMING_ITEM II USING ( IID ) )
    ON ( I.CDATE = O.CDATE AND II.PART_NO = OI.PART_NO)
    WHERE
    I.CDATE = :PARAMETER1
    AND O.CDATE = :PARAMETER1
    GROUP BY
    PART_NO , CDATE
    this query is short and fast , but the results r not what i expected. i mean, although i have used FULL OUTER JOIN in the query , but the results i get r sth like this :
    part_no     cdate     O_qty     I_qty
    100     01/05/07     20     60
    300     01/06/02     30     40
    which means only the rows that has both values are returned.
    any change i make to this query would make the SQL* PLUS hang , like when i use the cartesian product of two large tables, so i guess my changes wheren't in the right direction.
    i think its possible to write this query using FULL OUTER JOIN syntax, but i just can't find it.
    Can anybody pleaseeeeeeeeeeeee help me?
    thanx in advance,
    Maryam.

    Note: I wrote this on the fly -- hope there is no syntax errors, otherwise forgive me -- but you get the idea..
    select
    fromUnionAll.cdate, fromUnionAll.part_no,
    sum(fromUnionAll.O_qty) O_qty,
    sum(fromUnionAll.I_qty) I_qty
    from
    select
    iinner.cdate, iinner.part_no, 0 O_qty, iinner.I_qty
    from
    select
    i.cdate, ii.part_no,
    /* added the case only for the extreme case when there is
    no record anywhere for the given CDATE in INCOMMING_item */
    sum( ( case when ii.qty is not null then ii.qty else 0 end) ) I_qty
    from
    incomming i,
    incomming_item ii
    where
    i.iid = ii.iid (+)
    group by i.cdate, ii.part_no
    ) iinner
    union all
    select
    oinner.cdate, oinner.part_no, oinner.O_qty, 0 I_qty
    from
    select
    o.cdate, oi.part_no,
    /* added the case only for the extreme case when there is
    no record anywhere for the given CDATE in OUTGOING_item */
    sum( ( case when oi.qty is not null then oi.qty else 0 end) ) O_qty
    from
    outgoing o,
    outgoing_item oi
    where
    o.oid = oi.oid (+)
    group by o.cdate, oi.part_no
    ) oinner
    ) fromUnionAll
    group by fromUnionAll.cdate, fromUnionAll.part_no;
    --Samson                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • SQL Join help for multiple values in single field.

    Hello All,
    I need a help, I have two tables T1 and T2
    Content of T2 will be like
    T2. ID | T2.USERNAME
    ID1 | John
    ID2 | Peter
    ID3 | Mark
    Content of T1 is like
    T1.ID
    ID1 ID2 ID3
    ID2
    ID1 ID3
    I need to join these two tables and replace the T1.ID values with T2.USERNAME, for eg. Row one of T1 should be changed as John Peter Mark.
    Note: ID1 ID2 ID3 is a single value.
    Thanks for your help.
    Sathish.

    At Volder: I have made a slight enhancement. It's the same thing but the other way around as last week's regular expression thread. Remember the "and then the bird flew away"? ;-)
    SQL> create table t1
      2  as
      3  select 'ID1 ID2 ID3' id from dual union all
      4  select 'ID2' from dual union all
      5  select 'ID1 ID3' from dual union all
      6  select 'ID11 ID2' from dual
      7  /
    Tabel is aangemaakt.
    SQL> create table t2
      2  as
      3  select 'ID1' id, 'John' username from dual union all
      4  select 'ID2', 'Peter' from dual union all
      5  select 'ID3', 'Mark' from dual union all
      6  select 'ID11', 'Volder' from dual
      7  /
    Tabel is aangemaakt.
    SQL> select * from t1
      2   model
      3    reference r on (select t2.*, rownum rn from t2)
      4     dimension by (rn)
      5     measures(id, username)
      6    main m
      7     dimension by (id)
      8     measures(cast(id as varchar(200)) str)
      9     rules iterate(100) until (r.id[iteration_number+1] is null)
    10      (str[any] = replace(str[CV()], r.id[iteration_number+1],r.username[iteration_number+1]))
    11  /
    ID          STR
    ID1 ID2 ID3 John Peter Mark
    ID2         Peter
    ID1 ID3     John Mark
    ID11 ID2    John1 Peter
    4 rijen zijn geselecteerd.
    SQL> select id
      2       , str
      3    from t1
      4   model
      5         reference r on (select id,username,rownum rn from t2)
      6           dimension by (rn)
      7           measures (id, username)
      8         main m
      9           dimension by (id)
    10           measures (cast(' ' || id || ' ' as varchar2(200)) str)
    11           rules iterate(1000) until (r.id[iteration_number+1] is null)
    12           ( str[any] = replace
    13             ( str[cv()]
    14             , ' ' || r.id[iteration_number+1] || ' '
    15             , ' ' || r.username[iteration_number+1] || ' '
    16             )
    17           )
    18  /
    ID          STR
    ID1 ID2 ID3  John Peter Mark
    ID2          Peter
    ID1 ID3      John Mark
    ID11 ID2     Volder Peter
    4 rijen zijn geselecteerd.At Sathish: just look at the queries we have to come up with to do such a simple thing. As said many times before, I would also encourage you to change the design instead of executing the queries presented by Volder and me.
    Regards,
    Rob.
    Message was edited by:
    Rob van Wijk
    Just noticed I missed a final TRIM function around "str", so please add this.

  • Left Outer Join Help...

    Hi Everyone,
    I am still in the process of learning SQL, and I am having trouble specifically with the the left outer join.  I normally join tables using equijoin's, but I am not getting the right data set returns, and thought the using a left or right outer join would resolve the problem...
    Here is my SQL that is working properly with 1 left outer join.  I am building the query slowly so in the next SQL you will see where I am seeing the error.  I don't expect you to understand the data and columns I am trying to join, I believe the problems I am experiencing are related to syntax, and I am hoping you can find where my syntax errors are.
    select
      s.name as "Screen Name",
      sv.view_name as "View Name",
      s_view.name
    from
      s_screen s,
      s_screen_view sv
      left outer join s_view
      ON (sv.view_name = s_view.name)
    where
      sv.screen_id = '1-866A-1X3LU' and
      s.row_id = sv.screen_id and
      s.repository_id = '1-866A-1' and
      s_view.repository_id = '1-866A-1';
    Here is the SQL where I am experiencing the following error...
    Error:
    ORA-00904: "SV"."VIEW_NAME": invalid identifier
    00904. 00000 -  "%s: invalid identifier"
    *Cause:   
    *Action:
    Error at Line: 14 Column: 7
    Problematic SQL:
    select
      s.name as "Screen Name",
      sv.view_name as "View Name",
      s_view.name,
      s_applet.name as Applet
      --a.name as "Applet Name"
    from
      s_screen s,
      s_screen_view sv,
      s_view_wtmpl_it wti
      left outer join s_view
      ON (sv.view_name = s_view.name)
      left outer join s_applet
      ON (wti.name = s_applet.name)
    where
      sv.screen_id = '1-866A-1X3LU' and
      s.row_id = sv.screen_id and
      s.repository_id = '1-866A-1' and
      s_view.repository_id = '1-866A-1';
    Thanks in advance for your help.
    Chris

    Hi,
    cjpicc11 wrote:
    What is an Oracle style join vs. ANSI join?
    Would this be Oracle Style or ANSI Join?
      left outer join s_view
      ON (sv.view_name = s_view.name)
      left outer join s_applet
    Would this be Oracle Style or ANSI Join?
    s.row_id = sv.screen_id
    "Oracle style" or "old style" joins have commas between tables in the FROM clause, and the join conditions are all in the WHERE clause.  They do not use the keywords JOIN or ON.
    ANSI style joins have the keyword JOIN between tables (sometimes accompanied by other keywords, such as LEFT, OUTER or CROSS), and the join conditions are given in the FROM clause, usually after the keyword ON.
    The first example you gave is an ANSI style join.  It has the JOIN keyword, not a comma, between tables.
    The second example you gave:
    s.row_id = sv.screen_id
    could be part of either.  It could occur in an ANSI join (in the FROM clause, after the keyword ON), or it could occur in an old-style join (in the WHERE clause).

  • Reg: Full outer join help pls

    hi friends,
    i have created the following query using full outer join
    pls help me.
    Thanks
    Rajesh
    Message was edited by:
    Rajesh.mani

    I'm not sure if there should be a AND before FULL OUTER JOIN clause. I've always been reluctant to use ANSI syntax. here you can check the actual syntax. Bu then, from now on, try to keep quote your query between [pre] and [/pre] tags to preserve the format.
    Cheers
    Sarma.

  • Full Outer Join Help Needed

    Hi All,
    I am new to crystal and my sql.
    l have 2 command objects in that i have a common column pos_no.Now i need to apply Fullouterjoin between pos_no,but in crystal we don,t have that option.
    So i saw some threads that we need to apply leftouterjoin union rightouterjoin to get FOJ in db level ,i tried ,but i don't have much knowledge in my sql.So can any one please apply FOJ between Pos_no of these two queries.
    Please help me i strucked here
    1st Query:
    select
    MIN(till_close.start_transaction_id) AS start_trans_id,
    MAX(till_close.end_transaction_id) AS end_trans_id,
    pos_config.pos_no,
    pos_config.name AS pos_name,
    SUM(transaction_tender.amount) AS EodDeposit
    FROM
    till_close
    LEFT OUTER JOIN employee ON (till_close.employee_id = employee.employee_id)
    INNER JOIN pos_config ON (till_close.pos_config_id = pos_config.pos_config_id)
    INNER JOIN transaction_tender ON (till_close.end_transaction_id = transaction_tender.transaction_id)
    INNER JOIN media_type ON (transaction_tender.media_type_id = media_type.media_type_id)
    WHERE
      DATE_FORMAT(till_close.transaction_date,'%d/%m/%Y') = DATE_FORMAT(STR_TO_DATE(?,'%d/%m/%Y'),'%d/%m/%Y')
    AND transaction_tender.media_type_id NOT IN (10000)
    GROUP BY
    pos_config.pos_no
    ORDER BY
    pos_config.pos_no
    2nd Query:
    select
    pos_config.pos_no,
    pos_config.name AS pos_name,
    SUM(CASE WHEN transaction.transaction_type_id=7 AND ((SELECT COUNT(transaction_id) FROM transaction_tender WHERE transaction_id = transaction.transaction_id  AND media_type_id IN (SELECT media_type_id FROM media_type WHERE tender_type_id=12) AND amount < 0 )>0) THEN 0
      WHEN  transaction_tender.balance < 0
      THEN transaction_tender.amount
      ELSE (transaction_tender.amount - transaction_tender.balance)
      END) AS net
    FROM
    TRANSACTION
    INNER JOIN transaction_tender ON (transaction.transaction_id = transaction_tender.transaction_id)
    INNER JOIN media_type ON (transaction_tender.media_type_id = media_type.media_type_id)
    INNER JOIN pos_config ON (transaction.pos_config_id = pos_config.pos_config_id)
    WHERE
    transaction.transaction_date = DATE_FORMAT(STR_TO_DATE(?, '%d/%m/%Y'), '%Y-%m-%d')
    AND transaction.transaction_type_id IN (1,5,7)
    AND transaction.transaction_status_id = 3
    AND transaction.is_training_mode = 0
    GROUP BY
    pos_config.pos_no

    hi Divya,
    i would definitely take the advice from the folks here on a rewrite of your commands into one command.
    since you've got 2 commands that are bringing back different results you may wish to consider bringing back the results from the 2nd command via a sub-query.
    the general idea of the query is below, but you'll need to consult your online help for your database and or your dba for the correct syntax for your situation.
    notes:
    1) the subselect is in the bold font and is defined as a result set in the join...again, please consult your database help or the appropriated forum for your database as your syntax may vary
    2) the fields in the subquery are then referenced in the select clause using the TT alias assigned to the result set in the join
    3) the full outer join i guessed that you wanted on the two pos_no fields
    4) if you do try to do this method and are looking on your database forum, do a search on "subquery multiple columns"
    cheers,
    jamie
    select
    MIN(till_close.start_transaction_id) AS start_trans_id,
    MAX(till_close.end_transaction_id) AS end_trans_id,
    pos_config.pos_no AS PC_pos_no,
    pos_config.name AS pos_name AS PC_pos_name,
    SUM(transaction_tender.amount) AS EodDeposit,
    TT.pos_no AS TT_pos_no,
    TT.pos_name AS TT_pos_name,
    TT.net
    FROM
    till_close
    LEFT OUTER JOIN employee ON (till_close.employee_id = employee.employee_id)
    INNER JOIN pos_config ON (till_close.pos_config_id = pos_config.pos_config_id)
    INNER JOIN transaction_tender ON (till_close.end_transaction_id = transaction_tender.transaction_id)
    INNER JOIN media_type ON (transaction_tender.media_type_id = media_type.media_type_id)
    FULL OUTER JOIN
    (select
    pos_config.pos_no,
    pos_config.pos_name,
    SUM(CASE WHEN transaction.transaction_type_id=7 AND ((SELECT COUNT(transaction_id) FROM transaction_tender WHERE transaction_id = transaction.transaction_id  AND media_type_id IN (SELECT media_type_id FROM media_type WHERE tender_type_id=12) AND amount < 0 )>0) THEN 0
      WHEN  transaction_tender.balance < 0
      THEN transaction_tender.amount
      ELSE (transaction_tender.amount - transaction_tender.balance)
      END) AS net
    FROM
    TRANSACTION
    INNER JOIN transaction_tender ON (transaction.transaction_id = transaction_tender.transaction_id)
    INNER JOIN media_type ON (transaction_tender.media_type_id = media_type.media_type_id)
    INNER JOIN pos_config ON (transaction.pos_config_id = pos_config.pos_config_id)
    WHERE
    transaction.transaction_date = DATE_FORMAT(STR_TO_DATE(?, '%d/%m/%Y'), '%Y-%m-%d')
    AND transaction.transaction_type_id IN (1,5,7)
    AND transaction.transaction_status_id = 3
    AND transaction.is_training_mode = 0
    GROUP BY
    pos_config.pos_no) AS TT
    ON TT.pos_no =  pos_config.pos_no
    WHERE
      DATE_FORMAT(till_close.transaction_date,'%d/%m/%Y') = DATE_FORMAT(STR_TO_DATE(?,'%d/%m/%Y'),'%d/%m/%Y')
    AND transaction_tender.media_type_id NOT IN (10000)
    GROUP BY
    pos_config.pos_no
    ORDER BY
    pos_config.pos_no

  • Full Outer Join Help please

    Using : Oracle 11.2.0.1 on Windows XP
    create table tab40(bno varchar2(1),pno varchar2(7),weight number(3),lineno number(3));
    insert into tab40 values ('1','1115027',36,1);
    insert into tab40 values ('2','1115100',25,2);
    insert into tab40 values ('3','1115200',50,3);
    insert into tab40 values ('1','1112510',60,4);
    insert into tab40 values ('+','1112610',6,5);
    insert into tab40 values ('+','1112710',4,6);
    insert into tab40 values ('+','1110710',1,7);
    insert into tab40 values ('2','1117014',79,8);
    insert into tab40 values ('+','1117000',9,9);
    insert into tab40 values ('+','1117001',2,10);
    insert into tab40 values ('1','1317001',200,11);
    insert into tab40 values ('1','1697001',20,12);
    insert into tab40 values ('2','2997001',40,13);
    insert into tab40 values ('1','2996001',44,14);
    SQL> select * from tab40;
    B PNO         WEIGHT     LINENO
    1 1115027         36          1
    2 1115100         25          2
    3 1115200         50          3
    1 1112510         60          4
    + 1112610          6          5
    + 1112710          4          6
    + 1110710          1          7
    2 1117014         79          8
    + 1117000          9          9
    + 1117001          2         10
    1 1317001        200         11
    B PNO         WEIGHT     LINENO
    1 1697001         20         12
    2 2997001         40         13
    1 2996001         44         14
    14 rows selected.
    SQL>create table tab41(bno varchar2(1),pno varchar2(7),weight number(3),lineno number(3));
    insert into tab41 values ('1','1115027',36,1);
    insert into tab41 values ('2','1115100',25,2);
    insert into tab41 values ('3','1115200',50,3);
    insert into tab41 values ('1','1112510',60,4);
    insert into tab41 values ('+','1112610',6,5);
    insert into tab41 values ('+','1110710',1,6);
    insert into tab41 values ('2','1117014',79,7);
    insert into tab41 values ('+','1117000',9,8);
    insert into tab41 values ('1','1317001',200,9);
    insert into tab41 values ('1','1697001',20,10);
    insert into tab41 values ('2','2997001',40,11);
    insert into tab41 values ('1','2996001',44,12);
    insert into tab41 values ('+','1112710',4,13);
    insert into tab41 values ('+','3332710',8,14);
    SQL> select * from tab41;
    B PNO         WEIGHT     LINENO
    1 1115027         36          1
    2 1115100         25          2
    3 1115200         50          3
    1 1112510         60          4
    + 1112610          6          5
    + 1110710          1          6
    2 1117014         79          7
    + 1117000          9          8
    1 1317001        200          9
    1 1697001         20         10
    2 2997001         40         11
    B PNO         WEIGHT     LINENO
    1 2996001         44         12
    + 1112710          4         13
    + 3332710          8         14
    14 rows selected.
    SQL>Required Output :
    A       B          C          D        E       F          G          H
    1 1112510         60          4        1 2996001         44         12
    + 1112710          4          6        + 1112710          4         13
    2 1117014         79          8
    + 1117001          2         10
                                           1 2996001         44         12
                                           + 3332710          8         14Logic : The plus sign in Bno indicates that Packet No. was sent to customer with last non plus signed number packet (order by lineno) i.e. for example in tab40 there is line no. 6 whose master packet number is 1112510 (previous non plus signed rows's PNO). Now come to required output; which is showing that these plus signed packets (whose master PNO is that) are not as per tab40 vs tab41 something like full outer join between tab40 and tab41. Example :
    + 1112710 is associated with 1112510 in tab40, while in tab41 it is associated with 2996001; so it is difference.
    + 1117001 is associated with 1117014 in tab40, while it is not in tab41.
    vice versa;
    + 3332710 is associated with 2996001 in tab41, while it is not in tab40.
    I mean, if only plus marked row's PNO's mater packet number is not equal to master PNO(previous non plus marked BNO) in both the table, it should be part of the select query please. Generally this happens, when our packing department gets late delivery of prior master packet, so they just send the small packet with immediate available one (non plus) packet.
    Kindly let me; i am clear in my request or i should elaborate more. This may be more complex, because may be bad table/data design, but i need the output by select query please help me.
    Regards.
    Edited by: user12050217 on Jan 3, 2012 3:57 AM

    Yes, you have perfectly understood my question... just i added lineno > 1
    SELECT bno,
    pno,
    weight,
    lineno
    FROM ( SELECT tab40.*, lag (bno) OVER (ORDER BY pno) prev_bno
    FROM tab40
    ORDER BY pno)
    WHERE bno = '+' OR prev_bno = '+' and lineno > 1
    ORDER BY lineno
    and it worked, because there are no chances that plus marked row will be having lineno <=1
    B PNO         WEIGHT     LINENO
    1 1112510         60          4
    + 1112610          6          5
    + 1112710          4          6
    + 1110710          1          7
    2 1117014         79          8
    + 1117000          9          9
    + 1117001          2         10
    7 rows selected.

  • Join help pls

    hello can some one help me in joining the tables
    po_vendors and gl_je_headers or gl_he_headers
    thankyou

    No, not without known a minimum of your datamodel.
    Unless this is OracleApps (or other known ERP) for which you can find some guys which know the very complex datamodel, you have no chance without contact the vendor.
    You may interest to post your question into a eBusiness forum, choose the more appropriate in the following list :
    http://forums.oracle.com/forums/categoryHome.jspa?categoryID=84
    Nicolas.

  • Oracle table merge - JOIN help needed

    Can someone help me with this results we want:
    F_FCN_DT F_FCN_NUM F F_APPLD_DT F_APPLD_AMT F_R C_TCN_NUM
    2007-08-16 54 1 2008-02-08 4.06 131 40617700776019668
    2007-08-16 54 1 2008-02-08 4.06 135 40623900776014856
    2007-08-16 54 1 2007-11-02 1022.08 135 60630500002011374
    2007-08-16 54 1 2008-02-08 1022.08 135 30632600004003338
    Below listed are the two tables and data combinations, which need to be linked in this process.
    SQL> desc test11
    Name Null? Type
    F_FCN_DT NOT NULL CHAR(10)
    F_FCN_NUM NOT NULL NUMBER(5)
    F_FCN_MED_CD NOT NULL CHAR(1)
    F_APPLD_DT NOT NULL CHAR(10)
    F_APPLD_AMT NOT NULL NUMBER(13,2)
    F_RSN_CD NOT NULL CHAR(3)
    SQL> select * from test11;
    F_FCN_DT F_FCN_NUM F F_APPLD_DT F_APPLD_AMT F_R
    2007-08-16 54 1 2008-02-08 4.06 131
    2007-08-16 54 1 2008-02-08 4.06 135
    2007-08-16 54 1 2007-11-02 1022.08 135
    2007-08-16 54 1 2008-02-08 1022.08 135
    SQL> desc test12
    Name Null? Type
    F_FCN_DT NOT NULL CHAR(10)
    F_FCN_NUM NOT NULL NUMBER(9)
    F_FCN_MED_CD NOT NULL CHAR(1)
    F_APPLD_AMT NOT NULL NUMBER(23,6)
    C_TCN_NUM NOT NULL CHAR(17)
    SQL> select * from test12;
    F_FCN_DT F_FCN_NUM F F_APPLD_AMT C_TCN_NUM
    2007-08-16 54 1 4.06 40617700776019668
    2007-08-16 54 1 4.06 40623900776014856
    2007-08-16 54 1 1022.08 60630500002011374
    2007-08-16 54 1 1022.08 30632600004003338

    Hi,
    If you don't care which row from test11 gets paired with which row from test12 (as long as they're in the same group), then use the analytic ROW_NUMBER function to assign arbitrary ids to each row within each group, and add that id to the join condition, like this:
    WITH    test11_plus  AS
         SELECT  f_fcn_dt, f_fcn_num, f, f_appld_dt, f_appld_amt, f_r               -- or whatever you need
         ,     ROW_NUMBER () OVER ( PARTITION BY  f_fcn_dt, f_fcn_num, f, f_appld_amt     -- common columns only
                                   ORDER BY      NULL
                           )                    AS r_num
         FROM    test11
    ,     test12_plus  AS
         SELECT  f_fcn_dt, f_fcn_num, f, f_appld_amt, c_tcn_num                    -- or whatever you need
         ,     ROW_NUMBER () OVER ( PARTITION BY  f_fcn_dt, f_fcn_num, f, f_appld_amt     -- common columns only
                                   ORDER BY      NULL
                           )                    AS r_num
         FROM    test11
    SELECT     p11.*               -- Or list everything except r_num
    ,     p12.c_tcn_num
    FROM     test11_plus     p11
    JOIN     test12_plus     p12     ON     p11.f_fcn_dt     = p12.f_fcn_dt
                        AND     p11.f_fcn_num     = p12.f_fcn_num
                        AND     p11.f          = p12.f
                        AND     p11.f_appld_amt     = p12.f_appld_amt
                        AND     p11.r_num     = p12.r_num
    ;If the number of rows in the two tables may be different, then you may need an outer join.

  • SQL Join help required!

    Hi all
    I need help with a join.
    If you click the image link below, I need to find a way to join table 1 and table 2 to get table3:
    http://img229.imageshack.us/img229/1401/83192078uq8.jpg
    Help would be very much appreciated!
    Thanks in advance.

    with Table1 as(select 1234 as ProjectID,'Smith' as LastName from dual
    union select 1234,'Maria' from dual
    union select 1234,'Victo' from dual),
    Table2 as (select 1234 as ProjectID,200 as Val from dual
    union select 1234,300 from dual
    union select 1234,400 from dual)
    select nvl(a.ProjectID,b.ProjectID) as ProjectID,a.LastName,b.Val
      from Table1 a full join Table2 b
        on 1=0;
    PROJECTID  LASTN  VAL
         1234  Maria  null
         1234  Smith  null
         1234  Victo  null
         1234  null    200
         1234  null    300
         1234  null    400

  • SQL Join Help Please

    Hi there,
    Thanks for checking this out.
    I have a slight problm with my query. It's not causing any
    errors at all, it's just not giving me the desired results I'm
    trying to get.
    <cfquery name="getSchemaFields" datasource="#request.dsn#"
    username="#request.username#" password="#request.password#">
    SELECT schema_#getSchemas.schema_token#_fields.*,
    fields_content.*
    FROM schema_#getSchemas.schema_token#_fields
    LEFT JOIN fields_content
    ON schema_#getSchemas.schema_token#_fields.field_type_uuid =
    fields_content.field_content_field_uuid
    WHERE fields_content.field_content_item_id =
    #getSchemas.item_id#
    AND fields_content.field_content_item_uuid =
    '#getSchemas.item_uuid#'
    </cfquery>
    This query returns all the results for items in
    "fields_content" where it matches in the "where" clause. But I need
    the query to return all records from the
    "schema_#getSchemas.schema_token#_fields" table regardless of
    wether or not any records are found in the "fields_content" table.
    I thought a LEFT JOIN would solve this and it does to a point.
    If I remove the WHERE and AND clauses I get all the rows, but
    I can't use this method because the records returned from
    "fields_content" need to be specific to an ID I have in the page
    (this comes from another query I do called "getSchemas").
    So basically, it works but only outputs rows where it finds
    it in both tables. If I take out there WHERE clause (which I need
    it causes other problems with my site, I need it to only pull back
    rows based on an ID)
    Is this making any sense? Probably not...hope you can
    understand, I'm getting mighty stressed at this one :-s
    Any help whatsoever would be great.
    Many thanks,
    Mickey.

    Hi Draves,
    Thanks for your response. I think I'm getting a little closer
    because now I simply get a syntax error. Like you said, I think
    it's something to do with paraenthesis. My query is now as follows:
    <cfquery name="getSchemaFields" datasource="#request.dsn#"
    username="#request.username#" password="#request.password#">
    SELECT schema_#getSchemas.schema_token#_fields.*,
    fields_content.*
    FROM schema_#getSchemas.schema_token#_fields
    LEFT JOIN fields_content
    ON schema_#getSchemas.schema_token#_fields.field_type_uuid =
    fields_content.field_content_field_uuid
    AND ON fields_content.field_content_item_id =
    <cfqueryparam cfsqltype="cf_sql_integer"
    value="#getSchemas.item_id#" />
    AND ON fields_content.field_content_item_uuid =
    <cfqueryparam cfsqltype="cf_sql_varchar"
    value="#getSchemas.item_uuid#" />
    </cfquery>
    But I get the following error (a bit nicer than before - if
    that makes sense, but still puzzling):
    [Macromedia][SequeLink JDBC Driver][ODBC
    Socket][Microsoft][ODBC Microsoft Access Driver] Syntax error
    (missing operator) in query expression ''.
    The error occurred in
    C:\ColdFusion8\wwwroot\coldbox\edit.cfm: line 152
    150 : ON
    schema_#getSchemas.schema_token#_fields.field_type_uuid =
    fields_content.field_content_field_uuid
    151 : AND ON fields_content.field_content_item_id =
    <cfqueryparam cfsqltype="cf_sql_integer"
    value="#getSchemas.item_id#" />
    152 : AND ON fields_content.field_content_item_uuid =
    <cfqueryparam cfsqltype="cf_sql_varchar"
    value="#getSchemas.item_uuid#" />
    153 : </cfquery>
    Hope that you'd be able to assist further. I have no clue
    where to put the paranthesis - I read the links you gave but they
    confused the hell out of me!! Clearly I don't consider myself a
    programmer, haha.
    Many thansk for your kind help!
    Mickey.

  • SQL Join Help

    SQL Experts,
    I have a query below. Can I improve the SQL joining order in highlighted section - Lines 31 to 40
    SNIOTM.SNI_PA_OTM_ARC_DRVR -- around 200K rows
    SNIOTM.SNI_PA_ORD_SHP_STATUS -- 8 rows
    Edited by: 922411 on May 12, 2013 7:42 PM

    922411 wrote:
    SQL Experts,
    I have a query below. Can I improve the SQL joining order in highlighted section - Lines 31 to 40
    SNIOTM.SNI_PA_OTM_ARC_DRVR -- around 200K rows
    SNIOTM.SNI_PA_ORD_SHP_STATUS -- 8 rowsYou need not worry about the Joining Order, Oracle CBO is efficient enough to choose the Most Efficient way of joining the tables. However, it will depend on your Oracle Version, which you have not mentioned. If you are 10g or higher then you certainly need not worry.
    If you sense a problem in performance of query, then I will suggest you read the linked threads in {message:id=9360003} and post the details as specified.
    Only this will help us provide releavant suggestions and avoid guessing games.
    PS:- Do not forget to post Oracle Version and the Explain Plan in {noformat}{noformat} tags, exactly as specified.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Query join help

    Hello everyone, I kindly need help with a query I am trying to write. I think it's meant to be a kind of join but I'm a little unsure. Here is an example:
    select a.person_id, a.company, b.name, e.element, f.value
    from a, b, e, f
    where a.person_id = b.person_id
    and e.el_id = f.el_id
    -- e.t.c
    Lets say this returns
    person_id, company, name, element, value
    1, Vols, krog, lunch, 34
    2, Mols, flog, munch, 24
    The problem now is the table e. I want to get all values from table e that meet a certain criteria. As in:
    select e.element
    where e.name = 'EVALUE'
    Lets say this returns
    element
    food
    lunch
    munch
    And combine it with the top query. But I also want to show all of the other values, a.person_id, a.company, b.name for each row.
    So my goal is to have finally:
    person_id, company, name, element, value
    1, Vols, krog, lunch, 34
    1, Vols, krog, food, 0
    1, Vols, krog, munch, 0
    2, Mols, flog, munch, 24
    2, Mols, flog, food, 0
    2, Mols, flog, lunch, 0
    That is to have a default for zero where no join exists for value and not to duplicate anything although I could still use distinct.
    Can anyone help with this?

    with t1 as (
                select a.person_id, a.company, b.name, e.element, f.value
                  from a, b, e, f
                  where a.person_id = b.person_id
                  and e.el_id = f.el_id
                  -- e.t.c
         t2 as (
                select e.element
                 where e.name = 'EVALUE'
    select  person_id,
            company,
            name,
            t2.element,
            sum(
                case t1.element
                  when t2.element then value
                  else 0
                end
               ) value
      from  t1,
            t2
      group by person_id,
               company,
               name,
      order by person_id,
               company,
               name,
               t2.element
    /For example:
    with t1 as (
                select 1 person_id, 'Vols' company, 'krog' name, 'lunch' element, 34 value from dual union all
                select 2, 'Mols', 'flog', 'munch', 24 from dual
         t2 as (
                select 'food' element from dual union all
                select 'lunch' from dual union all
                select 'munch' from dual
    select  person_id,
            company,
            name,
            t2.element,
            sum(
                case t1.element
                  when t2.element then value
                  else 0
                end
               ) value
      from  t1,
            t2
      group by person_id,
               company,
               name,
               t2.element
      order by person_id,
               company,
               name,
               t2.element
    PERSON_ID COMP NAME ELEME      VALUE
             1 Vols krog food           0
             1 Vols krog lunch         34
             1 Vols krog munch          0
             2 Mols flog food           0
             2 Mols flog lunch          0
             2 Mols flog munch         24
    6 rows selected.
    SQL> SY.

  • IN in NATURAL JOIN help

    SELECT name,telephone
    FROM Customer where cid IN
    (SELECT cid
    FROM Order where oid IN
    (SELECT oid
    FROM OrderDetal where icode IN
    (SELECT icode
    FROM item where itemName=’Dry Fish 200g’)));
    How can I convert this query to Natural join?
    Thanks in Advance

    Hi,
    You can't do that with NATURAL JOIN.
    Forget about NATURAL JOIN.  I've never seen it used outside of a texbook (or on a forum like this) for good reasons.  If you add a column to any table, that may change the results of any NATURAL JOIN involving that table.  You don't want to look at all those queries whenever you condiser adding a new column to a table; you don't even want to keep track of which queries you'd have to look at.
    Upon sober reflection, I think it might be possible to do this with NATURAL JOIN, depending on how your tables are designed. I can't tell if it's possible or not without seeing the CREATE TABLE statements for all the tables involved, including unique constraints. If it is possible, it would look like this:
    SELECT        c.name, c.telephone
    FROM          Customer   c
    NATURAL JOIN  Orders     o -- ORDER is not a good table name
    NATURAL JOIN  OrderDetal od
    NATURAL JOIN  Item  i
    WHERE         i.ItemName = 'Dry Fish 200g'
    Depending on your data, you may need SELECT DISTINCT.
    Message was edited by: FrankKulash
    NATURAL JOIN is still a bad idea.

  • Inner Join help req??Urgent!!!!!!!!!!!!!!!

    I had written the below qery : its showing error that:for pooled tble,cluster tble,projection veiws join is not allowd : 'CDPOS'.
      SELECT ekkoebeln cdhdrudate cdhdrtcode cdhdrtabname INTO CORRESPONDING FIELDS OF TABLE it_cdhdr
      FROM ( ( ekko AS ekko INNER JOIN cdhdr AS cdhdr  ON ekkoebeln = cdhdrobjectid )
                            INNER JOIN cdpos AS cdpos  ON ekkoebeln = cdposobjectid )
                WHERE ekko~ebeln = i_ebeln      AND
                      ekko~frgke EQ 'R'         AND
                      ekko~procstat EQ '05'     AND
                      cdhdr~tcode    IN ('ME29N' , 'ME28') AND
                      cdpos~tabname  = 'EKKO'
                      GROUP BY     ekko~ebeln
                                   ekko~frgke
                                   ekko~procstat
                                   cdhdr~udate
                                   cdhdr~tcode
                                   cdpos~tabname.
    so what shd i do,i need this table for the fetching data.
    Plz tell me any way to solve this......its Urgent!!!!!!!!!!!!!!!!!!
    Regards
    Vipin

    Hi Vipin,
    U can't perform JOIN with Pooled or Cluster tables
    And U can use only Internal table after FOR ALL ENTRIES <internal table>
    Try to store whole data of CDPOS in the internal table and then use it..
    See below links to get idea on how to work with cluster and pooled tables...
    trying to join A018 and KONP .... here A018 is Pooled table... Look at below threads...
    Re: abap query join A018/KONP
    Can't perform join table
    Hope it will solve your problem..
    <b>Reward points if useful.</b>
    Thanks & Regards
    ilesh 24x7

Maybe you are looking for

  • Speed Issues Fixed In 9.1.2

    Lost all my thumbnails updating to iPhoto 9.1.2 but rebuilding the thumbnail database solved the problem. Best of all, the speed issues are finally gone in iPhoto 9.1.2. Opening the application is way faster again (2 seconds as opposed to 12 seconds

  • All my itunes files are visible but none will play. Error message file cannot be found

    All my track file names and other details are visible in Itunes but none will play. single click on a track and an exclamation mark embeds at the front of the track name bar double click or click play and the error message I get says, "The song  coul

  • Dynamical Tree handling multiple requests

    Hi, I think I already explained well my need. I currently have an implementation of a tree control that is dynamic. So, when I click to open a node, it sends a request to server to ask for its children. Attached you have my tree code. And here is a s

  • Redirecting email to iCloud problem(s)

    Hi everyone,      I find now we have an iPad I pretty much only check our mail on it.  The problem that creates is when we go onto the computer there are 100's of emails to delete.  I figured an iCloud email account would be best so when we deleted t

  • Does "sysctl -w net.link.ether.bridge_ipfw=1" not work (anymore)?

    Hi, According to the ipfw man page I should be able to use:      sysctl -w net.link.ether.bridge_ipfw=1 in order to route the bridged packets to ipfw. When I try that on (Mountain) Lion though I get:      net: class is not implemented I am missing so